Ways to convert an image to base64 and back

  • Thread starter Thread starter James dean
  • Start date Start date
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;
}
 
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
 
Back
Top