Prints the binary byte string of a picture as a picture in an HTML page

Source: Internet
Author: User

The specific implementation code is as follows:

1, a new general processing program: IMAGE.ASHX

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Net;6 usingSystem.Drawing.Imaging;7 usingSystem.IO;8 9 namespaceTestTen { One     /// <summary> A     ///test picture output to HTML page in binary byte (displayed as Picture) -     /// </summary> -      Public classImage:ihttphandler the     { -  -          Public voidProcessRequest (HttpContext context) -         { +             //method One: -             //WebRequest wreq = WebRequest.Create ("Http://img.baidu.com/video/img/video_logo_new.gif"); +             //HttpWebResponse Wresp = (httpwebresponse) wreq. GetResponse (); A             //Stream s = wresp. GetResponseStream (); at             //System.Drawing.Image img; -             //img = System.Drawing.Image.FromStream (s); -             ////Direct Output below -             //MemoryStream ms = new MemoryStream (); -             //img. Dispose (); -  in             //Key Code -             //context. Response.clearcontent (); to             //context. Response.ContentType = "Image/gif"; +             //context. Response.BinaryWrite (Ms. ToArray ()); -  the             //Method Two: *WebClient my =NewWebClient (); $             byte[] mybyte;Panax NotoginsengMyByte = My. Downloaddata ("Http://img.baidu.com/video/img/video_logo_new.gif");  -MemoryStream ms =NewMemoryStream (mybyte); the System.Drawing.Image img; +IMG =System.Drawing.Image.FromStream (MS); A             //Key Code the context. Response.clearcontent (); +Context. Response.ContentType ="Image/gif"; - context. Response.BinaryWrite (MyByte);  $         } $  -          Public BOOLisreusable -         { the             Get -             {Wuyi                 return false; the             } -         } Wu     } -}

2. Create a new HTML page: imagetest.htm

1<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">2"http://www.w3.org/1999/xhtml">34<title></title>56<body>7Use: Response.BinaryWrite binary string output Picture: <br/>8Main: Image img tag path (SRC) to point to that (image.ashx) generic handler <br/>9"image.ashx"/>Ten</body> One

Prints the binary byte string of a picture as a picture in an HTML page

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.