gpt4 book ai didi

mysql - 使用vb检索数据库mysql中的图像

转载 作者:行者123 更新时间:2023-11-29 14:32:17 25 4
gpt4 key购买 nike

我想在vb中不使用id从数据库mysql中检索图像。但为什么我有错误“无法将类型为‘System.Byte[]’的对象转换为类型为‘System.Drawing.Image’。”

这是我的编码。我正在使用 Visual Basic 2008 和数据库 mysql。图片格式为BLOB。例如。[BLOB - 22.1 KiB]

Imports MySql.Data.MySqlClient
Imports System.Drawing.Imaging
Public Class Form_Popup

Private Sub Form_Popup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
objconn = New MySqlConnection("server=localhost;database=new;userid=root;password= 'root'")
objconn.Open()
strsql = "select * from peribadi where Noid =@field1"
command = (New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn))
With command
.Parameters.AddWithValue("@field1", FormRegister.TextBox1.Text)
End With

objdr = command.ExecuteReader
If (objdr.Read()) Then
Label4.Text = (objdr("Name"))
Label5.Text = (objdr("Address"))
PictureBox1.Image = (objdr("picture"))

End If

End Sub

最佳答案

检索您的图像时出现错误。您需要先将图像转换为字节数组。在将其显示到图片框之前..

 Public Function ResizeImageWithAspect(ByVal picImage As Image, ByVal newWidth As Integer) As Bitmap
Dim original As Image = picImage
If Not original Is Nothing Then
//Find the aspect ratio between the height and width.
Dim aspect As Single = CSng(original.Height) / CSng(original.Width)

//Calculate the new height using the aspect ratio
// and the desired new width.
Dim newHeight As Integer = CInt((newWidth * aspect))

//Create a bitmap of the correct size.
Dim temp As New Bitmap(newWidth, newHeight, original.PixelFormat)

//Get a Graphics object from the bitmap.
Dim newImage As Graphics = Graphics.FromImage(temp)

//Draw the image with the new width/height
newImage.DrawImage(original, 0, 0, newWidth, newHeight)

//Dispose of our objects.
Return temp
original.Dispose()
temp.Dispose()
newImage.Dispose()
Else
Return Nothing
End If

End Function

Public Function ByteToImage(ByVal blob() As Byte) As Bitmap
Dim mStream As New System.IO.MemoryStream
Dim pData() As Byte = DirectCast(blob, Byte())
mStream.Write(pData, 0, Convert.ToInt32(pData.Length))
Dim bm As Bitmap = New Bitmap(mStream, False)
mStream.Dispose()
Return bm
End Function

Public Function FileImageToByte(ByVal filePath As String) As Byte()
Dim fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bm() As Byte = br.ReadBytes(fs.Length)
br.Close()
fs.Close()
Dim photo() As Byte = bm
Return photo
End Function

For more info refer to this link

关于mysql - 使用vb检索数据库mysql中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com