将PNG图像保存到WP7的隔离存储

时间:2012-04-28 00:24:07

标签: c# windows-phone-7 stream bitmapimage isolatedstorage

这里有很多图像到隔离存储的问题,但我找不到适合我的情况的好答案 - 所以我们走了。

我从网上抓取.png图片,并将其保存为BitmapImage - 对象。当它完成加载(在BitmapImage.ImageOpened事件上)时,我想将它保存到独立存储。

那么,如何从这个BitmapImage(或直接来自网络)获取字节或文件流 - 无关紧要,这样我就可以将它写入我的IsolatedStorageFileStream?我在互联网上找不到关于它的一篇文章,这篇文章适用于WP7(所以BitmapImage.StreamSource不可用).png图像。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为你不能开箱即用,但是有一个codeplex / nuget项目可以让你以png格式保存。

假设您安装了codeplex的image tools(通过nuget!)。

_bi = new BitmapImage(new Uri("http://blog.webnames.ca/images/Godzilla.png", UriKind.Absolute));
_bi.ImageOpened += ImageOpened;
...

private void ImageOpened(object sender, RoutedEventArgs e)
{
    var isf = IsolatedStorageFile.GetUserStoreForApplication();

    using (var writer = new StreamWriter(new IsolatedStorageFileStream("godzilla.png", FileMode.Create, FileAccess.Write, isf)))
    {
        var encoder = new PngEncoder();
        var wb = new WriteableBitmap(_bi);
        encoder.Encode(wb.ToImage(), writer.BaseStream);
    }
}

John Pappa有一篇关于这项技术的优秀博客文章。 Saving snapshots to PNG

相关问题