I created a lib where I need to fill a byte array of 307200 elements (320x240x4(=32bit)) in the correct order for a bitmap, the display uses the format RGBA, and I would like to avoid using interop to use GetBitmapBits as I'm doing at the moment, while I prefer writing it in c# code to understand how bytes are printed on it.
Can someone help me?
Here is my actual code
/// <summary>
/// LONG GetBitmapBits(
/// __in HBITMAP hbmp,
/// __in LONG cbBuffer,
/// __out LPVOID lpvBits
/// );
/// </summary>
/// <param name="hbmp"></param>
/// <param name="cbBuffer"></param>
/// <param name="lpvBits"></param>
/// <returns></returns>
[DllImport("Gdi32", EntryPoint = "GetBitmapBits")]
private extern static long GetBitmapBits([In] IntPtr hbmp, [In] int cbBuffer, [Out] byte[] lpvBits);
[DllImport("Gdi32", EntryPoint = "GdiFlush")]
private extern static void GdiFlush();
private void FillPixelArray(Bitmap bmp, ref byte[] array, bool bw = false)
{
Color tmp;
if (!bw)
{
IntPtr hbmp = bmp.GetHbitmap();
GdiFlush();
GetBitmapBits(hbmp, array.Length * Marshal.SizeOf(typeof(byte)), array);
}
else
{
for (int x = 0; x < LgLcd.NativeConstants.LGLCD_BMP_WIDTH; ++x)
{
for (int y = 0; y < LgLcd.NativeConstants.LGLCD_BMP_HEIGHT; ++y)
{
tmp = bmp.GetPixel(x, y);
array[y * 160 + x] = (byte)((tmp.R == 255 && tmp.G == 255 && tmp.B == 255) ? 0 : 255);
}
}
}
}
Another thing, is GetBitmapBits faster than any implementation I can do in C#?