Home > DeveloperSection > Forums > Saving Bitmap as PNG on WP7
Royce Roy
Royce Roy

Total Post:134

Posted on    April-25-2015 8:13 AM

 Windows Phone C#  Windows Phone 

 1 Reply(s)
 496  View(s)
Rate this:
I'm trying to save a bitmap to my isolated storage as a png file. I found a library on Codeplex called ImageTools which people have been recommending but when i try it and attempt to open the file it says that its corrupt. Any know what i am doing wrong?

private static void SaveImageToIsolatedStorageAsPng(BitmapImage bitmap, string fileName)
    //convert to memory stream
    MemoryStream memoryStream = new MemoryStream();
    WriteableBitmap writableBitmap = new WriteableBitmap(bitmap);
    writableBitmap.SaveJpeg(memoryStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
    //encode memory stream as PNG
    ExtendedImage image = new ExtendedImage();
    PngEncoder encoder = new PngEncoder();
    //Save to IsolatedStorage
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    using (var writeStream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
        encoder.Encode(image, writeStream);

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    April-25-2015 8:24 AM

You're attempting to convert the JPEG memory stream into PNG. That will make it corrupt - you should save the Bitmap directly to PNG.

I haven't tried this particular task with the imagetools library, but if you see John Papa's blog, it looks like you need to call the ToImage extension method on your WriteableBitmap which is provided as part of ImageTools. Then you can use the encoder to take this image and write out to your open stream.

var img = bitmap.ToImage();
var encoder = new PngEncoder();
using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
    encoder.Encode(img, stream);

Don't want to miss updates? Please click the below button!

Follow MindStick