一般在ASP.NET中要載入圖片,都是要先把圖片存成檔案
然後把Image的Url指向圖片檔案
但是如果圖片的來源是資料庫的二進位值,或是由系統產生無實體圖片就無法這麼做了
解決方法就是使用泛型處理常式(ashx)
然後把程式寫在裡面(程式碼請往下看)
最後把Image的Url位置指向我們新增的ashx檔的位置即可

範例程式:測試程式的專案檔
Default.aspx
'把實體圖片讀成Byte
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim f As System.IO.FileStream
f = System.IO.File.OpenRead(HttpContext.Current
.Server.MapPath("20100309_1.jpg"))
Dim b(f.Length) As Byte
f.Read(b, 0, f.Length - 1)
Session("pic") = b
End Sub
Showlogo.ashx
Imports System.Web.SessionState
Public Class Showlogo
Implements System.Web.IHttpHandler
Implements IRequiresSessionState
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim b() As Byte = context.Session("pic")
context.Response.ContentType = "text/jpg"
context.Response.BinaryWrite(b)
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
參考資料:
在 HttpHandler 中使用 Session 的注意事項
利用 ASP.NET的泛型處理常式(Handler)產生圖片驗證碼,結合IRequiresSessionState將驗證碼儲存在session裡,透過 session值來驗證
