亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

UI 在圖像控件加載 BitmapImage 時凍結

UI 在圖像控件加載 BitmapImage 時凍結

C#
慕神8447489 2022-01-15 19:37:19
我的 WPF MVVM 應用程序通過Webclient.DownloadFileAsync(url, fileLocation). 該過程順利進行,下載圖片時根本沒有凍結。但是當我將圖像文件呈現給用戶時會出現問題 - 應用程序變得無響應。下載文件后,我將圖像文件分配給 BitmapImage:public async void LoadFileToBitmapImage(string filePath)    {        _isDownloading = false;        await FileToBitmapImage(filePath);    }public Task FileToBitmapImage(string filePath)    {        return Task.Run(() =>        {            var executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);            var imageLocation = Path.Combine(executableLocation, filePath);            var bi = new BitmapImage();            bi.BeginInit();            bi.UriSource = new Uri(imageLocation);            bi.EndInit();            bi.Freeze();            Image = bi;        });    }圖片.cs:private BitmapImage _image;public BitmapImage Image    {        get => _image;        set        {            _image = value;            NotifyOfPropertyChange("Image");        }    }XAML 圖像綁定:<Image Source="{Binding Image, IsAsync=True}" Margin="3"/>下載圖像并將其呈現給用戶時會出現問題。圖像越大,向用戶呈現圖像所需的時間越多,應用程序無響應的時間就越長。當應用程序凍結以檢查線程并獲取以下信息時,我嘗試單擊暫停,不幸的是它沒有為我提供任何信息。任何幫助都感激不盡!編輯值得注意的是,在引發 PropertyChanged 事件之后,應用程序變得無響應,而不是之前。也許這與將圖像渲染到 UI 有關?
查看完整描述

2 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

首先,如果你保存圖像,直接將綁定更改為字符串/uri,沒有 BitmapImage,不需要創建它,Wpf 為你處理 public BitmapImage Image ===> public Uri Image

并刪除 FileToBitmapImage。


查看完整回答
反對 回復 2022-01-15
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

我花了幾天的時間來找到解決這個問題的簡單方法。我需要在不凍結 UI 的情況下以高質量顯示一百多張圖像。


我嘗試了對綁定等的各種修改,最后只有通過代碼和 Source 屬性集創建 Image 控件才起作用,然后 Image 出現在界面元素樹中。


在 XAML 中只有空的 ContentControl:


    <ContentControl x:Name="ImageContent"/>

在代碼中:


    static readonly ConcurrentExclusiveSchedulerPair _pair = new  ConcurrentExclusiveSchedulerPair();


    // Works for very big images

    public void LoadImage(Uri imageUri)

    {

        var image = new System.Windows.Controls.Image(); // On UI thread

        RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);


        Task.Factory.StartNew(() =>

        {

            var source = new BitmapImage(imageUri); // load ImageSource


            Dispatcher.RunOnMainThread(() =>

            {

                image.Source = source; // Set source while not in UI


                // Add image in UI tree

                ImageContent.Content = image; // ImageContent is empty ContentControl

                ImageContent.InvalidateVisual();

            });

        }, default, TaskCreationOptions.LongRunning, _pair.ExclusiveScheduler);

    }

使用 CacheOption OnLoad 加載圖像效果更好。


    public static ImageSource BitmapFromUri(Uri source)

    {

        if (source == null)

            return new BitmapImage(source);


        using (var fs = new FileStream(source.LocalPath, FileMode.Open))

        {

            var bitmap = new BitmapImage();

            bitmap.BeginInit();

            bitmap.StreamSource = fs;

            bitmap.CacheOption = BitmapCacheOption.OnLoad;

            bitmap.EndInit();

            bitmap.Freeze();

            return bitmap;

        }

    }


查看完整回答
反對 回復 2022-01-15
  • 2 回答
  • 0 關注
  • 237 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號