How to save an image received from the Internet and display it in Windows Phone 7

I am creating my first application in a Windows Phone 7 application. I have an image that comes from the Internet, and when I click on a click, I go to another page. My xaml code:

 <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>

My .cs code

private void Image_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/AAP.xaml", UriKind.Relative));
    }

Now the problem is that I want to save the image so that it can be viewed even offline. Can someone help me what kind of modification should I do for this purpose.

0
source share
1 answer

This is a working example of how to do this. The logic is as follows:

  • In the page constructor, call the method LoadImage.
  • imageBrush ImageSource , .
  • , LoadImage .
  • (DownloadCompleted) LoadImage. 2 , .

, , MVVM 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 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  
    }
}
+2

Source: https://habr.com/ru/post/1525373/


All Articles