C # BASE64 encoding and picture conversion code

Source: Internet
Author: User

The process of converting a picture to a Base64 string is to first serialize the picture file into binary data using BinaryFormatter, and then use the ToBase64String method of the Convert class. The process of converting a Base64 string to a picture is reversed: Use the frombase64string of the convert class to get the binary data of the picture file, and then use the BinaryFormatter deserialization method.

?
123456789101112131415161718192021222324252627282930 /// <summary>    /// 将图片数据转换为Base64字符串    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void ToBase64(object sender, EventArgs e)    {      Image img = this.pictureBox.Image;      BinaryFormatter binFormatter = new BinaryFormatter();      MemoryStream memStream = new MemoryStream();      binFormatter.Serialize(memStream, img);      byte[] bytes = memStream.GetBuffer();      string base64 = Convert.ToBase64String(bytes);      this.richTextBox.Text = base64;    }    /// <summary>    /// 将Base64字符串转换为图片    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void ToImage(object sender, EventArgs e)    {      string base64 = this.richTextBox.Text;      byte[] bytes = Convert.FromBase64String(base64);      MemoryStream memStream = new MemoryStream(bytes);      BinaryFormatter binFormatter = new BinaryFormatter();      Image img = (Image)binFormatter.Deserialize(memStream);      this.pictureBox.Image = img;    }

C # BASE64 encoding and picture conversion code

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.