Csharp: Convert Image to Base64 String and Base64 String to Image

Source: Internet
Author: User

/// <Summary> /// convert the image to a binary array /// </summary> /// <param name = "imageIn"> </param> /// <returns> </returns> public byte [] imageToByteArray (System. drawing. image imageIn) {MemoryStream MS = new MemoryStream (); imageIn. save (MS, System. drawing. imaging. imageFormat. gif); return ms. toArray ();} /// <summary> /// convert binary data to an image /// </summary> /// <param name = "byteArrayIn"> </param> /// <returns> </returns> public System. drawing. image byteArrayToImage (byte [] byteArrayIn) {MemoryStream MS = new MemoryStream (byteArrayIn); System. drawing. image returnImage = System. drawing. image. fromStream (MS); return returnImage ;} /// <summary> /// convert the image to Base64 characters /// </summary> /// <param name = "image"> </param> // <param name = "format"> </param> // <returns> </returns> 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 base64String = Convert. toBase64String (imageBytes); return base64String ;}} /// <summary> /// convert Base64 to an image /// </summary> /// <param name = "base64String"> </param> /// <returns> </returns> public Image Base64ToImage (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 ;}

Http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx

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.