Skip to content

Instantly share code, notes, and snippets.

@ukcoderj
Last active May 10, 2018 10:00
Show Gist options
  • Save ukcoderj/b065b85971a3e8c7a1f610e283997ba0 to your computer and use it in GitHub Desktop.
Save ukcoderj/b065b85971a3e8c7a1f610e283997ba0 to your computer and use it in GitHub Desktop.
Quick Handling of a downloaded image to save as base 64 string, then return to original form (assumes jpg). Requires system.drawing as well as other standard using statements
public static void GetImage_ConvertItToBase64String_ThenConvertItBack()
{
var webClient = new WebClient();
byte[] imageBytesRaw = webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");
string imageBytesAsBase64String = Convert.ToBase64String(imageBytesRaw);
byte[] imageBytes = Convert.FromBase64String(imageBytesAsBase64String);
//System.Drawing.Bitmap
Bitmap bitmap;
using (Stream bmpStream = new MemoryStream(imageBytes))
{
Image image = Image.FromStream(bmpStream);
bitmap = new Bitmap(image);
}
bitmap.Save(@"C:\google.jpg", ImageFormat.Jpeg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment