csharp:Convert Image to Base64 String and Base64 String to Image

8/3/2015来源:C#应用人气:1816

csharp:Convert Image to Base64 String and Base64 String to Image

 /// <summary>        /// 图像转成二进制数组        /// </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>        /// 二进制数据转成图像        /// </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>        /// 图像转成Base64字符        /// </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                string base64String = Convert.ToBase64String(imageBytes);                return base64String;            }        }        /// <summary>        /// Base64字符转成图像        /// </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