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.

 

 


How to choose photo from media library or capture a new one in windows phone app

This is the first blog post of this series in which I am going to tell you about how to choose a image from the phone media gallery and How to capture a new image and display it.

The task that we shall be using is PhotoChooserTask. We shall be using the task complete event handler for this purpose.

 

Different steps involved are

 

1. Create a new project called Photochooser.

 


 

2. Firstly I shall demonstrate you how to choose the image from the existing ones that are there in media library of your phone

 

The namespace required for Launchers and Choosers task is

 

 

Select the existing image

Create a button on the MainPage and add the following code given below in the click event handler of the button to launch a Photochooser task.

 

 

The task has got different properties the important ones are the height and width of the image which we want to select.

The event handler which we need to concentrate on is the Photoresult event handler.

Just below the code of button click event handler add the following code of PhotoResult event handler.

 

 

The code given above will set the image source of image control to that of the image chosen from the media library.

 

3. We can also capture a new image by launching CameraCapturetask

.

Capturing image from camera

Create a new button on the MainPage and add the following code for launching the CameraCapturetask in the click event handler of button.

 

 

This task has got no properties. The event handler which we need to concentrate on is same as that of Photochooser which is PhotoResult.

Just below the code of button click event handler add the following code of PhotoResult event handler.

 

 

The code above will set the image source of image control to that of image you have captures from your camera.

 


 

4. If you have any doubts related to this post you can download the full source project file from the link given below.

 
Download full Project Source file Photochooser.zip
 

5. If you want to save the selected or captured image to isolated storage you can refer to the link of following post given below.