保存图片流到数据库之后固定显示新法

类别:.NET开发 点击:0 评论:0 推荐:

在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”>
即可

本文地址:http://com.8s8s.com/it/it43380.htm