初始化BitmapImage时出现System.NotSupportedException

时间:2019-02-17 11:41:30

标签: c# wpf mvvm bitmap webclient

我从API接收了一系列带有附件的消息,并根据它们为Models创建了UserControls。在模型中,我调用Initial方法来下载本地存储中的图像。但是,如果我在运行应用程序后立即打开对话框(来自API的接收数组),则会收到错误消息System.NotSupportedException in PresentationCore.dll  Additional Information: No pixel format information found.

这是型号代码:

最有趣的是我抓住了它,但还是得到了

public class MediaMessageAttachment : ViewModelBase
    {
        public string FullImageURI { get; set; }
        public string VideoURI { get; set; }
        public string Title { get; set; }
        public bool HasVideo => VideoURI != null;
        public bool HasImage => FullImageURI != null;
        public string Duration { get; set; }
        public bool IsLoading
        {
            get { return isLoading; }
            set
            {
                isLoading = value;
                RaisePropertyChanged("IsLoading");
            }
        }

        private BitmapImage image;
        private string previewImageURI;
        private bool isLoading;

        public string PreviewImageURI { get { return previewImageURI; } set { previewImageURI = value; RaisePropertyChanged("PreviewImageURI"); Initial(); } }

        public BitmapImage Image
        {
            get { return image; }
            set
            {
                image = value;
                RaisePropertyChanged("Image");
            }
        }

        public RelayCommand OpenImageCommand
        {
            get;
            private set;
        }

        public MediaMessageAttachment()
        {
            OpenImageCommand = new RelayCommand(() => OpenImage());
        }

        private void OpenImage()
        {
            if (HasImage)
                PopupVM.ShowPopup(FullImageURI);
            else if (HasVideo)
                PopupVM.ShowPopup(VideoURI);
        }
        public async Task Initial()
        {
            IsLoading = true;
            if (!Directory.Exists(Environment.CurrentDirectory + "\\temp"))
            {
                Directory.CreateDirectory(Environment.CurrentDirectory + "\\temp");
            }
            using (WebClient client = new WebClient())
            {
                var path = Environment.CurrentDirectory + "\\temp\\" + PreviewImageURI.Substring(PreviewImageURI.LastIndexOf('/') + 1);
                if (File.Exists(path))
                    DownloadFileCompleted(null, null);
                else
                {
                    client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
                    await client.DownloadFileTaskAsync(new Uri(PreviewImageURI), path);
                }
            }
        }

        private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            try
            {
                var path = Environment.CurrentDirectory + "\\temp\\" + PreviewImageURI.Substring(PreviewImageURI.LastIndexOf('/') + 1);
                Image = new BitmapImage();
                Image.BeginInit();
                Image.UriSource = new Uri(path);
                Image.CacheOption = BitmapCacheOption.OnLoad;
                Image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                Image.EndInit();
                Image.Freeze();
                IsLoading = false;
            }
            catch (System.NotSupportedException)
            {
                Initial();
            }
        }
    }

0 个答案:

没有答案