Convert picture to BASE64 encoding in C #, BASE64 encoding to picture

Source: Internet
Author: User

#region picture to Base64 encoded string
public string imgtobase64string (string imagefilename)
{
Try
{
Bitmap bmp = new Bitmap (imagefilename);

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 ();
return convert.tobase64string (arr);
}
catch (Exception ex)
{
return null;
}
}
#endregion

#region Base64 encoded string to picture
public void Base64stringtoimage (string strbase64)
{

Try
{
byte[] arr = convert.frombase64string (strbase64);
MemoryStream ms = new MemoryStream (arr);
System.Drawing.Image img = System.Drawing.Image.FromStream (ms);


String ls_path = Server.MapPath (@ "images\camera\" + Ls_imagenamecamera + ". jpg");

Img. Save (Ls_path, System.Drawing.Imaging.ImageFormat.Jpeg);

System.Drawing.Image img = System.Drawing.Image.FromStream (ms);
Img. Save ("Imgname.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Img. Save ("Imgname.bmp", imageformat.bmp);
Img. Save ("Imgname.gif", imageformat.gif);
Img. Save (@ "Images\imgname.png", imageformat.png);

session["Imagenamecamera"] = Ls_imagenamecamera;
}
catch (Exception ex)
{

}
}
#endregion

Convert picture to BASE64 encoding in C #, BASE64 encoding to picture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.