【问题标题】:WPF Byte array to BitmapImageWPF 字节数组到 BitmapImage
【发布时间】:2015-01-08 12:33:05
【问题描述】:

我阅读了“Byte array to BitmapImage”的所有首页结果,我找到了 Byte array to BitmapImage WP byte[] to BitmapImage in silverlight 问题是我的代码对我不起作用,我收到了这个错误:

'System.Windows.Media.Imaging.BitmapImage' 不包含 'SetSource' 的定义并且没有扩展方法 'SetSource' 接受类型的第一个参数 'System.Windows.Media.Imaging.BitmapImage' 可以找到(你是 缺少 using 指令或程序集引用?)

我的主要代码是:

int stride = CoverPhotoBitmap.PixelWidth * 4;
int size = CoverPhotoBitmap.PixelHeight * stride;
byte[] CoverPhotoPixels = new byte[size];
CoverPhotoBitmap.CopyPixels(CoverPhotoPixels, stride, 0);

byte[] HiddenPhotoPixels = new byte[size];
HiddenPhotoBitmap.CopyPixels(HiddenPhotoPixels, stride, 0);
ResultPhotoBitmap = ByteArraytoBitmap(HiddenPhotoPixels);

我的方法是:

public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
    MemoryStream stream = new MemoryStream(byteArray);
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(stream);
    return bitmapImage;
}

【问题讨论】:

  • 你想如何使用这个位图,你想把它设置为图像控件吗?
  • 您想设置Source 属性,而不是调用 Silverlight BitmapImage 的SetSource 方法。无论如何,这无济于事,因为它需要一个您没有的编码图像缓冲区(如 PNG 或 JPEG)。您必须从字节数组创建一个BitmapSource,或者将一个字节数组写入WriteableBitmap
  • @Amer Zafar 是的,我想将它用作图像控件
  • @Clemens 如您所见,我使用了 Jcl 向我建议的问题并解决了我后来的问题,我目前的问题是 WPF 中的 SetSource 替代方案是什么。

标签: c# arrays wpf bitmap


【解决方案1】:

您发现的示例似乎是 Silverlight 特有的。异常说明您调用的方法(SetSource)不存在。您需要做的是设置StreamSource

public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
    MemoryStream stream = new MemoryStream(byteArray);

    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = stream;
    bitmapImage.EndInit();

    return bitmapImage;
}

【讨论】:

  • 现在我收到此错误:PresentationCore.dll 中出现“System.NotSupportedException”类型的未处理异常附加信息:未找到适合完成此操作的成像组件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
相关资源
最近更新 更多