Search 
DailyCoding > General

Convert Image to Base64 String and Base64 String to Image

Learn how to convert Image to Base64 String and Base64 String to Image. (Online Converter Image to Base64)
Author admin on Jun 6, 2008 31 Comments
Rate it    (Rated 3 by 38 people)
33,664 Views

Online Converter Image to Base64

This article will help you to learn how we can convert an image into a base64 string and base64 string back to image.

Image to Base64 String

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;
  }
}

Base64 String to Image

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;
}
C# | Utility

Discussion

bill On Jun 13, 2008 06:27 AM
i was looking for this
thanks

nour On Jul 14, 2008 08:15 AM
I was looking for this method for a while ,so thanks but i still have a problem i couldn't find the suitable java package for "Convert.ToBase64String" when i compile it's underlined as an error

Daily Coder On Jul 23, 2008 02:42 AM
I am not a java guy, but still let me know in case this article helps you out
http://www.wikihow.c....-to-Base64-With-Java

anon On Aug 12, 2008 11:19 AM
this helped me. thanks

mohan On Sep 22, 2008 02:09 AM
pl help me about hw to convert data or text in the image file like .*gif,*.jpeg,*.jpg files

online tv addicted On Oct 21, 2008 01:33 AM
Do you know any online services to encode files to Base64 String?
Without codeing?

Daily Coder On Oct 21, 2008 02:48 AM
> Do you know any online services to encode files to Base64 String?
> Without codeing?

Check out this:
http://www.dailycodi....r/ImageToBase64.aspx

Here you can convert you files and images to base64. Let me know if this doesn't solve you purpose.

vipin ranka On Jan 6, 2009 09:19 AM
Thank you...the code was really helpful...

Visual C# Kicks On Jan 13, 2009 12:06 AM
I agree this is pretty cool

Waqas On Feb 15, 2009 11:25 PM
Thank you so much for all this article!
It worked properly !

Many thanks!

Webshop Winkelen On Apr 8, 2009 03:05 AM
Great article, short but to the point, that's the way we like it!

George On Apr 27, 2009 03:49 AM
How can I do Base64ToImage in VBA for Excel ?
Is there a way to work with Convert.FromBase64String(base64String) in VBA ?

Thanks

Kals On May 12, 2009 11:16 PM
Hey mates can ne1 tell me a trick how to compress a base64string which i gt from an image???

Daily Coder On May 13, 2009 04:41 AM
@Kals, you can directly use .net compression. For ref. see
http://www.codeguru.....et/article.php/c9931


Scott McLellan On Jun 5, 2009 08:12 AM
I'm attempting to split a MIME response on a string boundary, is it possible to split a base64 string using another base64 string?

I've tried the following VB.Net code with no success...

Dim byteArray() as Byte = memoryStream.ToArray()
Dim base64String as String = System.Convert.ToBase64String(byteArray, 0 byteArray.Length)

Dim base64Array() as String = base64String.Split(System.Convert.ToBase64String(System.Convert.FromBase64String(boundary)))

Thanks!

Nipun On Jun 9, 2009 03:12 AM
Is this conversion is for only small size images?

Mohit Gupta On Jun 9, 2009 09:35 PM
Thanx, it's work

Scott McLellan On Jun 10, 2009 10:41 AM
Well the images range in size from 15KB to 300KB or so

Daily Coder On Jun 11, 2009 04:07 AM
The limit is its 512 KB

lea On Jun 24, 2009 06:16 AM
need ur help..i'm trying to convert image to byte array and display it in textbox..but i dont know what command to use..
i'm trying to display number of color such as black will be display "00"..

private byte[] convertPicBoxImageToByte(System.Windows.Forms.PictureBox pictureBox1)
{

System.IO.MemoryStream ms = new System.IO.MemoryStream();

pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

return ms.ToArray();

}

thanx for ur help =)

Karthic On Aug 23, 2009 02:52 AM
Thank you very much this is working nice in vb.net

the vb code is
-----------------------------------------
Dim ms As New MemoryStream
PictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytes() As Byte = ms.ToArray
Dim image_base64String As String = Convert.ToBase64String(bytes)
MsgBox(image_base64String)
-----------------------------------------
Dim rbytes() As Byte = Convert.FromBase64String(image_base64String)
Dim rms As New MemoryStream(rbytes, 0, rbytes.Length)
PictureBox1.Image = Image.FromStream(ms)

Cappie On Sep 30, 2009 02:28 PM
http://www.dailycodi....r/ImageToBase64.aspx

This is awesome, is there link to the reverse, where I can take what converter createdd in text and recreate image file? Without coding??

Raag On Oct 19, 2009 11:12 PM
how it possible in report viewer

Jack On Oct 20, 2009 06:45 PM
Nice, but we should remind in our mind that after the Base64 operation, it will be 30% larger

Raka osiana On Nov 2, 2009 09:21 PM
how about converting jpeg image file to base 16 string and convert base 16 string back to image jpeg....anyone can help me??

Jitu On Nov 13, 2009 04:40 PM
Is it possible in javascript

Jitu On Nov 13, 2009 04:40 PM
Is it possible in javascript

tANIGUCHI On Nov 25, 2009 04:24 AM
Thanks alot! Those 2 func.saved me alot of trouble :)

TiTo On Feb 3, 2010 12:59 PM
very usefull thx, i looking for it o/

par On Feb 15, 2010 03:59 AM
thanks....but how can I do it in powerbuilder..plz help me..convert base64 to jpeg format....

Jorge On Mar 11, 2010 05:07 AM
I'm using this method to create an image from base64 but didn't work in IE6 and IE7, any suggestion? I'm in troubles

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved