2013-06-04 17 views
5

Używam tego kodu do przechowywania obrazu w pamięci izolatu w momencie zakończenia działania kamery.Jak załadować obraz z odizolowanej pamięci do sterowania obrazem w telefonie z systemem Windows?

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(); 
    } 
} 

Teraz chcę pobrać obraz z odizolowanej pamięci i wyświetlić go w moim sterowaniu obrazem.

Czy to możliwe?

Odpowiedz

2

coś takiego:

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

W kodzie

image1.Source = LoadImageFromIsolatedStorage("image.jpg"); 
8

Tak jest. Można użyć tej funkcji, aby załadować obraz z 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; 
} 

Zastosowanie:

ImageControl.Source = GetImageFromIsolatedStorage(fnam); 
+0

Działa poprawnie. I poruszam następną stronę, wykonuję operację. i wróć do tego starego ekranu. Tutaj załadowany obraz nie jest wyświetlany. Piszę tę samą funkcję w zdarzeniu page_Loaded. Dlaczego nie jest ponownie ładowany. using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (isolatedStorage.FileExists (fnam)) { Own_Image.Source = GetImageFromIsolatedStorage (fnam); }} – selvam

0

sprawdzić ten fragment

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(); 
    } 
} 

}

Powiązane problemy