2013-06-04 23 views
5

Tôi đang sử dụng mã này để lưu trữ hình ảnh vào bộ nhớ cách ly tại thời điểm tác vụ của máy ảnh hoàn tất.Cách tải hình ảnh từ bộ nhớ bị cô lập vào điều khiển hình ảnh trên điện thoại 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(); 
    } 
} 

Bây giờ tôi muốn lấy hình ảnh từ bộ nhớ riêng và hiển thị nó trong điều khiển hình ảnh của tôi.

Có thể không?

Trả lời

2

Something như thế này:

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

Trong mã của bạn

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

Có nó được. Bạn có thể sử dụng chức năng này để tải hình ảnh từ 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; 
} 

Cách sử dụng:

ImageControl.Source = GetImageFromIsolatedStorage(fnam); 
+0

Nó hoạt động tốt. Và tôi di chuyển trang tiếp theo làm một số hoạt động. và trở lại màn hình cũ này. Ở đây hình ảnh được tải không được hiển thị. Và tôi viết cùng một hàm gọi trong sự kiện page_Loaded. Tại sao nó không được nạp lại một lần nữa. sử dụng (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { nếu (isolationStorage.FileExists (fnam)) { Own_Image.Source = GetImageFromIsolatedStorage (fnam); }} – selvam

0

kiểm tra đoạn này

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

}

Các vấn đề liên quan