Tech Study

convert base64 string to byte array c#

Introduction

In this article, I will explain you how to convert base64 string to byte array c#.

1) When the image upload button clicked, that particular image file is read into a Byte Array with the help of BinaryReader class Object.

2) Now that Byte Array is converted into Base64 string using Convert.ToBase64String method.

3) Now to save Base64 encoded string as Image File, Base64 string is converted back to Byte Array with the help of Convert.FromBase64String function.

4) Lastly, the Byte Array save to your folder as a file using WriteAllBytes method.

protected void btnUpload_Click(object sender, EventArgs e)
{
    //Convert Image File to Base64 Encoded string
 
    //Read the uploaded image file using BinaryReader and convert it to Byte Array.

    BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream);
    byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
 
    //Convert the Byte Array to Base64 Encoded string.
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
 
    //Save Base64 Encoded string as Image File
 
    //Convert Base64 Encoded string to Byte Array.
    byte[] imageBytes = Convert.FromBase64String(base64String);
       
    //Save the Byte Array as Image File.
    string filePath = Server.MapPath("~/Images/" + Path.GetFileName(FileUpload1.PostedFile.FileName));
    File.WriteAllBytes(filePath, imageBytes);
}

Here are some c# program for your practice :

How To Convert Celsius To Fahrenheit In C# Program

C# Program to Multiply two Floating Point Numbers

TaggedHow to Convert Base64 string to Byte Array using C#

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more