Convert Base64 string to Image

public Image Base64StringToImage(string base64String){

//Convert Base64 String to byte[]

byte[] imageBytes = Convert.FromBase64String(base64String);

MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

//Convert byte[] to Image

ms.Write(imagebytes, 0, imageBytes.Length);

Image image = Image.FromStream(ms, true);

return image;



Convert Image to Base64 string

public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format){

using(MemoryStream ms = new MemoryStream()){

//Convert Image to byte[]

image.Save(ms, format);

byte[] imageBytes = ms.ToArray();

//Convert byte[] to Base64 string

string base64String = Convert.ToBase64String(imageBytes);

return base64String;



DateTime from C# to SQL

string tempDate;

tempDate = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[1].Value.ToString()).ToString(“yyyy-MM-dd HH:mm:ss”);

query = “UPDATE…WHERE CheckDate = CONVERT(datetime, ‘” + tempDate + “‘, 121)”;

Convert Base64 String to PDF

byte[] bytes = Convert.FromBase64String(richtextbox1.Text);

System.IO.FileStream stream = new FileStream(@”C:\\…pdf”, FileMode.CreateNew);

System.IO.BinaryWriter writer = new BinaryWriter(stream);

writer.Write(bytes, 0, bytes.Length);


Convert PDF to Base64 String

string pdfPath = @”C:\\…pdf”;

byte[] pdfBytes = File.ReadAllBytes(pdfPath);

string pdfBase64 = Convert.ToBase64String(pdfBytes);

richtextbox1.Text = pdfBase64;