1. 程式人生 > WINDOWS開發 >C# 獲取影象畫素

C# 獲取影象畫素

第一種方法:

 1  public static byte[] GetImagePixel(Bitmap img)
 2 {
 3             byte[] result = new byte[img.Width*img.Height*3];
 4             int n = 0;
 5             for (int i = 0; i < img.Height; i++)
 6             {
 7                 for (int j = 0; j < img.Width; j++)
 8                 {
9 result[n] = img.GetPixel(i,j).R; 10 result[n+1] = img.GetPixel(i,j).G; 11 result[n+2] = img.GetPixel(i,j).B; 12 n += 3; 13 } 14 } 15 return result; 16 }

第二種方法:

  1 using System;
  2
using System.Collections.Generic; 3 using System.Text; 4 using System.Drawing; 5 using System.Drawing.Imaging; 6 7 namespace ImageCompare 8 { 9 public unsafe class UnsafeBitmap 10 { 11 Bitmap bitmap; 12 13 // three elements used for MakeGreyUnsafe 14 int
width; 15 BitmapData bitmapData = null; 16 Byte* pBase = null; 17 18 public UnsafeBitmap(Bitmap bitmap) 19 { 20 this.bitmap = new Bitmap(bitmap); 21 } 22 23 public UnsafeBitmap(int width,int height) 24 { 25 this.bitmap = new Bitmap(width,height,PixelFormat.Format24bppRgb); 26 } 27 28 public void Dispose() 29 { 30 bitmap.Dispose(); 31 } 32 33 public Bitmap Bitmap 34 { 35 get 36 { 37 return (bitmap); 38 } 39 } 40 41 private Point PixelSize 42 { 43 get 44 { 45 GraphicsUnit unit = GraphicsUnit.Pixel; 46 RectangleF bounds = bitmap.GetBounds(ref unit); 47 48 return new Point((int)bounds.Width,(int)bounds.Height); 49 } 50 } 51 52 public void LockBitmap() 53 { 54 GraphicsUnit unit = GraphicsUnit.Pixel; 55 RectangleF boundsF = bitmap.GetBounds(ref unit); 56 Rectangle bounds = new Rectangle((int)boundsF.X, 57 (int)boundsF.Y, 58 (int)boundsF.Width, 59 (int)boundsF.Height); 60 61 // Figure out the number of bytes in a row 62 // This is rounded up to be a multiple of 4 63 // bytes,since a scan line in an image must always be a multiple of 4 bytes 64 // in length. 65 width = (int)boundsF.Width * sizeof(PixelData); 66 if (width % 4 != 0) 67 { 68 width = 4 * (width / 4 + 1); 69 } 70 bitmapData = 71 bitmap.LockBits(bounds,ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb); 72 73 pBase = (Byte*)bitmapData.Scan0.ToPointer(); 74 } 75 76 public PixelData GetPixel(int x,int y) 77 { 78 PixelData returnValue = *PixelAt(x,y); 79 return returnValue; 80 } 81 82 public void SetPixel(int x,int y,PixelData colour) 83 { 84 PixelData* pixel = PixelAt(x,y); 85 *pixel = colour; 86 } 87 88 public void UnlockBitmap() 89 { 90 bitmap.UnlockBits(bitmapData); 91 bitmapData = null; 92 pBase = null; 93 } 94 public PixelData* PixelAt(int x,int y) 95 { 96 return (PixelData*)(pBase + y * width + x * sizeof(PixelData)); 97 } 98 } 99 public struct PixelData 100 { 101 public byte blue; 102 public byte green; 103 public byte red; 104 } 105 } 106 107 108 public static byte[] GetUnsafeImagePixel(Bitmap img) 109 { 110 byte[] result = new byte[img.Width * img.Height * 3]; 111 int n = 0; 112 UnsafeBitmap unsafeBitmap = new UnsafeBitmap(img); 113 unsafeBitmap.LockBitmap(); 114 int x = 0,y = 0; 115 do 116 { 117 for (x = 0; x < img.Width; x++) 118 { 119 PixelData pixel = unsafeBitmap.GetPixel(x,y); 120 result[n] = pixel.red; 121 result[n + 1] = pixel.green; 122 result[n + 2] = pixel.blue; 123 n += 3; 124 } 125 y++; 126 } while (y!=img.Height); 127 unsafeBitmap.UnlockBitmap(); 128 return result; 129 130 }

第三種速度最快:

 1 public static unsafe byte[] ConvertTo8Byte(Bitmap img)
 2 {
 3    byte[] result = new byte[img.Width*img.Height];
 4     int n = 0;
 5     BitmapData data = img.LockBits(new Rectangle(0,0,img.Width,img.Height),ImageLockMode.ReadOnly, 6                                    PixelFormat.Format24bppRgb);
 7     var bp = (byte*)data.Scan0.ToPointer();
 8    
 9     for (int i = 0; i != data.Height; i++)
10     {
11         for (int j = 0; j != data.Width; j++)
12         {
13             result[n] = bp[i * data.Stride + j * 3];
14             n++;
15             //0.3R+0.59G+0.11B
16             //float value = 0.11F * bp[i * data.Stride + j * 3] + 0.59F * bp[i * data.Stride + j * 3 + 1] +
17             //              0.3F * bp[i * data.Stride + j * 3 + 2];
18            
19         }
20     }
21     img.UnlockBits(data);
22     //img.Dispose();
23     return result;
24    // ColorPalette palette = bit.Palette;
25     //for (int i = 0; i != palette.Entries.Length; i++)
26     //{
27     //    palette.Entries[i] = Color.FromArgb(i,i,i);
28     //}
29     //bit.Palette = palette;
30     //bit.Save(destFile,ImageFormat.Bmp);
31     //img.Dispose();
32     //bit.Dispose();
33 }