Reputation: 1
I want to import photos and examine each pixel to determine its RGB value and then put each pixel (or its equivalent value in RGB) in an array or similar data structure that keeps the pixels in their original order.
The most important thing I need to know is how to separate the pixels and determine each pixel value.
Upvotes: 0
Views: 859
Reputation: 942255
A fast version of the upvoted answer:
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;
}
Use Color.FromArgb() to map the pixel value to a Color.
Upvotes: 0
Reputation: 49013
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);
}
}
Upvotes: 2