I'm trying to load a BitmapImage in a background thread and then set the (WPF) image source to this BitmapImage.
I'm currently trying something like this:
public void LoadPicture()
{
Uri filePath = new Uri(Directory.GetCurrentDirectory() + "/" + picture.PictureCacheLocation);
if (Visible && !loaded)
{
if (File.Exists(filePath.AbsolutePath) && picture.DownloadComplete)
{
BitmapImage bitmapImage = LoadImage(filePath.AbsolutePath);
image.Dispatcher.Invoke(new Action<BitmapImage>((btm) => image.Source = btm), bitmapImage);
loaded = true;
}
}
}
But I get an InvalidOperationException because the background thread owns the BitmapImage. Is there a way to give the ownership of the BitmapImage or a copy to the UI Thread?
I need to load the bitmap image in the background thread because it may block for a long time.