asp.C image file and base64string encoding and decoding

  • 2020-05-09 18:24:12
  • OfStack

The picture is of course stored in the js file, so I opened flashblocker.js, and then I scanned 1 and found the following sentence:
var flash = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAA......'
(white) < SPAN style="FONT-SIZE: small" > This is the first time that I realized the use of base64. I remember that when saving web pages before, I always used to save them in the format of.mht, because it would save the pictures in the web pages < /SPAN >
view sourceprint? < SPAN style="FONT-SIZE: small" > mht file, opened with images, now know that when saved in mht format all the images in the web page are converted into base64 string and stored inside. < /SPAN >
view sourceprint? < SPAN style="FONT-SIZE: small" > < /SPAN > < SPAN style="FONT-SIZE: small" > Implementation: < /SPAN >
view sourceprint? < SPAN style="FONT-SIZE: small" > < IMG src="http://pic002.cnblogs.com/img/1971ruru/201003/2010031510340776.jpg" > < /SPAN >
In fact, it is very simple. The conversion from image file to Base64String only requires one ToBase64String method in the Convert class
 
Image fromImage = Image.FromFile( txtImg.Text ); 
MemoryStream stream = new MemoryStream(); 
fromImage.Save( stream, imgFormat[extension] ); 
txtString.Text = Convert.ToBase64String( stream.GetBuffer() ); 

Add 1 here, it seems that the icon file cannot be converted (the result of my own experiment), if you need to transfer icon, you need to change 1, so as to save the country
To extract the image from Base64String, you need the Convert.FromBase64String () method,
 
MemoryStream stream = new MemoryStream( Convert.FromBase64String( txtString.Text ) ); 
bitmap img = new Bitmap( stream ); 
img.Save( txtImg.Text ); 
MessageBox.Show( "Completed!" ); 

It seems that some comrades have some problems when switching back, and they feel that the problem should be the definition of stream. When new directly gives the converted byte[] to it, there should be no "beyond the expected range" mistake. The conversion problem should have nothing to do with the original image being generated in Java or another language. Because the code of base64 should be like 1.
Author: Mr. Xiu
Reference: http: / / 1971 ruru cnblogs. com

Related articles: