如何在 C# 中进行高效的 BitmapSource 到 byte[] 的转换,反之亦然?
问问题
10251 次
1 回答
11
BitmapSource 到 byte[]:
private byte[] BitmapSourceToArray(BitmapSource bitmapSource)
{
// Stride = (width) x (bytes per pixel)
int stride = (int)bitmapSource.PixelWidth * (bitmapSource.Format.BitsPerPixel / 8);
byte[] pixels = new byte[(int)bitmapSource.PixelHeight * stride];
bitmapSource.CopyPixels(pixels, stride, 0);
return pixels;
}
byte[] 到 BitmapSource:
private BitmapSource BitmapSourceFromArray(byte[] pixels, int width, int height)
{
WriteableBitmap bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * (bitmap.Format.BitsPerPixel / 8), 0);
return bitmap;
}
于 2014-02-28T14:10:06.430 回答