[英]WPF WriteableBitmap to byte array
反正有没有将 WriteableBitmap 转换为字节数组? 如果有办法从中获取它,我也将可写位图分配给 System.Windows.Controls.Image 源。 我试过了,但在 FromHBitmap 上遇到了一般的 GDI 异常。
System.Drawing.Image img = System.Drawing.Image.FromHbitmap(wb.BackBuffer);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
myarray = ms.ToArray();
您的代码以 PNG 格式对图像数据进行编码,但FromHBitmap
需要原始的、未编码的位图数据。
尝试这个:
var width = bitmapSource.PixelWidth;
var height = bitmapSource.PixelHeight;
var stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
var bitmapData = new byte[height * stride];
bitmapSource.CopyPixels(bitmapData, stride, 0);
...其中bitmapSource
是您的WriteableBitmap
(或任何其他BitmapSource
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.