Base64 encoding implementation of images and display on Web pages

Source: Internet
Author: User
Tags base64 encode

Generate and parse Base64 encoded pictures
The picture is converted to Base64 string public static string Getimagestr (<span style= "font-family:arial, Helvetica, Sans-serif;" >string imgfile</span><span style= "font-family:arial, Helvetica, Sans-serif;"          >) </span> {//Convert picture file to byte array string and BASE64 encode it inputstream in = null;          byte[] data = null;                      Reads a picture byte array try {in = new FileInputStream (imgfile);              data = new byte[in.available ()];              In.read (data);          In.close ();          } catch (IOException e) {e.printstacktrace ();          }//byte array Base64 encoded Base64encoder encoder = new Base64encoder (); return Encoder.encode (data);//returns BASE64 encoded byte array string} <span style= "White-space:pre" ></span>// Base64 string converted to picture public static Boolean generateimage (String Imgstr,<span style= "font-family:arial, Helvetica, sans- serif; " >string Imgfilepath</span><span StylE= "Font-family:arial, Helvetica, Sans-serif;"          >) </span> {//Base64 decode byte array string and generate picture if (imgstr = = null)//image data is empty return false;          Base64encoder decoder = new Base64encoder ();              try {//base64 decoding byte[] b = Decoder.decode (IMGSTR);                      for (int i=0;i<b.length;++i) {if (b[i]<0) {//Adjust exception data                  b[i]+=256;                  }}//The newly generated picture outputstream out = new FileOutputStream (Imgfilepath);              Out.write (b);              Out.flush ();              Out.close ();          return true;          } catch (Exception e) {return false; }      }

Display on Web page

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>img</title>
<link rel= "stylesheet" type= "Text/css" href= "" >
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<body>

</body>

Base64 encoding implementation of images and display on Web pages

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.