Thanks for the very helpful extension! I've been working with trying to save a WriteableBitmap to a PNG and this was just the thing. I did find what I think is a bug in the implementation. I'm using this code to save png files that are used for Windows Phone Live Tiles. After adding the code from BubbaRichard it was working great, but I found that I was getting OutOfMemoryExceptions when run in the phone memory constrained ScheduledTask environment. In many cases the process is limited to 11MB (WP8). In this case I was seeing much higher memory consumption than expected. I finally was able to live within the memory limits, but only after modifying the WritePNG() function.
I hope this helps save someone else avoid some pain.
Even with this change I had to manually call the GC to live in the small memory footprint allowed.
Thanks again for the code!
public static void WritePNG(this WriteableBitmap bmp, System.IO.Stream destination)
{
Encode(bmp, destination);
}
changed to public static void WritePNG(this WriteableBitmap bmp, System.IO.Stream destination)
{
Encode(bmp, destination);
_image = null;
}
I think that because that variable is static and WriteableBitmap doesn't implement IDisposable, that it doesn't get released and there is no way for the caller to release it. There isn't the same issue with the Stream because the caller can call Dispose() to free it.I hope this helps save someone else avoid some pain.
Even with this change I had to manually call the GC to live in the small memory footprint allowed.
Thanks again for the code!