问题
I have got a Base64 image in an HTML embedded, how can I decode this using C# or VB.net.
回答1:
google.com > base64 image decode c# > http://www.eggheadcafe.com/community/aspnet/2/39033/convert-base64-string-to-image.aspx
Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage(ImageText));
System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData);
Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));
public string FixBase64ForImage(string Image) {
System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image,Image.Length);
sbText.Replace("\r\n", String.Empty); sbText.Replace(" ", String.Empty);
return sbText.ToString();
}
回答2:
Use Convert.FromBase64String to get a byte[]
representing the image binary.
You can then save the resulting byte[]
into a file.
回答3:
Scrape the embedded image into a string. Using WebClient
is probably your best bet. Convert the base64 string to a byte array using Convert.FromBase64String()
. Use a MemoryStream
and Image.FromStream()
to reconstitute an image object.
回答4:
In above example memory stream has not been disposing This may cause memory leak.So Basic Idea is convertion to base64string to bytearray[] to image or bitmap Image creation can be done through memorystream A perfect example for you Try this link http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx
来源:https://stackoverflow.com/questions/5083336/decoding-base64-image