Image to Base64 Encoding in C#

Transmitting images through the network or through API’s and Web service in programming is a little bit tricky. And the best way to do it is to convert images into a string or to be significant convert it into ASCII code which is commonly understood by most of the computer system. On this page we are going to use Base64 encoding to convert an image into a string and as well as decoding it.

Note: Base64 encoding is not an encryption it is just done to safely transmit images or any bigbyte data without modification or being misinterpreted with any other computer systems.

Tutorial diagram

  • ImgToBase64() => function use to generate baser64 string from an image (image to base64 Conversion)
  • Base64ToImg()=> function use to convert base64 string to an image object
  • CreateImageObject() => function use to generate image object use for ImgToBase64() for testing

Let’s start by declaring namespaces we need for the conversion:

  •  using System.Drawing; => For creating new Image Object;·
  • using System.IO; => for memory stream, use to store our image in bytes

Image to Base64 Conversion

Base64 String to Image Conversion

Creating an image Object  => For Testing

Thank you for reading!! Happy Coding.


Post you may like:

SOLVED: An unexpected error occurred in the .NET Framework Data Provider for MySQL

If you are here I’m expecting that you encountered same problem as mine. I was transferring a DataSet file to ...
Read More

How to create custom 404 error handler in ASP.NET MVC

Custom 404 error handler - 404 Error refer to a not found resource/URL . Here’s how a default 404 Error ...
Read More

AngularJS CRUD Operation using ASP.NET MVC

I am a .NET Developer and I’m new in AngularJS. I started using AngularJS in about a week ago and ...
Read More
SignalR Chat App With ASP.NET

SignalR Chat Application with ASP.NET MVC

SignalR Chat App With ASP.NET - This article will show you an actual application using SignalR in ASP.NET MVC. The ...
Read More

Submit Forms using AgularJS with ASP.NET MVC

Ways on how to submit forms using AngularJS: I’m new to AngularJS and I was trying to create a CRUD application ...
Read More
Self Signed Certificate

How: Creating Self Signed Certificate

As defined in A self-signed certificate is a certificate that is signed by the person creating it rather than a trusted ...
Read More

AngularJS with Asp.Net MVC

What is Angular? AngularJS is a JavaScript framework that extends HTML attribute to help develop a dynamic Web application. AngularJS ...
Read More

Importing Excel with ASP.NET MVC

Importing excel, In my previous article, I demonstrated how to perform simple way of downloading data into excels. Now, this ...
Read More

Export Data to Excel with ASP.NET MVC

In creating a system there are times that we need to export data to excel, it could either be for ...
Read More

Easy Steps to Implement Partial View in Asp.Net MVC

One of the best ways to implement a dynamic table without loading the whole page is by using Jquery Ajax ...
Read More

Leave a Reply

Your email address will not be published. Required fields are marked *