Asp.net 无法在图像控件中显示图像
它是我的处理器(.ashx) 一切正常。只有Asp.net 无法在图像控件中显示图像,asp.net,Asp.net,它是我的处理器(.ashx) 一切正常。只有imageData长度为0,因此图像无法显示 @肖恩:还有。。这里查询字符串正确地获取传递的employeeid 下面是数据库访问的代码: Public Sub bind() Dim ds1 As New DataSet() Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
imageData
长度为0,因此图像无法显示
@肖恩:还有。。这里查询字符串正确地获取传递的employeeid 下面是数据库访问的代码:
Public Sub bind()
Dim ds1 As New DataSet()
Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)
con.Open()
Dim query As String = "select * from EmployeeTable"
Dim cmd As New SqlCommand()
Dim da1 As New SqlDataAdapter(query, con)
da1.Fill(ds1, "EmployeeTable")
GridView1.DataSource = ds1.Tables("EmployeeTable")
GridView1.DataBind()
con.Close()
End Sub
您正在将数据加载到
GridView
中,但没有任何数据加载到imageData
变量中。所以我们只需连接到数据库并将数据取出。我假设您的图像列名为imageData
,但请根据需要进行更改
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
Using con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
Using cmd As New SqlCommand("SELECT imageData FROM EmployeeTable WHERE EmployeeID = @EmployeeID", con) 'select imageData column, change column name as appropriate
cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID)
Try
con.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader()
If rdr.Read() Then
imageData = CType(rdr("imageData"), Byte()) 'convert imageData column from result set to byte array and assign to variable
End If
End Using
Catch ex As Exception
'do any error handling here
End Try
End Using
End Using
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(imageData)
将处理程序中的代码更改为:
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"))
Else
Throw New ArgumentException("No parameter specified")
End If
Dim Image() As Byte = {}
' get the image data from the database using the employeeId Querystring
Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)
Dim cmd As New SqlCommand("SELECT Image FROM EmployeeTable WHERE EmployeeID = @EmployeeID", con)
'select imageData column, change column name as appropriate
cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID)
Try
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Image = CType(rdr("Image"), Byte())
'convert imageData column from result set to byte array and assign to variable
End While
Catch ex As Exception
'do any error handling here
End Try
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(Image)
End Sub
为我工作,可能也会为你工作…这里没有数据库访问代码。。。填充字节数组的方式一定有问题,否则它就没有数据了。。这里查询字符串正确地获取传递的employeeid。。。下面是db access的代码:Ok,这样就不会将任何数据放入
imageData
变量中。您在数据库中存储图像数据的数据类型是什么?图像数据类型。。。(它被存储为二进制数据)你能不能通过这个:不工作。。。图像长度0。。。n我的图像列名是图像。。我将imageData更改为ImageEverywhere…n jst btw,hws gridview与此相关。。我不想在gridview中显示它。。我想在图像控件中显示图像…控件永远不会转到rdr.Read()thnx vvv太多。。。如果你没有忘记将连接对象传递给命令对象,你会为我节省另一半的时间。。。不要介意。。。你帮了大忙!哦,我真的很抱歉,这是新手犯的错误!很抱歉,回复太慢,我昨天一整天都没有上网,但我很高兴你最后把它整理好了=]
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"))
Else
Throw New ArgumentException("No parameter specified")
End If
Dim Image() As Byte = {}
' get the image data from the database using the employeeId Querystring
Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)
Dim cmd As New SqlCommand("SELECT Image FROM EmployeeTable WHERE EmployeeID = @EmployeeID", con)
'select imageData column, change column name as appropriate
cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID)
Try
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
Image = CType(rdr("Image"), Byte())
'convert imageData column from result set to byte array and assign to variable
End While
Catch ex As Exception
'do any error handling here
End Try
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(Image)
End Sub