从Acess Data Ole对象读取图像

时间:2015-09-14 11:39:11

标签: sql vb.net

我无法使用ole对象从访问文件中读取图像, 以上是我的编码,请帮我解决。

错误消息:错误3“System.Drawing.Image”类型的值无法转换为“String”。

For i As Integer = 1 To Row

    pic(i) = New PictureBox

    strSQL = "SELECT * FROM packageDetail WHERE packageDetailID = ?"
    objcmd = New OleDbCommand(strSQL, dbconn)

    objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@packageID", i))

    reader = objcmd.ExecuteReader()

    reader.Read()

    Dim bytes() As Byte = CType(reader(11), Byte())
    Dim img As Image = Image.FromStream(New System.IO.MemoryStream(bytes))

    With pic(i)
        .Name = "pic(" & i.ToString & ")"
        .Image = Image.FromFile(img)

        .Visible = True
        .Left = 15
        .Width = 201
        .Height = 142
        .Top = 9
    End With
Next

1 个答案:

答案 0 :(得分:0)

在这里,您可以从读取的字节中创建一个图像:

Dim img As Image = Image.FromStream(New System.IO.MemoryStream(bytes))

在这里,您尝试将该图像用作文件名:

.Image = Image.FromFile(img)

ImageFromFile需要文件名(字符串),您正在传递图像。这应该有效:

.Image = img

您应该处理记忆流:

Dim imgData As Byte()
imgData = TryCast(rdr.Item("Image"), Byte())
If imgData IsNot Nothing Then
     Using ms As New MemoryStream(imgData )
        img = CType(Image.FromStream(ms), Bitmap)
    End Using
End If