Elspeedy
Elspeedy

Reputation: 3

Asynchronous Thumbnail Converter

I've got a problem with WPF's thumbnail creation. While I can create a thumbnail from and image in a different thread, I cannot transfer it to the main UI thread.

The conversion into a thumbnail has already been handled, so that's not an issue anymore; this is how I'm doing that right now:

public ImageSource Image
{
    get
    {
      BitmapImage bi = new BitmapImage();

      bi.BeginInit();
      bi.DecodePixelWidth = 100;
      bi.CacheOption = BitmapCacheOption.OnLoad;
      bi.UriSource = new Uri(value.ToString());
      bi.EndInit();

      return bi;
    }
}

<Image Source="{Binding Image, IsAsync=True}"/>

 
How do I manage to pass the thumbnail to the the mainthread?
Thanks a lot in advance!

Upvotes: 0

Views: 308

Answers (1)

Clemens
Clemens

Reputation: 128013

In order to make the BitmapImage cross-thread accessible, you have to freeze it. You should also keep the created BitmapImage in case the getter is called multiple times.

private BitmapImage image;

public ImageSource Image
{
    get
    {
        if (image == null)
        {
            image = new BitmapImage();
            image.BeginInit();
            image.DecodePixelWidth = 100;
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.UriSource = new Uri(value.ToString());
            image.EndInit();
            image.Freeze(); // here
        }
        return image;
    }
}

Upvotes: 2

Related Questions