0

EMPPhotos在 SQL Server 2008 R2 上的表中以image数据类型存储了员工图像。我创建了一个通用处理程序来从表中获取图像并将其发送到页面。这没用。我已经测试了查询本身,并且正在获取数据。

处理程序:

<%@ WebHandler Language="VB" Class="EmpImageHandler" %>

Imports System
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Public Class EmpImageHandler : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        'context.Response.ContentType = "text/bmp"
        'context.Response.Write("Hello World")

        context.Response.ContentType = "text/bmp"
        Dim img As Image = GetImage(context.Request.QueryString("id"))
        img.Save(context.Response.OutputStream, ImageFormat.Bmp)
    End Sub

    Private Function GetImage(inID As Long) As Image
        Dim ms As MemoryStream = New MemoryStream

        Dim cnSTR As New clsConnections
        Dim cn As New SqlConnection(cnSTR.ConnectToDB("AgencyStaff"))
        Try
            cn.Open()
        Catch ex As Exception

        End Try
        Dim ssql As String = "Select BMPPhoto From EMPPhotos where empid = " & inID
        Dim CMD As SqlCommand = New SqlCommand(ssql, cn)
        Dim dr As SqlDataReader = CMD.ExecuteReader
        dr.Read()
        Dim img() As Byte = CType(dr("BMPPhoto"), Byte())
        ms = New MemoryStream(img, False)
        Return Image.FromStream(ms)
    End Function

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

End Class

提前感谢你的帮助。

4

1 回答 1

2

改变你的ContentType.

从:

context.Response.ContentType = "text/bmp"

至:

context.Response.ContentType = "image/bmp"
于 2013-08-30T15:40:30.243 回答