将Bitmap数据转为byte[]数据,且每个数据代表像素值
Bitmap bmp = new Bitmap(Image.FromFile(path));
private byte[] Getpix(bmp)
{
BitmapData bmdat = bmp.LockBits(new Rectangle(System.Drawing.Point.Empty, bmp.Size),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
byte[] buffer = new byte[bmdat.Stride * bmdat.Height];
Marshal.Copy(bmdat.Scan0, buffer, 0, buffer.Length);
bmp.UnlockBits(bmdat);
return buffer;
}
如果bmp为8位灰度图时,输出的数据位像素数据的3倍,(a,a,a)格式。需要使用循环将像素提取出来。
for (int i = 0; i < bytes3.Length; i++)
{
bytes3[i] = (byte)(bytes[i * 3]);
}