首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在IsolatedStorage中存储和获取图像?

如何在IsolatedStorage中存储和获取图像?
EN

Stack Overflow用户
提问于 2013-09-07 22:18:49
回答 1查看 2K关注 0票数 1

我希望能够在IsolatedStorage中保存图像,然后得到它。

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

用于创建图像的

代码语言:javascript
复制
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");
    }
}

和再次获得那个图像:

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-09 04:32:03

假设您在WMAppManifest.xml下拥有正确的功能,那么您需要的是:

代码语言:javascript
复制
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存储到所需的目录中。如果没有,请确保create目录和Path.Combine()方法被正确使用。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18678452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档