从数据库WPF中检索图像

时间:2018-02-24 04:48:32

标签: wpf vb.net image byte

我正在尝试从.mdb数据库中检索图像并在image控件中显示它。我的代码是:

 Dim cmd As New OleDbCommand("Select * from recents", con)
    Dim table As New DataTable
    Dim adap As New OleDbDataAdapter(cmd)
    adap.Fill(table)
    If table.Rows.Count <= 0 Then
    Else
        For Each row In table.Rows
            Dim recentbtn As New Rctsctt.UserControl1
            Dim data As Byte() = CType(table.Rows(0)(1), Byte())
            Dim strm As MemoryStream = New MemoryStream()
            strm.Write(data, 0, data.Length)
            strm.Position = 0
            Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(strm)
            Dim bi As BitmapImage = New BitmapImage()
            bi.BeginInit()
            Dim ms As MemoryStream = New MemoryStream()
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
            ms.Seek(0, SeekOrigin.Begin)
            bi.StreamSource = ms
            bi.EndInit()
            recentbtn.Image.ImageSource = bi
    Next
    End if

我在行中得到一个`参数无效的异常:

 Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(strm)

为什么会这样?

更新1

我打开了我的.mdb数据库,看到图像/图片保存为Long binary data'..So, i thought if i could convert the binary data to an ImageSource`,它可能有用。所以我试过here

        For Each row In table.Rows
            Dim recentbtn As New Rctsctt.UserControl1
            Dim picsource() As Byte = CType(row("Pic"), Byte())
            Dim bmp As BitmapImage = New BitmapImage
            Dim strm As MemoryStream = New MemoryStream
            Dim offset As Integer = 1
            strm.Write(picsource, offset, picsource.Length - offset)
            bmp.BeginInit()
            bmp.StreamSource = strm
            bmp.EndInit()
            recentbtn.Image.ImageSource = bmp
         Next

但现在我收到一个错误:找不到适合完成此操作的成像组件。

0 个答案:

没有答案