在图像控件中显示图像(来自数据库)

时间:2013-03-04 09:39:43

标签: asp.net

Triyng基于employeeid在图像控件中检索和显示数据库中的图像... 我已经拿了一个httphandler,我有这个:

  Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    'context.Response.ContentType = "text/plain"
    'context.Response.Write("Hello World!")

    Dim employeeId As Integer
    If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
        employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
    Else
        Throw New ArgumentException("No parameter specified")
    End If
    Dim imageData() As Byte = {}
    ' get the image data from the database using the employeeId Querystring
    context.Response.ContentType = "image/jpeg"
    ' You can retrieve this also from the database
    context.Response.BinaryWrite(imageData)

End Sub

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click
    bind()
    GridView1.Visible = "True"
    ProcessRequest(Context)
End Sub

错误:The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event. 我哪里错了?我需要做出哪些改变?

这是表单上的图像控件:

<asp:Image ID="Image1" runat="server" imageUrl="HttpHandler.ashx?employeeId=5"/>

@Stefano Altieri:

这是在Employee.aspx

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click
    bind()
    GridView1.Visible = "True"
    Image1.ImageUrl = "~/HttpHandler.ashx?EmployeeID='" & EmailIDTextBox.Text & "'"
End Sub

这是在HttpHandler.ashx上

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    'context.Response.ContentType = "text/plain"
    'context.Response.Write("Hello World!")

    Dim employeeId As Integer
    If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
        employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
    Else
        Throw New ArgumentException("No parameter specified")
    End If
    Dim imageData() As Byte = {}
    ' get the image data from the database using the employeeId Querystring
    context.Response.ContentType = "image/jpeg"
    ' You can retrieve this also from the database
    context.Response.BinaryWrite(imageData)

End Sub

2 个答案:

答案 0 :(得分:0)

Generic Handler可以帮助你..

答案 1 :(得分:0)

你的处理程序应该是这样的

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim employeeId As Integer
If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
Dim con As SqlConnection("your connection string")    
Dim cmd As SqlCommand("select image_colum from <table_name> where employeeID = "+employeeID)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim picture As Byte() = dr[0]
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(picture)
End If
End Sub