How to load an image from isolated storage into image control on windows phone?
Asked Answered
C

3

5

I am using this code for storing the image into isolate storage at the time of camera action completed.

void camera_Completed(object sender, PhotoResult e)
{
    BitmapImage objImage = new BitmapImage();
    //objImage.SetSource(e.ChosenPhoto);
    //Own_Image.Source = objImage;
    using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        fnam = e.OriginalFileName.Substring(93);
        MessageBox.Show(fnam);
        if (isolatedStorage.FileExists(fnam))
            isolatedStorage.DeleteFile(fnam);

        IsolatedStorageFileStream fileStream = isolatedStorage.CreateFile(fnam);
        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(e.ChosenPhoto);

        WriteableBitmap wb = new WriteableBitmap(bitmap);
        wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 100, 100);
        MessageBox.Show("File Created");
        fileStream.Close();
    }
}

Now I want to take the image from isolated storage and display it in my image control.

Is it possible?

Chapland answered 4/6, 2013 at 9:39 Comment(0)
S
8

Yes it is. You can use this function to load image from IsolatedStorage:

private static BitmapImage GetImageFromIsolatedStorage(string imageName)
{
    var bimg = new BitmapImage();
    using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var stream = iso.OpenFile(imageName, FileMode.Open, FileAccess.Read))
        {
            bimg.SetSource(stream);
        }
    }
    return bimg;
}

Usage:

ImageControl.Source = GetImageFromIsolatedStorage(fnam);
Suiter answered 4/6, 2013 at 10:16 Comment(1)
It's working fine. And i move the next page do some operation. and come back to this old screen. Here that loaded image is not displayed. And i write same function call in page_Loaded Event. Why it's not loaded once again. using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (isolatedStorage.FileExists(fnam)) { Own_Image.Source = GetImageFromIsolatedStorage(fnam); }}Chapland
P
2

Something like this:

public BitmapImage LoadImageFromIsolatedStorage(string path) {
  var isf = IsolatedStorageFile.GetUserStoreForApplication();
  using (var fs = isf.OpenFile(path, System.IO.FileMode.Open)) {
    var image = new BitmapImage();
    image.SetSource(fs);
    return image;
  }
}

In your code

image1.Source = LoadImageFromIsolatedStorage("image.jpg");
Pend answered 4/6, 2013 at 10:15 Comment(0)
H
0

check this snippet

public static void SaveImage( string name)

{

var bitmap = new BitmapImage();
bitmap.SetSource(attachmentStream);
var wb = new WriteableBitmap(bitmap);
var temp = new MemoryStream();
wb.SaveJpeg(temp, wb.PixelWidth, wb.PixelHeight, 0, 50);

using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (!myIsolatedStorage.DirectoryExists("foldername"))
    {
        myIsolatedStorage.CreateDirectory("foldername");
    }

    var filePath = Path.Combine("foldername", name + ".jpg");

    using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, myIsolatedStorage))
    {
        fileStream.Write(((MemoryStream)temp).ToArray(), 0, ((MemoryStream)temp).ToArray().Length);
        fileStream.Close();
    }
}

}

Hindi answered 12/6, 2014 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.