1

我希望能够将图像保存在 中IsolatedStorage,然后再获取。

为此,我编写了一个助手,我想从我的应用程序的任何地方访问它:

用于创建图像:

public static void SaveImage(Stream image, string name)
{
    try
    {
        IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

        if (!isolatedStorage.DirectoryExists("MyImages"))
        {
            isolatedStorage.CreateDirectory("MyImages");
        }

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

        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isolatedStorage))
        {
            StreamWriter sw = new StreamWriter(fileStream);
            sw.Write(image);
            sw.Close();
        }
    }
    catch (Exception ex)
    {
        throw new Exception("failed");
    }
}

并再次获取该图像:

public static BitmapImage Get(string name)
{
    try
    {
        IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

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

        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, isolatedStorage))
        {
            BitmapImage image = new BitmapImage();
            image.SetSource(fileStream);
            return image;
        }
    }
    catch (Exception ex)
    {
        throw new Exception("failed");
    }
}

当我尝试设置来源时出现image此错误消息:

找不到该组件。(来自 HRESULT 的异常:0x88982F50)

4

1 回答 1

3

假设您拥有正确的能力,WMAppManifest.xml您需要的是:

public static void SaveImage(Stream image, 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("MyImages"))
        {
            myIsolatedStorage.CreateDirectory("MyImages");
        }

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

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

这应该可以工作并将您的图像作为 jpeg 存储到您想要的目录中。如果没有,请确保Path.Combine()正确使用了创建目录和方法。

于 2013-09-09T04:32:03.197 回答