如何存储从网络接收的图像,并在Windows Phone 7的应用程序中显示它 [英] how to store an image received from the web and display it in the windows phone 7 application
问题描述
我建设我的第一个应用程序在Windows Phone 7的应用程序。我有一个来自于网络,并单击图像当我导航到另一个页面的图像。
我的XAML代码是:
I am building my first app in windows phone 7 application. I have an image which comes from the web and when the image is clicked i am navigated to another page. My xaml code is:
<Button Click="Image_Click" Name="image1" Margin="-33,-16,-26,-13">
<Button.Background>
<ImageBrush Stretch="Fill" ImageSource = "http://political-leader.vzons.com/ArvindKejriwal/images/icons/landing.png"/>
</Button.Background>
</Button>
我的.cs代码是
My .cs code is
private void Image_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/AAP.xaml", UriKind.Relative));
}
现在的问题是,我要存储图像,以便它可以即使观看而脱机。谁能帮我我应该做的修改,以达到这一目的。
Now the issue is that i want to store the image so that it can be even viewed while offline. Can anyone help me what modification should i do to for this purpose.
推荐答案
这是如何做到这一点的工作示例。其逻辑如下:
This is a working example on how to do that. The logic is as follow :
- 在页面的构造函数,调用
的LoadImage
方法。 - 的方法(如果可用)将设置的ImageBrush的ImageSource的图像独立存储。
- 如果图像不存在于独立存储,
的LoadImage
方法从网络下载的图像,并调用事件处理程序时,下载完成。 - 事件处理程序(
DownloadCompleted $那么C $ C>方法),保存图片到独立存储和
再次调用的LoadImage。参照点2为下发生什么。
- In the page's constructor, call
LoadImage
method. - The method will set imageBrush's ImageSource to image in Isolated storage if available.
- If the image not exists in Isolated storage,
LoadImage
method will download the image from web and call an event handler when download completed. - The event handler (
DownloadCompleted
method) will then, save the image to Isolated Storage and callLoadImage
again. Refer to point 2 for what happen next.
您想在以后改进它来实现MVVM,并使用数据绑定。
You may want to improve it later to implement MVVM and using DataBinding.
参考文献:的 nickharris.net ,的 geekchamp.com
string imageName = "myImage.jpg";
string imageUrl = "http://political-leader.vzons.com/ArvindKejriwal/images/icons/landing.png";
public MainPage()
{
InitializeComponent();
LoadImage();
}
private void LoadImage()
{
BitmapImage bi = new BitmapImage();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
//load image from Isolated Storage if it already exist
if (myIsolatedStorage.FileExists(imageName))
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(imageName, FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
imageBrushName.ImageSource = bi;
}
}
//else download image to Isolated Storage
else
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(DownloadCompleted);
wc.OpenReadAsync(new Uri(imageUrl, UriKind.Absolute), wc);
}
}
}
private void DownloadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(imageName);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.Result);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
//after image saved to Iso storage, call LoadImage method again
//so the method will set imageBrush's ImageSource to image in Iso storage
LoadImage();
}
catch (Exception ex)
{
//Exception handle appropriately for your app
}
}
else
{
//Either cancelled or error handle appropriately for your app
}
}
这篇关于如何存储从网络接收的图像,并在Windows Phone 7的应用程序中显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!