【问题标题】:Error when setting MemoryStream as the source of a BitMapImage将 MemoryStream 设置为 BitMapImage 的源时出错
【发布时间】:2015-06-27 07:18:48
【问题描述】:

对于 Windows Phone 8 开发,我读到的所有内容都说您必须将流设置为位图图像的源,以便将 byte[] 数组转换为位图图像。但是,当我实施此操作时,我在以下位置收到错误:

 bitmapImage.SetSource(stream);   

错误:

 An exception of type 'System.Exception' occurred in System.Windows.ni.dll 
 but was not handled in user code

 Additional information: The component cannot be found. (Exception from 
 HRESULT: 0x88982F50) 

代码片段:

 byte[] bytes = value as byte[];
 MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length);
 BitmapImage bitmapImage = new BitmapImage();
 bitmapImage.SetSource(stream);

【问题讨论】:

标签: c# windows-phone-8 memorystream bitmapimage


【解决方案1】:

您存储在bytes 中的数组不是有效图像。您需要进一步回到 value 填充的位置,并找出为什么它没有填充图像的字节数组。

【讨论】:

  • 我的应用程序拍摄了一张照片,将其发送到 wcf 服务,然后将其发送到 sql server 数据库,稍后我可以从应用程序中的不同页面访问它。你不会碰巧知道这行代码是否会导致它?: byte[] theBytes = Encoding.UTF8.GetBytes(upload);这就是我将图像转换为 wcf 服务上的字节以便将其添加到 sql server 数据库的方式。
  • 是的,这就是您的问题的根源,您不能通过Encoding. 方法发送任意数据。如果需要将其转换为字符串,可以使用Convert.ToBase64StringConvert.FromBase64String。然而,更好的做法是让您的 sql 表使用 varbinary 而不是 varchar,那么您根本不需要将其转换为字符串。
  • 你就是男人!我采纳了你的建议,把HttpPostedFile变成了byte[]数组,问题终于解决了。
【解决方案2】:

此类奇怪的错误通常是由未能将流设置到起始位置引起的。此外,最好将一次性对象包装在 using 语句中。

这能解决问题吗?

 var bytes = value as byte[]; 
 using(var stream = new MemoryStream(bytes, 0, bytes.Length))
 {
    //set this to the beginning of the stream
    stream.Position = 0;
    var bitmapImage = new BitmapImage();
    bitmapImage.SetSource(stream);
 }

【讨论】:

  • 不幸的是,它在同一个地方给出了同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
相关资源
最近更新 更多