vb检测mysql数据库变化,使用vb在数据库mysql中检索图像

i want to retrieve image from database mysql in vb by using no id. but why i have error "Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'."

this is my codding. i'm using visual basic 2008 and database mysql. picture format BLOB. eg.[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

解决方案

The error is in retrieving your image.

you need to convert the image first to byte array.

before you display it to the picture box..

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值