c# - 如何将二进制数据转换为图像并在图片框中显示它们?
问题描述
我在访问文件中有一个图像,我想使用用户 ID 在另一个 Windows 窗体窗体中检索并显示它。
保存图像的代码:
Dim img = PictureBox1. Image
Dim ms As New System.IO.MemoryStream
img.Save(ms, img.RawFormat)
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()
检索图像的代码:
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim userIdPro = Transfer.userIdPro
Dim query = ("SELECT Image FROM User_info WHERE ID = " & userIdPro & ";")
Dim ds As New DataSet
Dim dr As OleDb.OleDbDataReader
Dim cm = New OleDb.OleDbCommand(query, con)
dr = cm.ExecuteReader
While dr.Read()
Dim MyByte = dr.Item("Value")
Dim fs = New MemoryStream
fs.Write(MyByte, 0, MyByte.Length)
fs.Flush()
Dim MyImg = Image.FromStream(fs, True)
MyImg.Save(dr.Item("ID").ToString & ".JPG", System.Drawing.Imaging.ImageFormat.Jpeg)
PictureBox1.Image = MyImg
fs.Close()
fs = Nothing
End While
con.Close()
End Sub
我无法将二进制数据带入dr
;它总是空的。
解决方案
我把你的代码调用函数 bytesToImage(...)`。您不需要将图像保存在本地,除非您有理由这样做。
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim userIdPro = Transfer.userIdPro
// Don't concatenate your parameters. This is a bad practice and
// exposes your application to SQL injection attacks. Use SQL
// parameters instead.
Dim query = ("SELECT Image FROM User_info WHERE ID = " & userIdPro & ";")
Dim ds As New DataSet
Dim dr As OleDb.OleDbDataReader
Dim cm = New OleDb.OleDbCommand(query, con)
dr = cm.ExecuteReader
While dr.Read()
Dim MyByte = dr.Item("Value")
Dim MyImg As Image
If MyByte IsNot Nothing Then
// You do not need to save it, just convert to an image
// type and set it to your PictureBox1 control.
MyImg = bytesToImage(MyByte)
PictureBox1.Image = MyImg
End If
End While
con.Close()
您的班级应该具有以下Image
属性Byte()
<Table("User_info")>
Public Class User
Public Property Photo As Byte()
End Class
使用以下功能:
Public Function imageToBytes(ByVal imageIn As System.Drawing.Image) As Byte()
Dim ms As MemoryStream = New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Return ms.ToArray()
End Function
Public Function bytesToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As MemoryStream = New MemoryStream(byteArrayIn)
Dim returnImage As Image = Image.FromStream(ms)
Return returnImage
End Function
要将图像保存到数据库中:
Public Sub SaveImage()
Using context = New ProjectDb()
Dim user = New User() With {
.Id = Guid.NewGuid,
.Photo = imageToBytes(PictureBox1.Image)
}
context.Users.Add(user)
context.SaveChanges()
End Using
End Sub
要从数据库中获取图像:
Public Function GetImage(ByVal id As Guid) As Image
Using context = New ProjectDb()
Dim image As Image
Dim user As User = context.Users.FirstOrDefault(Function(x) x.Id = id)
If user IsNot Nothing Then
image = bytesToImage(user.Photo)
PictureBox2.Image = image
End If
End Using
End Function
推荐阅读
- batch-file - 用于将文件从一个位置复制和重命名到另一个位置的批处理脚本
- python - 姜戈;django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host, user, database
- javascript - 我如何从内容中制作“转到”按钮
标签?
- angular - Angular HttpClient.post 正文格式?
- php - 自定义模板使用事件表达插件 Wordpress
- linux-kernel - 写入 UIO 设备
- zcat - Zcat for循环中的多个压缩文件
- c# - C# 按钮执行异步方法。(尝试放入 Web Api (Phillips Hue))
- c# - 如何创建没有交易但将显示先前计数的日期计数
- python - Subplot a groupby 然后 Plot by groupby