如何将8位灰度图像的数据放入字节数组?我尝试了以下代码:
private byte[] loadBitmap(string filename, int width, int height)
{
byte[] data = new byte[width * height];
BitmapData bmpData = null;
Bitmap slice = new Bitmap(filename);
Rectangle rect = new Rectangle(0, 0, slice.Width, slice.Height);
bmpData = slice.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
int size = bmpData.Height * bmpData.Stride;
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size);
slice.UnlockBits(bmpData);
return data;
}
但是,由于Format8pppIndexed,数据数组的结果有一些错误。有什么想法吗?