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> OnePrints the binary byte string of a picture as a picture in an HTML page