Say I have a bitmap image, is it possible to iterate through all the individual bytes in the image? If yes, how?
Another solution is to use LockBits and Marshal.Copy to convert your bitmap into an array. I needed this solution because I had two images that differed only in their color depth and the other proffered solutions don't handle that well (or are too slow).
using (Bitmap bmp = new Bitmap(fname)) {
// Convert image to int32 array with each int being one pixel
int cnt = bmp.Width * bmp.Height * 4 / 4;
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Int32[] rgbValues = new Int32[cnt];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(bmData.Scan0, rgbValues, 0, cnt);
bmp.UnlockBits(bmData);
for (int i = 0; i < cnt; ++i) {
if (rgbValues[i] == 0xFFFF0000)
Console.WriteLine ("Red byte");
}
}