Convert Image to Base64 String and Base64 String to Image

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

109 comment(S)


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

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?

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!

sponsored

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

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

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

Rakesh on Aug 2, 2010 10:04 AM

Thanks dude !!!

arpan on Aug 7, 2010 06:58 PM

hey can u tell me how to compare two images in c#.net ....i want to match two fingerprints

bhagyashree on Aug 12, 2010 05:05 AM

how to convert all images into low format
and then into keys

Asif on Aug 20, 2010 12:07 AM

hi allz....
i want to convert captcha into text
but i found nothing no help can any one give me any suggestion
i tried it alot
thanx

Abid on Aug 25, 2010 10:01 PM

I am searching for a C# code which can convert a scanned pic to the text
If someone helps me i will be thankful ..........

Saurabh on Sep 22, 2010 07:22 PM

I am the recipient of the encoded data and at the source end the scanned image is encoded using the Base64 algorithm. Is there a way to confirm if the data I receive is the encoded form of the image and no other format.
If they encode the text and send it to me then my code fails as I try to save it in TIFF format and transmit it.

Is there any header in the encoded data that tells if the encoded data is was image.

Thanks!

Shiva on Sep 28, 2010 02:23 AM

Hi,

I want to convert an image to base64 string in objective-c. Can any one help.

What I am doing is:

UIImage *pickedImage=[UIImage imageNamed:@"test.jpg"];
NSData *topImageData = UIImageJPEGRepresentation(pickedImage, 1.0);
NSString *myString=[topImageData base64EncodedString];

I am able to get the base64 string. But when I pass it through JSON request to a java server. Server is able to decode. But the image is different.

That means some thing is wrong. My guess is here we are changing UImage to NSData and then converting to base64. At Java end they are directly converting base64 string to image....

Please any one help me in this issue.

Thanks
Shiva

Arabind on Oct 12, 2010 11:45 PM

Thank you soo much...the code was really helpful.

Arabind on Oct 12, 2010 11:46 PM


Buddhi on Nov 24, 2010 11:21 PM

Really helpful... !!!

Buddhi from Sri Lanka...

Das Khatri on Feb 11, 2011 05:10 AM

how to convert base64string to image in j2me .. please any idea to support me!
I'm new to j2me, I'm receiving base64 in output of a request from web-service(C#) , I'm not able to process it. How can I convert this base64 to a image. please

Rob Johnson on Feb 16, 2011 07:54 AM

Thanks, just what I was looking for.

Although you dont need the below line when converting back to an image:

ms.Write(imageBytes, 0, imageBytes.Length);


Alternate implementation:

byte[] imageBytes = Convert.FromBase64String(Card.Base64Photo);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
return Image.FromStream(ms, true);
}

srabasti on Feb 22, 2011 11:56 AM

i want to convert from image to text in java.plz help me.

Chat on Mar 3, 2011 08:06 AM

Really helpful... !!!

suril on Mar 10, 2011 06:02 AM

hi.....d code seems 2 b great..
can nebody help me which imports r to be done...
m nt much thorough with java...learnig it...
thnx people...!!!

mochi on Mar 22, 2011 12:33 AM

thanks, that's Really helpful...

---------------------------------
Pertamina

domy on Mar 28, 2011 04:17 AM

I want to know if is possible the way to convert a base64String to image in android and not image to base64 string.

Thank's

saber on Apr 7, 2011 09:13 PM

Hello,

I have know this method,but It is not fit png image,why? thanks!

Joey Joya on Apr 11, 2011 09:47 PM

Hi Sirs,

how about using visual basic 6.
im trying to encodebase64 and save it to mssql database

Dim strQuery
Dim strData As String
Set pBag = New PropertyBag
pBag.WriteProperty "Picture", frmELLinkage.Image1.Picture
pByteA = pBag.Contents

strData = EncodeBase64(pByteA)

rs.AddNew
rs.Fields("SerialNo").Value = UCase(txtSerial.Text)
rs.Fields("TestDate").Value = cDate(Date)
rs.Fields("TestTime").Value = cDate(Time)
rs.Fields("ImageFileName").Value = UCase(frmELLinkage.Text2.Text)
rs.Fields("Image").Value = strData
rs.Update


please help

zaif on Apr 12, 2011 07:43 PM

i wanted the java code for this,, can u guys help me with this?

glen on Apr 13, 2011 12:46 AM

what package this could be found>?

Sri on Jul 19, 2011 10:35 AM

Hi,
This is code is very helpful. If we have more than one image in a jpg image then how can we recognise the images

ram on Jul 27, 2011 11:08 AM

any one know how to write the same code in JAVA

Bala on Sep 2, 2011 03:39 AM

Thanks a lot!! I was searching this for very long time and your solution worked like a charm!!

George @ Justice Coin on Sep 5, 2011 09:33 PM

I was looking for this method. Just have to figure out how to use the code now.

dp on Sep 7, 2011 05:12 AM

Can Anyone help me to find out the way to convert from Image data type to ASCII..

jmease on Sep 7, 2011 02:43 PM

What about doing this client side in javascript or jquery? Have an offline web app that I need to do this with so server side code is not an option. I can successfully convert the base64string.toDataURL("image/png") and apply it as the image src, but not in all browsers.

JMarco on Sep 12, 2011 07:04 PM

Good Day Sirs,

I was wondering if you know how to transfer the codes into JAVASCRIPT? I need it badly.. Thanks for the helpful idea.

rajkumar on Sep 29, 2011 11:04 AM

pl help me about hw to convert data or text in the image file like .*gif,*.jpeg,*.jpg files

raj on Sep 29, 2011 11:06 AM

pl help me about hw to convert data or text in the image file like .*gif,*.jpeg,*.jpg files

Searock on Sep 30, 2011 10:48 AM

Thanks a lot.

Master coder on Oct 17, 2011 03:20 AM

Great tutorial. Thanks a lot !!

Akshay on Nov 22, 2011 12:28 AM

I want to convert Hindi text to image in J2me and then print it onto the bluetooth printer.
Please help me

Eric on Nov 22, 2011 12:46 PM

Thanks a lot it worked for me

sowmiya on Nov 24, 2011 06:38 AM

Can any body help me..
where to get the file name from the above code,
I am new to .net pls help me on this...

Mariano on Dec 20, 2011 01:05 AM

Thanks!!!! very usefull this article....

Xusan on Mar 31, 2012 02:47 AM

This article helped me.
Thank you.

Iva on Apr 3, 2012 06:20 AM

I have a dynamically created image with src=base64 string. How can I save this image in the database?

SWA on Apr 23, 2012 08:14 PM

hi,
can anyone plz help me to get a code that converts image to base64 strings n vice-verse in java..

pls help out.

SWA on Apr 23, 2012 08:15 PM


KumarAbhishek on Apr 25, 2012 04:06 AM

hello every1
thanks for the code above.i am making a application which will send barcode (png file) thru sms (mms is not a option). so i have to convert the barcode files into base64 encoding cause pic can't be send over sms. so can rewrite the same above code in C# . i am using asp.net. nd one more thing when i will send the base64 encoded to mobile phone (mostly smartphones) how it will covert the base64 data back to picture cause i wanna show the barcode on phone's screen .pls use c# when you show me how.
thnks in adavance.

Philip on Jun 3, 2012 04:47 AM

Hey thanks for that, created a byte version of it rather than base64. Appreciated as you steered me in the right direction :)

mohsinkhan on Jun 10, 2012 10:54 PM

Hello ,
I want to encode the image into base64 for IE browser.
How can i do that.Please help me .
Waiting for reply.
Thanks in advance
Mohsinkhan

pradeep on Jul 13, 2012 02:51 AM


anbu on Jul 25, 2012 05:37 AM

how to call this" public string ImageToBase64(Image image,
System.Drawing.Imaging.ImageFormat format)" function from console application using c#

Jenna on Sep 12, 2012 09:59 PM

Thanks for posting this piece of code .. needed it!

Kedar on Oct 11, 2012 10:30 AM

Thanks for Posting Code

Nagendra on Oct 11, 2012 01:33 PM

Thank you so much for this post

Uvoh Gift on Feb 26, 2013 11:07 AM

i want to compare pictures but i don't know how to do the comparism, pls can anyone help me.

Uvoh Gift on Feb 26, 2013 11:07 AM

i want to compare pictures but i don't know how to do the comparism, pls can anyone help me.

Uvoh Gift on Feb 26, 2013 11:07 AM

i want to compare pictures but i don't know how to do the comparism, pls can anyone help me.

Uvoh Gift on Feb 26, 2013 11:07 AM

i want to compare pictures but i don't know how to do the comparism, pls can anyone help me.

Ripon on Jun 4, 2013 03:21 AM

Thanks for a good and simple solution.

dileep on Jun 9, 2013 11:58 PM

i want to convert string to base64 in powerbuilder.please help on this

bianca on Jul 8, 2013 04:27 PM


bianca on Jul 8, 2013 04:28 PM

Future health software is great software. Check out there website

Zho on Oct 26, 2013 10:51 PM

thank alot its work

Lucky on Nov 25, 2013 11:54 AM

One quick query...I am converting image files to base64string..and size of each base64 string is about 10-15 KB..so in this case, would you recommend using base64 string instead of direct image?

richard on Dec 12, 2013 11:52 PM

I'll try to convert Base64S string to Image but not working.

Can anyone help?.


Dim imgTest As New ServiceReference1.ServiceSoapClient("ServiceSoap")
Dim resp = imgTest.RetrieveImage(txt_image_id.Text)

Dim imageBytes As Byte() = Convert.FromBase64String(resp.Length)
Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)

ms.Write(imageBytes, 0, imageBytes.Length)
PictureBox2.Image = Image.FromStream(ms, True)

richard on Dec 15, 2013 11:40 PM


M on Jan 8, 2014 04:04 AM

In the "Base64ToImage" method you initialise the memory stream on creating it and then you write the same content into the stream again. Why?

manoj on Jan 8, 2014 10:24 PM

Yes,That great...!It help for our purpose...

manoj on Jan 8, 2014 10:25 PM

keep it up man...thank you...

Rula on Jan 14, 2014 02:38 AM

what if i am using sql database to send and retrieve the values , how do i do this?

jay prakash on Jan 30, 2014 05:02 AM

there is some problem at "Image image = Image.FromStream(ms, true);" line i got a problem of "Parameter is not valid".

jay prakash on Jan 30, 2014 05:02 AM

there is some problem at "Image image = Image.FromStream(ms, true);" line i got a problem of "Parameter is not valid".

Srinivas on Feb 11, 2014 08:33 PM

I'm using the below code to convert multipage tiff image to base64 string and when i converted back to image in Java it's not getting converted to multipage tiff image.

But when i use the above online tool it's converting and creating multipage tiff image. Not sure the online tool implemented in different way .

I appreciate if someone could help me on this .



Image multipageTiffImage = null;
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
multipageTiffImage.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
byte[] imageBytes = ms.ToArray();

// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
}

jamescmc on Feb 24, 2014 12:14 AM

This integration guide suggests how to use image converting control to convert images or documents.

http://www.rasteredg....ng/image-converting/

phil on Feb 25, 2014 08:09 AM

awesome! thanks a lot!
very neat code the perfect done method to use in project without the need of adapting anything ..

i used it to send images in a LAN-Chatroom :D
works perfectly.

karthik on Jun 23, 2014 09:57 PM

Code Worked perfect... Thanks a lot...

Oghenez on Jun 27, 2014 08:24 PM

I get an error excerpts

A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.micro....nt/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsof....ription>Unhandled exception</Description><AppDomain>CleanWash.vshost.exe</AppDomain><Exception><ExceptionType>System.Runtime.InteropServices.ExternalException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>A generic error occurred in GDI+.</Message><StackTrace> at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)

Leave a comment