WPF的BitmapSource的ImageSource

WPF的BitmapSource的ImageSource

本文介绍了WPF的BitmapSource的ImageSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定一个Image.Source属性下面所示的属性的结果。

I am binding an Image.Source property to the result of the property shown below.

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;
    }
}

有关某种原因,这是图像的渲染过程中失败(深在presentationCore组装)。我敢肯定的形象是不是贪官,我可以成功显示同一图像W / O的结合

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

<Image Name="FooImage" Source="/logo.png" />

我要在code图像源绑定,因为我最终会从创建一个base64字符串中的图像流。

I have to bind the image source in code because I will eventually be creating the image stream from a base64 string.

任何人都知道这是一个错误瓦特/ WPF?还是我做一些错误的?

Anyone know if this is a bug w/ WPF? or am I doing something incorrectly?

推荐答案

的问题是BitmapCacheOption选项,切换到BitmapCacheOption.OnLoad工作。

The problem was the BitmapCacheOption option, changing to BitmapCacheOption.OnLoad works.

使用BitmapCacheOption.None所述的BitmapSource不德$ C $光盘直到图像呈现,但与PNG流中它已经布置在该点。如果缓存的OnLoad,它会去code马上和缓存结果,而不是试图脱code以后当流不再存在。

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.

这篇关于WPF的BitmapSource的ImageSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:52