在sqlserver中的圖片類型是image
然後,通過dataset保存到數據庫中,通過showimg.ASPx文件來讀出圖片,即顯示圖片,代碼如下:
Dim image As Byte() = IssueQuestionRow.QuestionImage
'/轉換為支持存儲區為內存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定義並實例化Bitmap對象
Dim bm As New Bitmap(memStream)
'/根據不同的條件進行輸出或者下載;
Response.Clear()
'/如果請求字符串指定下載,就下載該文件;
'/否則,就顯示在浏覽器中。
If Request.QueryString("Download") = "1" Then
Response.Buffer = True
Response.ContentType = "application/octet-stream"
'/這裡下載輸出的文件名字 ok.jpg 為例子,你實際中可以根據情況動態決定。
Response.AddHeader("Content-Disposition", "attachment;filename=ok.jpg")
Else
Response.ContentType = "image/jpg"
End If
Response.BinaryWrite(image)
Response.End()
然後通過需要調用顯示圖片的頁面,加入 <img src=”./showimg.ASPx” wigth=”100px” height=”50”>
來固定圖片的顯示位置、大小等。
當然也可以通過一個頁面的不同參數來獲得不同的圖片,如下代碼:
Showimg.ASPx文件:
Public QuestionID As String
Public ChapterID As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此處放置初始化頁的用戶代碼
If Not IsPostBack Then
QuestionID = Request.QueryString("QID")
ChapterID = Request.QueryString("ChapterID")
Exercise = EXH.GetExercise(ChapterID)
Dim dv As New DataVIEw(Exercise.Ex_IssueQuestion)
dv.RowFilter = "QuestionID='" + QuestionID + "'"
If dv.Count > 0 Then
IssueQuestionRow = dv.Item(0).Row
Dim image As Byte() = IssueQuestionRow.QuestionImage
'/轉換為支持存儲區為內存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定義並實例化Bitmap對象
Dim bm As New Bitmap(memStream)
'/根據不同的條件進行輸出或者下載;
Response.BinaryWrite(image)
End If
End If
End Sub
在其他需要調用的地方的aspx頁面裡只需寫:<img src=”./showimg.ASPx?QuestionID=222&ChapterID=3” wigth=”100px” height=”50”>即可