Thoughts on Software

Encoding an image as a Base64 string in C#

with 2 comments

Following on from yesterday’s post, exactly how do you encode a .Net Image object as a Base64 string?

Again, it’s reasonably straightforward: Instantiate a memory stream, save the image to the memory stream, convert the memory stream to a byte array and finally, convert the byte array to a base64 string.

Simples, once again.

Some sample code to play with:

        public string ToBase64(Image image, ImageFormat format)
          using (var 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;

Written by andrewlocatelliwoodcock

April 26, 2011 at 22:25

2 Responses

Subscribe to comments with RSS.

  1. […] to Base64 strings online here or convert them programmatically in C# as described in my next post. Possibly related posts: (automatically generated)Getting the theme icons from SitecoreEmbeding […]

  2. Looks like you are an expert in this field, excellent articles and keep up the great work, my buddy recommended me it.


    May 4, 2011 at 08:30

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: