Nelson T Joseph
Nelson T Joseph

Reputation: 2763

How to know download image completed in wp7?

Thanks in advance..

I am downloading some images from a server to my wp7 application. For that I am using the following code.

ObservableCollection<BitmapImage> biList;
int currentItem;

private void DownloadImages(string[] imageUriList)
{
   biList = new ObservableCollection<BitmapImage>();
   BitmapImage bi;

   for (int i = 0; i < imageUriList.Length; i++)
   {
      bi = new BitmapImage();
      biList.Add(bi);
      bi.UriSource = new Uri(imageUriList[i], UriKind.Absolute);
      biList[i] = bi;
   }
}

After that I am showing these images one by one in an <Image /> Control in my Windows Phone application.

<Image x:Name="imgImage" />

I am using following code for display images

private void ShowImages()
{
   imgImage.Source = biList[0];
   currentItem = 1;
}

And the images are changed when clicking "next" or "previous" buttons.

private void btnNext_Click(object sender, RoutedEventArgs e)
{
   if(currentItem < biList.Count)
   {
      imgImage.Source = biList[currentItem];
      currentItem += 1;
   }
}

private void btnPrevious_Click(object sender, RoutedEventArgs e)
{
   if(currentItem > 1)
   {
      imgImage.Source = biList[currentItem-2];
      currentItem -= 1;
   }
}

When I am trying to show these images, some images are shown after some time.

How can I ensure that the images are fully downloaded?

Upvotes: 1

Views: 646

Answers (1)

ry8806
ry8806

Reputation: 2328

You can use WebClient to download the image and once it has been successfully downloaded you can add code to the event handler as below:

private void GetImage()
{
    WebClient client = new WebClient();
    client.OpenReadAsync(new Uri("http://website.com/image.jpg"));
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    //Image has been downloaded
    //Do something
}

Upvotes: 3

Related Questions