我将把字节数组转换为
System.Windows.Media.Imaging.BitmapImage
并显示在图像控件中。使用第一段代码时,什么都不会发生!没有错误,也没有显示图像。但是当我使用第二段代码时,它正常工作!有人能解释一下发生了什么吗?第一段代码如下: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;
}
}
第二段代码如下:
public BitmapImage ToImage(byte[] array)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = new System.IO.MemoryStream(array);
image.EndInit();
return image;
}
CacheOption
的一个非常好的观点,但是在你的代码中,MemoryStream
在图像加载之前仍然会被释放。除非调用EndInit()
加载图像。这是这种情况吗? - Mohammad DehghanBitmapCacheOption.OnLoad
正是这样工作的。在EndInit
过程中,图像会同步加载,因此流将在图像加载之前 不会 被处理。 - ClemensCacheOption
的那一点。我修改了我的代码,现在它可以工作了。 - Hossein Narimani Rad