/// <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