I have raw pixel data coming from a camera in RGB8 format which I need to convert to a Bitmap
. However, the Bitmap PixelFormat
only seems to support RGB 16, 24, 32, and 48 formats.
I attempted to use PixelFormat.Format8bppIndexed
, but the image appears discolored and inverted.
public static Bitmap CopyDataToBitmap(byte[] data)
{
var bmp = new Bitmap(640, 480, PixelFormat.Format8bppIndexed);
var bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
bmp.UnlockBits(bmpData);
return bmp;
}
Is there any other way to convert this data type correctly?