Use C # to encode and decode image files and base64string

Source: Internet
Author: User

Background:

A flashblocker script file was used in the opera forum a few days ago. As the name suggests, the Flash file in the webpage is filtered out. After filtering, an image is displayed in the original position, in the past, the flash filter plug-in used in Firefox was the same, and the displayed images were the same, so I wanted to change it.

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 ......'


(Tom)This is the first time I realized the use of base64. I 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 there is only one. 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.

Wikipedia:Http://zh.wikipedia.org/zh-cn/Base64
Implementation:


In fact, it is very simple. to convert an image file to base64string, you only need a 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 ());

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,

 
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.

 

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.