我有一个原始像素数据的数组。我希望将其转换为8bpp位图。
public static Bitmap ByteToGrayBitmap(byte[] rawBytes, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, bitmap.PixelFormat);
Marshal.Copy(rawBytes, 0, bitmapData .Scan0, rawBytes.Length);
bitmap.UnlockBits(bitmapData);
return bitmap;
}
位图看起来像彩色图像而不是灰度图像。