简体   繁体   中英

Saving PNG image to Isolated Storage for 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM