Ways to convert an image to base64 and back

J

James dean

The following code is the current method i have for converting an image
to Base64.Is this the most efficient way to convert an image to base64
or are there others. The Image class's Save method is slow here and i am
wondering if i can do it some other way to make it faster.....Any ideas

public string ImageToBase64String(Image imageData ImageFormat format)
{
string base64;
MemoryStream memory = new MemoryStream();
imageData.Save(memory, format);
base64 = System.Convert.ToBase64String(memory.ToArray());
memory.Close();
memory = null;

return base64;
}
 
G

Gabriel Lozano-Morán

Using the memorystream is the fastest way. I saw that there is a little
nifty private field called rawData wich contains a byte[] representation of
the image. If only this field was public you could immediatly call the
System.Convert.ToBase64String() passing this field :(

Gabriel Lozano-Morán
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top