C# WPF BitmapSource ImageSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/444331/
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
WPF BitmapSource ImageSource
提问by user38309
I am binding an Image.Source property to the result of the property shown below.
我将 Image.Source 属性绑定到如下所示的属性的结果。
public BitmapSource MyImageSource
{
get
{
BitmapSource source = null;
PngBitmapDecoder decoder;
using (var stream = new FileStream(@"C:\Temp\logo.png", FileMode.Open, FileAccess.Read, FileShare.Read))
{
decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
if (decoder.Frames != null && decoder.Frames.Count > 0)
source = decoder.Frames[0];
}
return source;
}
}
For some reason this is failing during the rendering of the image (Deep in the PresentationCore assembly). I am certain the image is not corrupt as I can successfully show the same image w/o the binding
由于某种原因,这在渲染图像期间失败(在 PresentationCore 程序集中)。我确定图像没有损坏,因为我可以在没有绑定的情况下成功显示相同的图像
<Image Name="FooImage" Source="/logo.png" />
I have to bind the image source in code because I will eventually be creating the image stream from a base64 string.
我必须在代码中绑定图像源,因为我最终将从 base64 字符串创建图像流。
Anyone know if this is a bug w/ WPF? or am I doing something incorrectly?
有谁知道这是否是 WPF 的错误?还是我做错了什么?
采纳答案by user38309
The problem was the BitmapCacheOption option, changing to BitmapCacheOption.OnLoad works.
问题是 BitmapCacheOption 选项,更改为 BitmapCacheOption.OnLoad 有效。
With BitmapCacheOption.None the BitmapSource isn't decoded until the image is rendered, but the stream with the png in it is already disposed at that point. If you cache OnLoad, it'll decode right away and cache the results, rather than trying to decode later when the stream no longer exists.
使用 BitmapCacheOption.None,在渲染图像之前不会对 BitmapSource 进行解码,但在该点处已经处理了其中包含 png 的流。如果您缓存 OnLoad,它将立即解码并缓存结果,而不是在流不再存在时尝试稍后解码。
回答by Adrian
Are you positive it's a PNG and not just a renamed Bitmap or Jpeg? If you create a new Bitmap image and then just rename it and change the file extension, this error is reproducible.
您确定它是 PNG 而不仅仅是重命名的位图或 Jpeg?如果您创建一个新的位图图像,然后只是重命名它并更改文件扩展名,则此错误是可重现的。
If I use a known PNG with your code, I don't get your issue, but a COM exception is thrown:
如果我在您的代码中使用已知的 PNG,我不会遇到您的问题,但会引发 COM 异常:
The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))
句柄无效。(来自 HRESULT 的异常:0x80070006 (E_HANDLE))
Can you try it out with a random PNG off the web and see if you get the same result?
您可以使用网络上的随机 PNG 进行尝试,看看是否得到相同的结果?
回答by Adrian
Also, have you tried just using a BitmapImage to load the image? It works fine with PNG, BMP, and JPEG.
另外,您是否尝试过仅使用 BitmapImage 加载图像?它适用于 PNG、BMP 和 JPEG。
It's also a specialized type of BitmapSource, so you could just replace your code in your property with this:
它也是一种特殊类型的 BitmapSource,因此您可以将属性中的代码替换为:
BitmapImage img = new BitmapImage(new Uri(@"C:\Temp\logo.png"));
return img;