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