WPF 冻结位图图像不显示

发布于 2024-12-12 00:49:14 字数 631 浏览 0 评论 0原文

我正在使用绑定到 UI 上的图像属性的视图模型,并且视图模型包含 ImageSource 属性。我使用以下函数设置该属性

    private BitmapImage GetImageFromUri(Uri urisource)
    {
        if (urisource == null)
            return null;

        var image = new BitmapImage();
        image.BeginInit();
        image.UriSource = urisource;
        image.EndInit();
        image.Freeze(); //commenting this shows the image if the routine is called from the proper thread.

        return image;
   }

出于某种奇怪的原因,在以下代码中,当我对 BitmapImage 调用 Freeze 时,它​​不会出现在主窗口上。我没有遇到异常或崩溃。有人能帮我解决这个问题吗? 我正在异步设置图像属性,因此我需要能够使用创建的图像,假设 GetImageFromUri 调用是从 UI 线程以外的线程进行的。

I am using a viewmodel bound to an image property on the UI and the viewmodel contains an ImageSource property. I set that property using the following function

    private BitmapImage GetImageFromUri(Uri urisource)
    {
        if (urisource == null)
            return null;

        var image = new BitmapImage();
        image.BeginInit();
        image.UriSource = urisource;
        image.EndInit();
        image.Freeze(); //commenting this shows the image if the routine is called from the proper thread.

        return image;
   }

For some odd reason, in the following code, when I call Freeze on my BitmapImage, it does not appear on the main window.I get no exception or crash. Can anybody help me with this?
I am setting the image property asynchronously so I need to be able to use the created image, assuming the GetImageFromUri call was made from a thread other than the UI thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野侃 2024-12-19 00:49:15

如果我使用 StreamSource,对我来说就足够了:

public static BitmapSource ToBitmap(this MemoryStream stream)
    {
        try
        {
            using (stream)
            {
                stream.Position = 0;
                var image = new BitmapImage();
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = stream;
                image.EndInit();
                image.Freeze();
                return image;
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

但是如果我使用 UriSource 我需要:

public static async Task<BitmapSource> ToBitmapAsync(this string sourceUri)
    {
        try
        {
            using (var client = new WebClient())
            using (var stream = await client.OpenReadTaskAsync(new Uri(sourceUri)))
            using (var ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                return ms.ToBitmap();
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

BitmapSourceUriSource,但它在 xaml 渲染后起作用

IF i use StreamSource, for me is enough:

public static BitmapSource ToBitmap(this MemoryStream stream)
    {
        try
        {
            using (stream)
            {
                stream.Position = 0;
                var image = new BitmapImage();
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = stream;
                image.EndInit();
                image.Freeze();
                return image;
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

But if i use UriSource i need:

public static async Task<BitmapSource> ToBitmapAsync(this string sourceUri)
    {
        try
        {
            using (var client = new WebClient())
            using (var stream = await client.OpenReadTaskAsync(new Uri(sourceUri)))
            using (var ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                return ms.ToBitmap();
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

BitmapSource has UriSource, but it works after xaml rendered

尴尬癌患者 2024-12-19 00:49:14

在冻结 BitmapImage 之前尝试设置 CacheOption。看看这是否有效 -

var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = urisource;
image.EndInit();
image.Freeze();

Try setting the CacheOption for BitmapImage before freezing it. See if this works -

var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = urisource;
image.EndInit();
image.Freeze();
暖阳 2024-12-19 00:49:14

在冻结它之前,您需要完全渲染它。
您应该尝试侦听 SourceUpdated 事件,然后才冻结图像。

顺便说一句,如果您想在此之后修改图像,则必须克隆它。

Before you freeze it, you need to fully render it.
You should try to listen to the SourceUpdated event, and only then freeze the image.

On a side note, if you ever want to modify the Image after that, you will have to Clone it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文