如何存储从 Web 接收的图像并将其显示在 Windows Phone 7 应用程序中 [英] how to store an image received from the web and display it in the windows phone 7 application

查看:18
本文介绍了如何存储从 Web 接收的图像并将其显示在 Windows Phone 7 应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 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 代码是

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 :

  1. 在页面的构造函数中,调用 LoadImage 方法.
  2. 如果可用,该方法会将 imageBrush 的 ImageSource 设置为隔离存储中的图像.
  3. 如果独立存储中不存在图像,LoadImage 方法将从网络下载图像并在下载完成后调用事件处理程序.
  4. 事件处理程序(DownloadCompleted 方法)然后将图像保存到独立存储并再次调用 LoadImage.关于接下来会发生什么,请参阅第 2 点.
  1. In the page's constructor, call LoadImage method.
  2. The method will set imageBrush's ImageSource to image in Isolated storage if available.
  3. If the image not exists in Isolated storage, LoadImage method will download the image from web and call an event handler when download completed.
  4. The event handler (DownloadCompleted method) will then, save the image to Isolated Storage and call LoadImage 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  
    }
}

这篇关于如何存储从 Web 接收的图像并将其显示在 Windows Phone 7 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆