从 MemoryStream 创建 WPF 位图图像 png、gif
我在从 Web 请求获取的 png 和 gif 字节的 MemoryStream
创建 BitmapImage
时遇到了一些问题。字节似乎下载得很好,并且 BitmapImage 对象的创建没有问题,但图像实际上并未在我的 UI 上呈现。仅当下载的图像类型为 png 或 gif(适用于 jpeg)时,才会出现此问题。
下面是演示该问题的代码:
var webResponse = webRequest.GetResponse();
var stream = webResponse.GetResponseStream();
if (stream.CanRead)
{
Byte[] buffer = new Byte[webResponse.ContentLength];
stream.Read(buffer, 0, buffer.Length);
var byteStream = new System.IO.MemoryStream(buffer);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 30;
bi.StreamSource = byteStream;
bi.EndInit();
byteStream.Close();
stream.Close();
return bi;
}
为了测试 Web 请求是否正确获取字节,我尝试了以下操作,将字节保存到磁盘上的文件中,然后使用 UriSource
而不是StreamSource
并且它适用于所有图像类型:
var webResponse = webRequest.GetResponse();
var stream = webResponse.GetResponseStream();
if (stream.CanRead)
{
Byte[] buffer = new Byte[webResponse.ContentLength];
stream.Read(buffer, 0, buffer.Length);
string fName = "c:\\" + ((Uri)value).Segments.Last();
System.IO.File.WriteAllBytes(fName, buffer);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 30;
bi.UriSource = new Uri(fName);
bi.EndInit();
stream.Close();
return bi;
}
有人有任何闪光吗?
I am having some trouble creating a BitmapImage
from a MemoryStream
from png and gif bytes obtained from a web request. The bytes seem to be downloaded fine and the BitmapImage
object is created without issue however the image is not actually rendering on my UI. The problem only occurs when the downloaded image is of type png or gif (works fine for jpeg).
Here is code that demonstrates the problem:
var webResponse = webRequest.GetResponse();
var stream = webResponse.GetResponseStream();
if (stream.CanRead)
{
Byte[] buffer = new Byte[webResponse.ContentLength];
stream.Read(buffer, 0, buffer.Length);
var byteStream = new System.IO.MemoryStream(buffer);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 30;
bi.StreamSource = byteStream;
bi.EndInit();
byteStream.Close();
stream.Close();
return bi;
}
To test that the web request was correctly obtaining the bytes I tried the following which saves the bytes to a file on disk and then loads the image using a UriSource
rather than a StreamSource
and it works for all image types:
var webResponse = webRequest.GetResponse();
var stream = webResponse.GetResponseStream();
if (stream.CanRead)
{
Byte[] buffer = new Byte[webResponse.ContentLength];
stream.Read(buffer, 0, buffer.Length);
string fName = "c:\\" + ((Uri)value).Segments.Last();
System.IO.File.WriteAllBytes(fName, buffer);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 30;
bi.UriSource = new Uri(fName);
bi.EndInit();
stream.Close();
return bi;
}
Anyone got any light to shine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
直接在
.BeginInit()
之后添加bi.CacheOption = BitmapCacheOption.OnLoad
:如果没有这个,BitmapImage 默认使用延迟初始化,此时流将被关闭。在第一个示例中,您尝试从可能已关闭甚至已处置的 MemoryStream 中读取图像。第二个示例使用文件,该文件仍然可用。
另外,不要写得
更好
Add
bi.CacheOption = BitmapCacheOption.OnLoad
directly after your.BeginInit()
:Without this, BitmapImage uses lazy initialization by default and stream will be closed by then. In first example you try to read image from possibly
garbage-collectedclosed or even disposed MemoryStream. Second example uses file, which is still available.Also, don't write
better
我正在使用这段代码:
也许你应该删除这一行:
I'm using this code:
May be you should delete this line: