5

我正在使用此代码在相机操作完成时将图像存储到隔离存储中。

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

现在我想从隔离存储中获取图像并将其显示在我的图像控件中。

可能吗?

4

3 回答 3

8

是的。您可以使用此函数从 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;
}

用法:

ImageControl.Source = GetImageFromIsolatedStorage(fnam);
于 2013-06-04T10:16:09.530 回答
2

像这样的东西:

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

在您的代码中

image1.Source = LoadImageFromIsolatedStorage("image.jpg");
于 2013-06-04T10:15:36.353 回答
0

检查这个片段

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

}

于 2014-06-12T12:17:22.613 回答