在Windows Phone上幻灯片显示
问题描述:
我有一个网址阵列。每个网址都包含一个图片。我需要逐一下载并开始幻灯片播放。我试图用循环下载每个文件并显示它们。但是每当我试图获得前一张图片时,我什么也得不到。在Windows Phone上幻灯片显示
我的代码如下所示
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--;
}
}
然后我被试图先下载所有的图像,并保存到BitmapImage的阵列,并试图完成下载之后,开始幻灯片放映。这种情况下没有显示任何内容。
的代码是
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);
}
}
然后我试图将图像转换为字节组,并将其保存到一个列表中的名称BMPList。 (List BMPList)。完成后,我想只显示一个黑色的颜色显示为图像的图像下载后
的代码是
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);
}
}
我如何可以下载所有图像,并开始播放幻灯片?
答
应该没有必要自己下载图像,只需将Image
的Source
指向Uri即可。
另请参阅http://blogs.msdn.com/b/swick/archive/2011/04/07/image-tips-for-windows-phone-7.aspx以了解有关避免处理大量基于Web的图像时的内存问题的详细信息。
+0
感谢您的答复。如果只加载一个图像,它可以正常工作。但是当我尝试加载多个项目(使用循环)时,没有任何事情发生。 – 2012-03-08 13:08:37
也请任何人告诉我如何格式化代码。 – 2012-03-08 11:05:17
有关格式化的信息,请参阅http://stackoverflow.com/editing-help – 2012-03-08 11:12:02
选择您的代码,并给ctrl + K,这个元为你:http://meta.stackexchange.com/questions/22186/how-do-我的格式我的代码块 – 2012-03-08 11:13:33