将图片的二进制字节 在HTML页面中显示

两种方法:

后端的一般处理程序:Imge.ashx

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI.WebControls;
 6 
 7 namespace Test
 8 {
 9     /// <summary>
10     /// Imge 的摘要说明
11     /// </summary>
12     public class Imge : IHttpHandler
13     {
14 
15         public void ProcessRequest(HttpContext context)
16         {
17 
18             #region 方法一
19             System.IO.MemoryStream ms = new System.IO.MemoryStream();
20             System.IO.Stream str = new FileUpload().PostedFile.InputStream;
21             System.Drawing.Bitmap map = new System.Drawing.Bitmap(str);
22             map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
23             context.Response.ClearContent();
24             context.Response.ContentType = "image/Jpeg";
25             context.Response.BinaryWrite(ms.ToArray());  //将二进制字节输出到页面 
26             #endregion
27 
28             #region 方法二
29             System.IO.FileStream fs = new System.IO.FileStream("Filename", System.IO.FileMode.Open, System.IO.FileAccess.Read);
30             byte[] datas = new byte[fs.Length];
31             fs.Read(datas, 0, Convert.ToInt32(fs.Length));
32             fs.Close();
33             context.Response.OutputStream.Write(datas, 0, Convert.ToInt32(fs.Length));
34             context.Response.End();
35             #endregion
36         }
37 
38         public bool IsReusable
39         {
40             get
41             {
42                 return false;
43             }
44         }
45     }
46 }
View Code

HTML页面代码:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4     <title></title>
5 </head>
6 <body>
7 <img src="/Imge.ashx" />  <!---图片的src指向Imge.ashx 就可以--->
8 </body>
9 </html>

 

posted @ 2016-02-27 22:05  赛跑的蜗牛  阅读(3590)  评论(0编辑  收藏  举报