This project is read-only.

Invalid cross-thread access Exception on ImageTools.ImageExtensions.ToBitmap

Mar 22, 2011 at 12:10 AM

Hi,

Thanks for making this library. Very useful!

I got "Invalid cross-thread access" exception when calling ImageTools.ImageExtensions.ToBitmap in the code sample below. Any suggestion?

 

Thanks,

         Meir :->

 

Decoders.AddDecoder<GifDecoder>();

ExtendedImage ei = new ExtendedImage();

 

 

ei.LoadingCompleted += new EventHandler (ExtendedImage_LoadingCompleted);

 

 

 

ei.LoadingFailed += new EventHandler<UnhandledExceptionEventArgs>(ExtendedImage_LoadingFailed);

 

 

 

 

 

 

ei.UriSource = new Uri("http://MY_URL/Something.gif", UriKind.Absolute);

 

 

 

 

 

void ExtendedImage_LoadingCompleted(object sender, EventArgs e)

{

 

 ExtendedImage ei = (ExtendedImage)sender;

 

 WriteableBitmap wb = ImageTools.ImageExtensions.ToBitmap(ei);

 

}

 

Coordinator
Mar 22, 2011 at 9:46 AM
This is normal, the image is loaded asnychronously in a seperate thread, which means that the callback is as well called in this background thread.

You only have access to the WriteableBitmap in the UI Thread, which means that you have to use the Dispatcher:

void ExtendedImage_LoadingCompleted(object sender, EventArgs e)

{

ExtendedImage ei = (ExtendedImage)sender;

Dispatcher.BeginInvoke(() =>
{

WriteableBitmap wb = ImageTools.ImageExtensions.ToBitmap(ei);
});

}