Problems with WPF / BackgroundWorker and BitmapSource

I start with WPF and try to make the home project familiar with technology. I have a simple form in which the user selects an image file, then I show the EXIF ​​data along with a thumbnail of the image. This works fine, but when I select a RAW image file (~ 9 MB), there may be a slight delay while the thumb is loading, so I decided to use BackgroundWorker to decode the image and the user can view the EXIF ​​data, then when the image is decoded, it is displayed .

The BitmapSource object is declared in the BackgroundWorkers DoWork method:

worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
    string filePath = args.Argument as string;

    BitmapDecoder bmpDecoder = BitmapDecoder.Create(new Uri(filePath), BitmapCreateOptions.None, BitmapCacheOption.None);
    BitmapSource bmpSource = bmpDecoder.Frames[0];
    bmpSource.Freeze(); //As suggested by Paul Betts

    args.Result = bmpSource;
};

, , - Image RunWorkerCompleted. , .

worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
    imgThumb.Source = args.Result as BitmapSource;
};

imgThumb.Dispatcher.BeginInvoke() , , , args.Result , imgThumb? ?

, ( , , ).

imgThumb.Dispatcher.Invoke(new Action<BitmapSource>(
    delegate(BitmapSource src)
    {
        imgThumb.Source = src;
    }
), bmpSource);

.

DoWork BitmapCreateOptions.None, .DelayCreation, RAW ( Canon.CR2 - , ), jpg. Canon Codec, RAW ?

, . ( HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

+5
2

Freeze() BitmapSource, (Freezing , )

+8

, .

: WriteableBitmap. , .

.

+2

All Articles