Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Using System. Drawing. Imaging;
Namespace base64_img
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
// Convert the image to base64 encoded text
Private void button#click (object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog ();
Dlg. Title = "select the image to be converted ";
Dlg. filter = "Image files (*. jpg ;*. bmp ;*. gif) | *. jpg *. jpeg ;*. gif ;*. bmp | AllFiles (*. *) | *. *";
If (DialogResult. OK = dlg. ShowDialog ())
{
ImgToBase64String (dlg. FileName );
}
}
// Convert the image to base64 encoded text
Private void ImgToBase64String (string Imagefilename)
{
Try
{
Bitmap bmp = new Bitmap (Imagefilename );
This. pictureBox1.Image = bmp;
FileStream fs = new FileStream (Imagefilename + ". txt", FileMode. Create );
StreamWriter sw = new StreamWriter (fs );
MemoryStream MS = new MemoryStream ();
Bmp. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
Byte [] arr = new byte [ms. Length];
Ms. Position = 0;
Ms. Read (arr, 0, (int) ms. Length );
Ms. Close ();
String strbaser64 = Convert. ToBase64String (arr );
Sw. Write (strbaser64 );
Sw. Close ();
Fs. Close ();
MessageBox. Show ("conversion successful! ");
}
Catch (Exception ex)
{
MessageBox. Show ("ImgToBase64String Conversion failed Exception:" + ex. Message );
}
}
// Convert base64 encoded text into an image
Private void button2_Click (object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog ();
Dlg. Title = "select the base64 encoded text to be converted ";
Dlg. Filter = "txt files | *. txt ";
If (DialogResult. OK = dlg. ShowDialog ())
{
Base64StringToImage (dlg. FileName );
}
}
// Convert base64 encoded text into an image
Private void Base64StringToImage (string txtFileName)
{
Try
{
FileStream ifs = new FileStream (txtFileName, FileMode. Open, FileAccess. Read );
StreamReader sr = new StreamReader (ifs );
String inputStr = sr. ReadToEnd ();
Byte [] arr = Convert. FromBase64String (inputStr );
MemoryStream MS = new MemoryStream (arr );
Bitmap bmp = new Bitmap (MS );
Bmp. Save (txtFileName + ". jpg", System. Drawing. Imaging. ImageFormat. Jpeg );
&