Converting a Bitmap to a pixel array
{
public byte red;
public byte green;
public byte blue;
}
then I read it using some Unsafe code thus:
public pixel[,] BitmapToArray(Bitmap b)
{
pixel[,] bmpData=new pixel[b.Width,b.Height];
// GDI+ still lies to us – the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride – b.Width*3;
int nWidth = b.Width * 3;
for(int y=0;y<b.Height;++y)
{
for(int x=0; x < nWidth; ++x )
{
switch(x%3)
{
case 0:
bmpData[x/3,y] =
new pixel();
bmpData[x/3,y].blue = (
byte)(p[0]);
break;
case 1:
bmpData[x/3,y].green = (
byte)(p[0]);
break;
case 2:
bmpData[x/3,y].red = (byte)(p[0]);
break;
}
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return bmpData;
}