小智 7
我只是在研究这个并且不得不投入我的两分钱,虽然在原始帖子后几年(以防万一其他人来寻找我正在研究的同样的事情).
我有一个Image控件,需要使用Stream在后台加载它的图像,然后显示.
我一直遇到的问题是BitmapSource,它的Stream源和Image控件都必须在同一个线程上.
在这种情况下,使用Binding并将其设置为IsAsynch = true将引发跨线程异常.
BackgroundWorker非常适合WinForms,你可以在WPF中使用它,但我更喜欢避免在WPF中使用WinForm程序集(不建议对项目进行膨胀,这也是一个很好的经验法则).在这种情况下,这应该抛出一个无效的交叉引用异常,但我没有测试它.
事实证明,一行代码将使这些工作中的任何一个:
//Create the image control
Image img = new Image {HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch};
//Create a seperate thread to load the image
ThreadStart thread = delegate
     {
         //Load the image in a seperate thread
         BitmapImage bmpImage = new BitmapImage();
         MemoryStream ms = new MemoryStream();
         //A custom class that reads the bytes of off the HD and shoves them into the MemoryStream. You could just replace the MemoryStream with something like this: FileStream fs = File.Open(@"C:\ImageFileName.jpg", FileMode.Open);
         MediaCoder.MediaDecoder.DecodeMediaWithStream(ImageItem, true, ms);
         bmpImage.BeginInit();
         bmpImage.StreamSource = ms;
         bmpImage.EndInit();
         //**THIS LINE locks the BitmapImage so that it can be transported across threads!! 
         bmpImage.Freeze();
         //Call the UI thread using the Dispatcher to update the Image control
         Dispatcher.BeginInvoke(new ThreadStart(delegate
                 {
                         img.Source = bmpImage;
                         img.Unloaded += delegate 
                                 {
                                         ms.Close();
                                         ms.Dispose();
                                 };
                          grdImageContainer.Children.Add(img);
                  }));
     };
//Start previously mentioned thread...
new Thread(thread).Start();
假设您使用数据绑定,将Binding.IsAsync属性设置为 True 似乎是实现此目的的标准方法。如果您使用后台线程 + Dispatcher 对象在代码隐藏文件中加载位图,这是异步更新 UI 的常用方法