WPF vb.net没有找到适合完成操作的成像组件 [英] No imaging component suitable to complete the operation was found WPF vb.net
问题描述
我使用来自我的WPF应用程序的代码将图像插入到.mdb数据库中:
I inserted an image into a .mdb database with this code from my WPF app :
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & GetCurrentDirectory() & "\Data\rctts.mdb;Jet OLEDB:Database Password=arn33423342;")
con.Open()
Dim cmd As New OleDbCommand("Insert into recents(Uname,Pic,Email)values(@uname,@pic,@email)", con)
image1.Source = New BitmapImage(New Uri("D:\logo.png"))
Dim buffer As Byte()
Dim bitmap = TryCast(image1.Source, BitmapSource)
Dim encoder = New PngBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(bitmap))
Using stream = New MemoryStream()
encoder.Save(stream)
buffer = stream.ToArray()
End Using
cmd.Parameters.AddWithValue("@pic", buffer)
Pic
列或单元格的数据类型是OLE对象...无论如何,插入数据后,我打开了我的数据库,我看到添加了一条新记录,但 Pic
列的值是长二进制数据
.Anyway,然后我继续在我的wpf应用程序中检索图像。我使用了这段代码?
The Pic
column or cell's data type is OLE Object...Anyway,after inserting the data,i opened my database,i saw that a new record was added but the value of the Pic
column was Long binary data
.Anyway,then i went on retrieving the image in my wpf app.I used this code ?
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
recentbtn.Image.ImageSource = BytesToImage(CType(row(1), Byte()))
recentbtn.Names.Text = table.Rows(0)(0).ToString
AddHandler recentbtn.MouseDown, AddressOf recentbtn_mousedow
RecentsList.stp.Children.Add(recentbtn)
Next
End If
loadingrecents.Visibility = Visibility.Hidden
End Sub
Private Shared Function BytesToImage(ByVal bytes As Byte()) As BitmapImage
Dim bm = New BitmapImage()
Using stream As MemoryStream = New MemoryStream(bytes)
stream.Position = 0
stream.Seek(0, SeekOrigin.Begin)
bm.BeginInit()
bm.StreamSource = stream
bm.CreateOptions = BitmapCreateOptions.PreservePixelFormat
bm.CacheOption = BitmapCacheOption.OnLoad
bm.EndInit()
End Using
Return bm
End Function
但它返回错误:找不到适合完成操作的成像组件
But it's returning an error : No imaging component suitable to complete the operation was found
推荐答案
修正了......对于将来遇到此错误的人:
Fixed it...For anyone who faces this error in future :
- 确保以正确的方式插入数据(有时数据库中的数据损坏会导致此类错误)
2。您不需要做一些繁重的编码就可以将图像转换为字节!
2 . You don't need to do some heavy coding to convert the image to byte!
最后,让我们的代码:
Public Sub read()
con.Open()
Dim cmd As New OleDbCommand("Select * from recents", con)
Dim _dr As OleDbDataReader
_dr = cmd.ExecuteReader
Dim _photo As Byte()
While _dr.Read()
Try
_photo = CType(_dr(1), Byte())
Dim strm As MemoryStream = New MemoryStream(_photo)
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()
image1.Source = bi
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End While
这篇关于WPF vb.net没有找到适合完成操作的成像组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!