我想使用no id从vb中的数据库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
答案 0 :(得分:1)
错误在于检索图像。 您需要先将图像转换为字节数组。 在将它显示到图片框之前..
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
答案 1 :(得分:0)
您可以使用内存流来处理数据库中的字节并将其转换为图像。
使用此代码将字节转换为图像:
Dim bytes() as byte
bytes = (objdr("picture"))
Dim memStream as New MemoryStream(bytes)
PictureBox1.image = Drawing.Image.FromStream(memStream)
答案 2 :(得分:0)
使用VB检索数据库MySQL中的图像。它正在工作..
但是如何使用webservice检索数据库mysql中的图像?
Try
Dim adapter As New MySqlDataAdapter
adapter.SelectCommand = dbcomm
Dim Data = New DataTable
adapter = New MySqlDataAdapter("SELECT image,img_filename FROM image_test WHERE img_id='" + txt_image_id.Text + "'", dbconn)
Dim commandbuild = New MySqlCommandBuilder(adapter)
adapter.Fill(Data)
Dim lb() As Byte = Data.Rows(0).Item("image")
'Dim fl = Data.Rows(0).Item("img_filename")
Dim lstr As New System.IO.MemoryStream(lb)
'txt_RetrieveFilename.Text = fl
PictureBox2.Image = Image.FromStream(lstr)
PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage
lstr.Close()
Catch ex As Exception
MsgBox(ex.ToString())
End Try