C# 将字节数组转换为位图图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14337071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert array of bytes to bitmapimage
提问by Hossein Narimani Rad
I'm going to convert array of bytes to System.Windows.Media.Imaging.BitmapImage
and show the BitmapImage
in an image control.
我要将字节数组转换为System.Windows.Media.Imaging.BitmapImage
并BitmapImage
在图像控件中显示。
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;
}
采纳答案by Clemens
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.OnLoadto achieve that the image is loaded immediately, otherwise the stream needs to be kept open, as in your second example.
在第一个代码示例中,using
在实际加载图像之前关闭流(通过离开块)。您还必须设置BitmapCacheOptions.OnLoad以实现立即加载图像,否则流需要保持打开状态,如您的第二个示例。
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:
从BitmapImage.StreamSource的备注部分:
Set the CacheOption property to BitmapCacheOption.OnLoad if you wish to close the stream after the BitmapImage is created.
如果您希望在创建 BitmapImage 后关闭流,请将 CacheOption 属性设置为 BitmapCacheOption.OnLoad。
Besides that, you can also use built-in type conversion to convert from type byte[]
to type ImageSource
(or the derived BitmapSource
):
除此之外,您还可以使用内置类型转换将类型转换byte[]
为类型ImageSource
(或派生的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[]
.
当您将类型属性ImageSource
(例如图像控件的Source
属性)绑定到类型string
,Uri
或的源属性时,会隐式调用 ImageSourceConverter byte[]
。
回答by Mohammad Dehghan
In the first case, you defined your MemoryStream
in a using
block, which causes the object to be disposed when you go out of the block. So you return a BitmapImage
with a disposes (and non-existing) stream.
在第一种情况下,您MemoryStream
在一个using
块中定义了您的对象,这会导致当您离开该块时对象被释放。因此,您返回BitmapImage
带有处置(和不存在)流的 a 。
MemoryStream
s keep no unmanaged resources, so you can leave the memory and let the GC handle the freeing process (but that's not a good practice).
MemoryStream
s 不保留非托管资源,因此您可以保留内存并让 GC 处理释放过程(但这不是一个好习惯)。