JPG 是一种压缩格式,这就是为什么它的大小(以及相应的 Byte 数组的大小)通常会远小于 1920*1080*3。为了从 JPG 中获取字节数组,您可以使用流:
Image myImage;
...
byte[] result;
using (MemoryStream ms = new MemoryStream()) {
myImage.Save(ms, ImageFormat.Jpeg);
result = ms.ToArray();
}
如果您想要的只是字节数组形式的像素,则必须将 JPG 转换为 BMP(或其他原始未压缩格式)
Bitmap myImage;
...
byte[] rgbValues = null;
BitmapData data = myImage.LockBits(new Rectangle(0, 0, myImage.Width, myImage.Height), ImageLockMode.ReadOnly, value.PixelFormat);
try {
IntPtr ptr = data.Scan0;
int bytes = Math.Abs(data.Stride) * myImage.Height;
rgbValues = new byte[bytes];
Marshal.Copy(ptr, rgbValues, 0, bytes);
}
finally {
myImage.UnlockBits(data);
}
}