从字节数组显示jpeg的参数错误

时间:2016-08-05 15:34:31

标签: vb.net image bytearray jpeg bmp

表格中的图像是图像类型IMAGE / PJPEG。

我正在拉BLOB图像,我只需要在图像框中显示它,或者我现在尝试图像列。但是,我认为我的问题是由于图像是JPEG。不知道如何解决这个问题。

    For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1

        Dim MTI_Image As Byte()

        STSQL = "Select IMAGE from mpcs.image_resource WHERE IMAGE_NAME = '01400010016.jpg'"

        rsMPCS = MPCS_SELECT_SQL(UCase(STSQL), rsMPCS)
        rsMPCS.Read()
        MTI_Image = rsMPCS("IMAGE")
        rsMPCS.Close()

        Using memory As MemoryStream = New MemoryStream(MTI_Image)

            'Dim newImage = Image.FromStream(memory) 'didn't work
            Dim newImage As Bitmap = New Bitmap(memory) 'ERROR HERE

            Dim ColImage As New DataGridViewImageColumn
            Dim Img As New DataGridViewImageCell
            DataGridView1.Columns.Add(ColImage)
            ColImage.HeaderText = "Image"
            ColImage.Name = "ColImg"
            Img.Value = newImage 
            DataGridView1.Rows(0).Cells.Add(Img)

        End Using
    Next

这是插入图像的方式。

        Dim temp As String = OpenFileDialog1.FileName
        Dim FileStream As Stream = System.IO.File.OpenRead((temp))
        Dim array() As Byte = File.ReadAllBytes(temp)

        Using memory As MemoryStream = New MemoryStream(array)
            Using reader As BinaryReader = New BinaryReader(memory)

然后用于插入的值为reader.ReadByte

这里有什么东西腐蚀它会阻止我显示它吗?

更新

所以使用图像后我知道有效(试过5我知道有效)。我告诉使用下面的代码没有该行/列的数据。

    For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1

        STSQL = "Select IMAGE from MPCS.IMAGE_RESOURCE WHERE IMAGE_NAME = 'ThermalEtch.JPG'"

        rsMPCS = MPCS_SELECT_SQL(UCase(STSQL), rsMPCS)

        rsMPCS.Read()
        Dim byteImage() As Byte = rsMPCS("IMAGE") 'ERROR HERE.

        Using memory As MemoryStream = New MemoryStream(byteImage)

            Dim newimage As Bitmap = New Bitmap(memory)

            Dim ColImage As New DataGridViewImageColumn
            Dim Img As New DataGridViewImageCell
            DataGridView1.Columns.Add(ColImage)
            ColImage.HeaderText = "Image"
            ColImage.Name = "Image"
            Img.Value = newimage
            DataGridView1.Rows(i).Cells.Add(Img)

        End Using
    Next

这是MPCS_SLECT_SQL

Public Function MPCS_SELECT_SQL(ByVal strSQL As String, Optional ByRef readerObj As OleDbDataReader = Nothing) As OleDbDataReader 'ADODB.Recordset
    Try


        If OPEN_CONNECTIONS() = False Then
            MessageBox.Show("Error connecting to database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            MPCS_SELECT_SQL = Nothing
            Exit Function
        Else


            If Not readerObj Is Nothing Then
                If readerObj.IsClosed - False Then readerObj.Close()
            End If
            Dim cmdMPCS As OleDbCommand = New OleDbCommand(strSQL, conMPCS)

            MPCS_SELECT_SQL = cmdMPCS.ExecuteReader()
            cmdMPCS.Dispose()

        End If

    Catch ex As Exception
        MessageBox.Show(ex.ToString & "     " & strSQL)
        Stop
        Return Nothing
    End Try

End Function

1 个答案:

答案 0 :(得分:0)

要从Oracle读取BLOB数据,您可能需要使用GetBytes和缓冲区来构建MemoryStream。我没有让Oracle进行测试,但是这样的东西适用于SQL Server ......

rsMPCS = MPCS_SELECT_SQL(UCase(STSQL), rsMPCS)
rsMPCS.Read()

Dim bufferSize = 10000
Using ms = New MemoryStream()
    Dim buffer(bufferSize - 1) As Byte
    Dim pos = 0L
    Dim bytesRead = rsMPCS.GetBytes(0, pos, buffer, 0, bufferSize)
    While bytesRead > 0
        ms.Write(buffer, 0, CInt(bytesRead))
        pos += bytesRead
        bytesRead = rsMPCS.GetBytes(0, pos, buffer, 0, bufferSize)
    End While
    Dim newimage = New Bitmap(ms)
    '... add to grid
End Using

我会把它放在一个帮助方法中来清理代码。