将 PNG 图像保存到 WP7 的独立存储 [英] Saving PNG image to Isolated Storage for WP7

查看:41
本文介绍了将 PNG 图像保存到 WP7 的独立存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There has been quite a few image-to-isolated-storage questions here, but I couldn't find a good answer for my situation - so here we go.

我正在从网络获取 .png 图像,并将其保存为 BitmapImage 对象.加载完成后(在 BitmapImage.ImageOpened 事件上),我想将其保存到独立存储中.

I am fetching a .png image from the web, and saving it as a BitmapImage-object. When it's done loading (on the BitmapImage.ImageOpened event), I want to save it to isolated storage.

那么,我怎样才能从这个 BitmapImage(或直接从网络 - 无所谓)获取字节或文件流,以便我可以将它写入我的 IsolatedStorageFileStream?我在互联网上找不到一篇关于它的帖子,它适用于 WP7(因此 BitmapImage.StreamSource 不可用)和 .png 图像.任何帮助将不胜感激.

So, how can I get the bytes or file stream from this BitmapImage (or directly from the web - doesn't matter) so that I can write it to my IsolatedStorageFileStream? I can't find a single post about it on the internet that works on WP7 (so BitmapImage.StreamSource is not available) with .png images. Any help would be greatly appreciated.

推荐答案

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

I don't think that you can do this out of the box, but there's a codeplex/nuget project that will allow you to save in png format.

假设您安装了来自 codeplex 的图像工具(通过 nuget!).

Assuming you have the image tools from codeplex installed (via 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 有一篇关于这项技术的优秀博客文章.将快照保存为 PNG

John Pappa has an excellent blog entry on this technique. Saving snapshots to PNG

这篇关于将 PNG 图像保存到 WP7 的独立存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆