I have an array of urls. Each url contains one image. I need to download them one by one and start a slideshow. I tried to download each file using a loop and display them. But whenever I am trying to get previous image I get nothing.
My code is shown below
string [] urlArray;
int currentItem;
int totalItems;
private void StartSlideShow()
{
for(int i=0;i < totalItems;i++)
{
DownloadImage(urlArray[i]);
}
}
private void DownloadImage(string url)
{
WebClient wc=new WebClient();
wc.OpenReadCompleted+=new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri(url));
}
private void wc_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e)
{
BitmapImage bi=new BitmapImage();
bi.SetSource(e.Result);
imgThumbnail.Source=bi;
}
private void btnNext_Click(object sender, RoutedEventArgs e)
{
if (currentItem < totalItems)
{
DownloadImage(urlArray[currentItem+1]);
currentItem++;
}
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (currentItem > 1)
{
DownloadImage(urlArray[currentItem-1]);
currentItem--;
}
}
Then I was tried to download all the images first and save into an array of BitmapImage and tried to start slideshow after completing the download. Nothing is displayed in this case.
The code is
private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
bi.SetSource(e.Result);
biArr[currentItem].SetSource(e.Result);
if(currentItem==totalItems])
ShowSlides(biArr);
}
private void ShowSlides(BitmapImage[] biArr)
{
for(int i=0;i < totalItems;i++)
{
imgThumbnail.Source=biArr[i];
System.Threading.Thread.Sleep(5000);
}
}
Then I tried to convert the images to byteArray and save it into a list names BMPList. ( List BMPList ). After complete the download when I am trying to display the images only a black color is shown as image
The code is
private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage bi = new BitmapImage();
bi.SetSource(e.Result);
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);
System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, bi.PixelWidth, bi.PixelHeight, 0, 100);
BMPList.Add(ms.ToArray());
}
if(currentItem == totalItems)
ShowSlides(BMPList);
}
private void ShowSlides(List<byte[]> BMPList)
{
for(int i=0; i < BMPList.Count;i++)
{
if (BMPList[currentDisplayItem] != null)
{
MemoryStream ms = new MemoryStream(BMPList[i], 0, BMPList[i].Length);
ms.Write(BMPList[i], 0, BMPList[i].Length);
BitmapImage img = new BitmapImage();
img.SetSource(ms);
imgThumbnail.Source = img;
}
System.Threading.Thread.Sleep(5000);
}
}
How can I download all images and start a slideshow?
There should be no need for you to download the images yourself, just point the
Sourceof theImageto the Uri.See also http://blogs.msdn.com/b/swick/archive/2011/04/07/image-tips-for-windows-phone-7.aspx for details about avoiding memory issues when working with lots of web based images.