2

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.

Kris Selbekk
  • 7,438
  • 7
  • 46
  • 73

1 Answers1

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

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
  • Unfortunately, this doesn't work on WP7 - as the `WriteableBitmap`-class doesn't include the `ToImage()`-method. A solution would be greatly appreciated. – Kris Selbekk Apr 29 '12 at 12:16
  • 1
    @Kris - `ToImage` is an extension method provided by the image tools package. FWIW, I ran this on my Titan before submitting it. – Ritch Melton Apr 29 '12 at 12:50
  • @Kris - It was an interesting problem for me too, I didn't realize how poor WP7's png support is. Let me know if you have any other issues, I'd be curious to look into them. – Ritch Melton Apr 29 '12 at 13:04