將圖片的二進制字節 在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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM