Asp. C # encoding and decoding of image files and base64string

Source: Internet
Author: User

Of course the image exists in the js file, so I opened flashblocker. js, browsed it, and found the following sentence:
Var flash = 'data: image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAA ......'
() <SPAN style = "FONT-SIZE: small"> This is the first time I realized that base64 is useful. Remember that when I saved a webpage, I always used to save it. mht format, because this will save the images in the web page, but it has been wondering why only one </SPAN>
View sourceprint? <SPAN style = "FONT-SIZE: small">. the mht file is opened, but there are images. Now, when we know that the image is saved in the mht format, we convert all the images in the webpage into base64 strings. </SPAN>
View sourceprint? <SPAN style = "FONT-SIZE: small"> </SPAN> <SPAN style = "FONT-SIZE: small"> Implementation: </SPAN>
View sourceprint? <SPAN style = "FONT-SIZE: small"> </SPAN>
In fact, it is very simple. to Convert an image file to Base64String, you only need a ToBase64String method in the Convert class.
Copy codeThe Code is as follows:
Image fromImage = Image. FromFile (txtImg. Text );
MemoryStream stream = new MemoryStream ();
FromImage. Save (stream, imgFormat [extension]);
TxtString. Text = Convert. ToBase64String (stream. GetBuffer ());

It seems that the icon file cannot be converted (the result of your own test). If you need to convert the icon, you have to change it. The curve saves the country ~
To extract an image from Base64String, the Convert. FromBase64String () method is required,
Copy codeThe Code is as follows:
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 encountered problems when switching back, and they thought it was a problem when defining stream. When new, they directly gave the converted byte [] to it, there should be no "beyond the expected range" error. Conversion Problems should be caused by the fact that the source image is generated in Java or other languages. Because base64 encoding is the same.
Author: Mr. Yi Xiu
Source: http://1971ruru.cnblogs.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.