How do you convert an image to a base64 string in C#?
For example, I have the path to the image C:/image/1.gif
and would like data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..
returned.
How do you convert an image to a base64 string in C#?
For example, I have the path to the image C:/image/1.gif
and would like data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..
returned.
This is the class I wrote for this purpose:
Something like that
Although the more complicated answer is ok, I find that this is much better
It is simple, you don't need to resave and handles different formats
You can easily pass the path of the image to retrieve the base64 string
Hope this will help.
Get the byte array (
byte[]
) representation of image, then useConvert.ToBase64String()
, st. like this:To convert a base4 image back to a System.Drawing.Image:
Since most of us like oneliners:
If you need it as Base64 byte array: