在VB.NET中使用LINQ to SQL检索图像 [英] Retrive image using LINQ to SQL in VB.NET
本文介绍了在VB.NET中使用LINQ to SQL检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Dim db As New DataContext("Data Source=.\SQLEXPRESS;Initial Catalog=DBMSTW;Integrated Security=True")
Dim emp As Table(Of Employee) = db.GetTable(Of Employee)()
Dim query = From p In emp _
Where p.emp_id = "E58" Select p
For Each p In query
EmpID.Text = p.emp_id
EmpName.Text = p.emp_name
If (p.gender = "F") Then
Female.Checked = True
Else
Male.Checked = True
End If
EmpAge.Text = p.age
Dim Img As Image
Dim ImgByte As Byte() = Nothing
Dim stream As MemoryStream
ImgByte = CType(p.img_data, Byte())
stream = New MemoryStream(ImgByte, 0, ImgByte.Length)
Img = Image.FromStream(stream)
EmpImg.Image = Img
Next
在上面的代码中,带下划线的部分存在错误.我想将图像检索到图片框EmpImg中.但是我不知道如何将其转换为使用LINQ to SQL.
错误消息为
类型"System.Data.Linq.Binary"的值不能转换为字节的一维数组".
In Above Code There is Error in Underlined Part. I want to retrive image into picturebox EmpImg. But I dont know how to convert it into using LINQ to SQL.
Error Message is
Value of type ''System.Data.Linq.Binary'' cannot be converted to ''1-dimensional array of Byte''.
推荐答案
我在应用程序中有一个类似的用例,它被设置为将二进制图像数据的内容写入用户控件:
I have a similar use case in an application and this is set to write the contents of the binary image data to a user control:
imgBytes = imgRow.ImageData.ToArray
Using mStream As New System.IO.MemoryStream(imgBytes)
Using qImg As System.Drawing.Image = System.Drawing.Image.FromStream(mStream)
Response.ContentType = mimeDict(qImg.RawFormat.Guid).MimeType
Response.BinaryWrite(imgBytes)
End Using
End Using
不过,对于您的情况...只要更改一下就可以摆脱现实
For your situation, though...you may be able to get away with just changing
ImgByte = CType(p.img_data, Byte())
TO
TO
ImgByte = CType(p.img_data.ToArray, Byte())
这篇关于在VB.NET中使用LINQ to SQL检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文