Saving PNG image to Isolated Storage for WP7

扶醉桌前 提交于 2019-12-12 08:16:24

问题


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.

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.

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.


回答1:


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.

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 has an excellent blog entry on this technique. Saving snapshots to PNG



来源:https://stackoverflow.com/questions/10359448/saving-png-image-to-isolated-storage-for-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!