0

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>

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.

bhaku
  • 438
  • 4
  • 18

1 Answers1

2

This is a working example on how to do that. The logic is as follow :

  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.

You may want to improve it later to implement MVVM and using DataBinding.

References: 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  
    }
}
har07
  • 88,338
  • 12
  • 84
  • 137
  • I am able to show the image in this way but in offline mode its not working. I want the image once viewed online it should be also able to view it offline – bhaku Jan 04 '14 at 09:45
  • It's work fine here. I don't have offline mode, the way I tested: connect my PC to internet, run apps. Disconnect from internet, rerun apps and the image still there. – har07 Jan 04 '14 at 09:57
  • or in the catch block – bhaku Jan 04 '14 at 10:00
  • You are loading a jpeg image while i am loading a png image. Do i need to change something in the code for that – bhaku Jan 04 '14 at 10:03
  • Not necessarily. Actually it shouldn't go to else or catch block, except something bad happen. Try to debug, put breakpoint in download completed and see if the execution goes to else/catch block.. – har07 Jan 04 '14 at 10:04
  • No, I tried to download exactly the same image as you posted in question. Its fine without conversion whatsoever – har07 Jan 04 '14 at 10:04
  • did u try it by closing the emulator and running the app again without internet connectivity – bhaku Jan 04 '14 at 10:22
  • I am also able to view the image if the emulator is not closed. But once the emulator is closed i am not able to view the image – bhaku Jan 04 '14 at 10:23
  • nope. AFAIK restarting emulator will erase previously saved data from isolated storage (only in emulator, restarting a real WP device doesn't cause the same) – har07 Jan 04 '14 at 10:23