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 38 Comments
Rate it    (Rated 4 by 154 people)
50,321 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

hari On Mar 22, 2010 07:26 PM
i'm need help,i try to convert image o text file but i don't know how to do

Patrickc On Apr 8, 2010 07:51 AM
@Jorge
If your trying to add data uris to your HTML this wont work in IE7 and older, see http://ajaxian.com/a....ded-images-on-ie-too

Carrie On May 4, 2010 01:03 PM
Could anyone tell me which packages should be imported for this program? I'm very new for this area but now a project needs this part immediately.

Thanks!

DeeJay On Jun 3, 2010 10:39 PM
Hi guys... this is a very good discussion about the image conversion.

But I am struggling and trying to convert base64 data into image, without having any information about the type & size of image.
Anyone please help me out with this ASAP in Java.

python On Jun 17, 2010 09:30 AM
Is this possible in Python

rupesh On Jul 16, 2010 12:02 PM
Great the sample example worked for me by doing little of changes.

it solved my purpose.

Thanks Alot

Rupesh Prasad

Anthony On Jul 23, 2010 11:41 AM
Hey, I just want to say that this is some awesome code that's really helped me out in many situations :)

Just for those who hadn't thought of this - You cannot transfer images to and from SOAP WebServices in .NET, so by using Base64 Encode and Decode on each side, you can transfer the Base64 string instead. This applies for all situations where serializability is important, as a string is much easier to serialize than complex types such as an image.

Anyway, thanks for this great code, once again!

Anthony Daly

Leave a Comment

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