I'm trying to load images from the web in my wpf application.
The idea is the following: When I click on a button, a popup with additional information is raised. In this popup I'm using some images from the web.
The problem: When the popup is being loaded the systems hangs while waiting for the images. I'm binding the images from my code behind. The images are stored in an ObservableCollection. I tried using a thread for loading the images but everytime I run into an exception saying the thread is not the owner of the object.
I tried using an Invoke to get the downloaded images to the UserinterfaceThread but I can't reach it. My code is the following:
IList<Image> imagesFromWeb = downloadImagesFromWeb(url);
DispatcherHelper.UIDispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate()
{
foreach (Image img in imagesFromWeb
{
this.ObservableCollection_Images.Add(img);
}
}
As soon as the images are downloaded and it tries to add the images to the (already opened) popup I get the exception saying the thread is not the owner of the object
Can someone please point me into the right direction?
If you have the image available on a public web server which can be adressed using a normal HTTP URI then you can set the source directly to that:
<Image Source="http://www.someserver.com/myimage.png" />
WPF will take care of downloading it - it'll even do it asynchronously I think although I'm not 100% sure.
You can of course do this with databinding as well:
<Image Source="{Binding TheImage}" />
And in the viewmodel
public string TheImage
{
get { return "http://www.someserver.com/myimage.png"; }
}