转换的字节数组的BitmapImage [英] convert array of bytes to 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 theBitmapImage
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 typeImageSource
(or the derivedBitmapSource
):var bitmap = (BitmapSource)new ImageSourceConverter().ConvertFrom(array);
ImageSourceConverter is called implicitly when you bind a property of type
ImageSource
(e.g. the Image control'sSource
property) to a source property of typestring
,Uri
orbyte[]
.这篇关于转换的字节数组的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!