2012-02-18 76 views

回答

2
 Bitmap img = (Bitmap)Image.FromFile(@"C:\..."); 

     Color[,] pixels = new Color[img.Width, img.Height]; 

     for (int x = 0; x < img.Width; x++) 
     { 
      for (int y = 0; y < img.Height; y++) 
      { 
       pixels[x, y] = img.GetPixel(x, y); 
      } 
     } 
0

的快速版本upvoted答案:

public static int[][] ImageToArray(Bitmap bmp) { 
     int height = bmp.Height; // Slow properties, read them once 
     int width = bmp.Width; 
     var arr = new int[height][]; 
     var data = bmp.LockBits(new Rectangle(0, 0, width, height), 
        System.Drawing.Imaging.ImageLockMode.ReadOnly, 
        System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
     try { 
      for (int y = 0; y < height; ++y) { 
       arr[y] = new int[width]; 
       System.Runtime.InteropServices.Marshal.Copy(
        (IntPtr)((long)data.Scan0 + (height-1-y) * data.Stride), 
        arr[y], 0, width); 
      } 
     } 
     finally { 
      bmp.UnlockBits(data); 
     } 
     return arr; 
    } 

使用Color.FromArgb()來映射像素值的顏色。

相關問題