转换图像,以字节数组数组、字节、图像

2023-09-03 03:18:24 作者:平凡的生活

谁能告诉我如何将图像(个.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.