Last active
May 10, 2018 10:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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