谁能告诉我如何将图像(个.jpg,.gif .BMP)转换成字节数组?
Can anyone please tell me how an image(.jpg,.gif,.bmp) is converted into a byte array ?
我假定你想要的是像素值。假设位图
是 System.Windows.Media.Imaging.BitmapSource
:
I've assumed what you want is the pixel values. Assuming bitmap
is a System.Windows.Media.Imaging.BitmapSource
:
int stride = bitmap.PixelWidth * ((bitmap.Format.BitsPerPixel + 7) / 8);
byte[] bmpPixels = new byte[bitmap.PixelHeight * stride];
bitmap.CopyPixels(bmpPixels, stride, 0);
请注意,该步幅是所需的像素DDATA的各行的字节数。提供一些更多的解释here.
Note that the 'stride' is the number of bytes required for each row of pixel ddata. Some more explanation available here.