保存圖片流到數(shù)據(jù)庫之后固定顯示新法
發(fā)表時(shí)間:2024-01-24 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在sqlserver中的圖片類型是image然后,通過dataset保存到數(shù)據(jù)庫中,通過showimg.aspx文件來讀出圖片,即顯示圖片,代碼如下: Dim image As Byte() = IssueQuestionRow.QuestionImage ...
在sqlserver中的圖片類型是image
然后,通過dataset保存到數(shù)據(jù)庫中,通過showimg.aspx文件來讀出圖片,即顯示圖片,代碼如下:
Dim image As Byte() = IssueQuestionRow.QuestionImage
'/轉(zhuǎn)換為支持存儲(chǔ)區(qū)為內(nèi)存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定義并實(shí)例化Bitmap對(duì)象
Dim bm As New Bitmap(memStream)
'/根據(jù)不同的條件進(jìn)行輸出或者下載;
Response.Clear()
'/如果請(qǐng)求字符串指定下載,就下載該文件;
'/否則,就顯示在瀏覽器中。
If Request.QueryString("Download") = "1" Then
Response.Buffer = True
Response.ContentType = "application/octet-stream"
'/這里下載輸出的文件名字 ok.jpg 為例子,你實(shí)際中可以根據(jù)情況動(dòng)態(tài)決定。
Response.AddHeader("Content-Disposition", "attachment;filename=ok.jpg")
Else
Response.ContentType = "image/jpg"
End If
Response.BinaryWrite(image)
Response.End()
然后通過需要調(diào)用顯示圖片的頁面,加入 <img src=”./showimg.aspx” wigth=”100px” height=”50”>
來固定圖片的顯示位置、大小等。
當(dāng)然也可以通過一個(gè)頁面的不同參數(shù)來獲得不同的圖片,如下代碼:
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
'/轉(zhuǎn)換為支持存儲(chǔ)區(qū)為內(nèi)存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定義并實(shí)例化Bitmap對(duì)象
Dim bm As New Bitmap(memStream)
'/根據(jù)不同的條件進(jìn)行輸出或者下載;
Response.BinaryWrite(image)
End If
End If
End Sub
在其他需要調(diào)用的地方的aspx頁面里只需寫:<img src=”./showimg.aspx?QuestionID=222&ChapterID=3” wigth=”100px” height=”50”>即可