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