Home > Uncategorized > Load Image as Base64 string

Load Image as Base64 string

In JavaScript you can display an image like this <img src=”data:image/jpg;base64,…”> where “…” is the base64 encoded version of the image. Here is some C# Code that can convert an image on-disk to this Base64 string format

     FileInfo fi = new FileInfo(strFileName);
     FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read,FileShare.None);
     byte[] rawData = new byte[fi.Length];
     fs.Read(rawData, 0, (int)fi.Length);
     string strBase64 = Convert.ToBase64String(rawData);
     Response.Write("data:image/jpg;base64," + strBase64);
     fs.Close();
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: