C# Base64 String to PNG Image

Here’s how you can save a base64 encoded string to image file in C#.

public void SaveImageFile(string filename, string base64Image)
{
    string destinationImgPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Constants.UserImagesPath+"\\"+filename+".png";
    try
    {
        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Constants.UserImagesPath);
    }
    catch (IOException iox)
    {
        Console.WriteLine(iox.Message+" "+iox.Data);
    }
    var bytes = Convert.FromBase64String(base64Image);
    using (var imageFile = new FileStream(destinationImgPath, FileMode.Create))
    {
        imageFile.Write(bytes, 0, bytes.Length);
        imageFile.Flush();
    }
}

How to Save a Transparent or a .png image file in Isolated Storage of Windows phone app

This is a small blog post in which I shall explain you how to save a transparent or a .png image file in isolated storage of Windows phone app.

By default you can save a WriteableBitmap object as .jpeg image file in the isolated storage of your application using SaveJpeg method.

But you can easily add an extension to it and provide a .png method of saving the images.

 

You will have to follow certain steps given below to add this extension to your project

 

Step1:
Add two external references (packages) to your project. They are

1) SharpZipLib-WP7

2) WriteableBitmapEx

 


 

If you don’t know how to add a reference to your project you can refer to the following blog post.

http://www.windowsapptutorials.com/windows-phone/how-to-integrate-a-toolkit-or-third-party-sdk-with-windows-phone-app-using-nuget-package-manager-2/

 

Step2:
Add the following class to your project.

PngChunkTypes.cs

 


 

After you have completed the above two steps. You will be provided with some additional methods for the WriteableBitmap object which are Writepng and Setsource. Using these two methods you can save a .png format file in the isolated storage of your Windows phone app.

A small example has been given below

 

 

I hope this post will be helpful to you.