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:

[pt_view id=”2384ae03it”]

Leave a Reply

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