【问题标题】:WPF WriteableBitmap to byte arrayWPF WriteableBitmap 到字节数组
【发布时间】:2014-10-23 14:00:22
【问题描述】:

是否有将 WriteableBitmap 转换为字节数组的方法?如果有办法从中获取它,我也将 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();

【问题讨论】:

  • 您的代码中的 WriteableBitmap 在哪里?
  • 在类范围内声明,但我对其进行了编辑以使其更有意义。
  • 现在您将 Data 更改为 BackBuffer 更有意义

标签: c# wpf writeablebitmap


【解决方案1】:

您的代码将图像数据编码为 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)。

【讨论】:

  • UWP 中的 WriteableBitmap 对象上没有 .Format.BitsPerPixel。此外,CopyPixels 也不存在。为什么 WPF 和 UWP 中相同的数据类型会有如此大的差异?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多