转换的字节数组的BitmapImage [英] convert array of bytes to bitmapimage

查看:190
本文介绍了转换的字节数组的BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的字节数组转换为 System.Windows.Media.Imaging.BitmapImage 键,显示的BitmapImage 在图像控制。

当我使用的第一个code,指出发生!不显示任何错误,也没有图像。但是,当我使用第二个它工作正常!任何人都可以说,这是怎么回事?

第一code是在这里:

 公开的BitmapImage ToImage(byte []数组)
{
   使用(System.IO.MemoryStream毫秒​​=新System.IO.MemoryStream(阵列))
   {
       BitmapImage的形象=新的BitmapImage();
       image.BeginInit();
       image.StreamSource =毫秒;
       image.EndInit();
       返回形象;
   }
}

第二code是在这里:

 公开的BitmapImage ToImage(byte []数组)
{
   BitmapImage的形象=新的BitmapImage();
   image.BeginInit();
   image.StreamSource =新System.IO.MemoryStream(数组);
   image.EndInit();
   返回形象;
 }


解决方案

在第一code例如流被关闭(由离开使用块)之前图像被实际装载。您还必须设置 BitmapCacheOptions.OnLoad 以实现该图像是立即加载,否则流需要保持打开,如在第二个实施例。

 公开的BitmapImage ToImage(byte []数组)
{
    使用(VAR毫秒=新System.IO.MemoryStream(阵列))
    {
        VAR图像=新的BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad; // 这里
        image.StreamSource =毫秒;
        image.EndInit();
        返回形象;
    }
}

在<一个说明部分href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.streamsource.aspx\"相对=nofollow> BitmapImage.StreamSource :


  

设置CacheOption财产,如果你想BitmapCacheOption.OnLoad
  创建的BitmapImage后关闭流



除此之外,你还可以使用内置的类型转换,从类型转换字节[] 键入的ImageSource (或其衍生的BitmapSource

  VAR位=(的BitmapSource)新ImageSourceConverter()ConvertFrom(阵列)。

当您绑定类型的属性的ImageSource (如Image控件的来源属性)ImageSourceConverter被隐式调用类型的源属性字符串乌里字节[]

I'm going to convert array of bytes to System.Windows.Media.Imaging.BitmapImage and show the BitmapImage in an image control.

When I'm using the first code, noting happens! no error and no image is displayed. But when I'm using the second one it works fine! can anyone say what is going on?

first code is here:

public BitmapImage ToImage(byte[] array)
{
   using (System.IO.MemoryStream ms = new System.IO.MemoryStream(array))
   {
       BitmapImage image = new BitmapImage();
       image.BeginInit();
       image.StreamSource = ms;
       image.EndInit();
       return image;
   }
}

second code is here:

public BitmapImage ToImage(byte[] array)
{
   BitmapImage image = new BitmapImage();
   image.BeginInit();
   image.StreamSource = new System.IO.MemoryStream(array);
   image.EndInit();
   return image;
 }

解决方案

In the first code example the stream is closed (by leaving the using block) before the image is actually loaded. You must also set BitmapCacheOptions.OnLoad to achieve that the image is loaded immediately, otherwise the stream needs to be kept open, as in your second example.

public BitmapImage ToImage(byte[] array)
{
    using (var ms = new System.IO.MemoryStream(array))
    {
        var image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad; // here
        image.StreamSource = ms;
        image.EndInit();
        return image;
    }
}

From the Remarks section in BitmapImage.StreamSource:

Set the CacheOption property to BitmapCacheOption.OnLoad if you wish to close the stream after the BitmapImage is created.


Besides that, you can also use built-in type conversion to convert from type byte[] to type ImageSource (or the derived BitmapSource):

var bitmap = (BitmapSource)new ImageSourceConverter().ConvertFrom(array);

ImageSourceConverter is called implicitly when you bind a property of type ImageSource (e.g. the Image control's Source property) to a source property of type string, Uri or byte[].

这篇关于转换的字节数组的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆