当我尝试使用Emgu将图像捕获到VB.NET中的网络摄像头并使用此代码Form23.PictureBox1.Image = New Bitmap(captureImageBox.Image)
将其复制到另一个FormBox中的另一个PictureBox并尝试将其保存在我的数据库中时,它不会保存并且出现错误,如Empty path name is not legal
,但图片就在那里,并从网络摄像头PictureBox中复制。
这是我将图像保存在数据库中的代码:
Dim a As OpenFileDialog = New OpenFileDialog
SQL = "UPDATE candidate SET photo='" photo=@photo WHERE idn='" & cd & "'"
Dim sqlCommand As New MySqlCommand
sqlCommand.Parameters.Add("@photo", MySqlDbType.LongBlob)
sqlCommand.Parameters("@photo").Value = IO.File.ReadAllBytes(a.FileName)
With sqlCommand
.CommandText = SQL
.Connection = sConnection
.ExecuteNonQuery()
但如果我从OpenFileDialog
保存图像,则会保存。为什么不能呢?嗯
我的代码是否存在将图像从PictureBox复制到另一个的问题?
更新
试过这个,但它也不起作用:
取代了这个:
sqlCommand.Parameters.Add("@photo", MySqlDbType.LongBlob)
sqlCommand.Parameters("@photo").Value = IO.File.ReadAllBytes(a.FileName)
With sqlCommand
.CommandText = SQL
.Connection = sConnection
.ExecuteNonQuery()
这一个:
Dim ms As New IO.MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim data As Byte() = MS.GetBuffer()
Dim p As New MySqlParameter("@photo", MySqlDbType.LongBlob)
p.Value = data
cmd.Parameters.Add(p)
sqlCommand.Parameters.Add("@cd", MySqlDbType.Int32).Value = cd
cmd.ExecuteNonQuery()
答案 0 :(得分:2)
警告:我不知道FileDialog在这一切中扮演什么角色。将图像从PictureBox保存到数据库不涉及文件或FileDialog。这解决了标题问题,而不是文件和对话框蠕动的更新。
首先,考虑不存储图像,但文件名的存档副本的名称。如果要将ID嵌入磁盘文件,可以像<id>_<orgfilename>.<ext>
中那样对这些进行修改。
要保存图片框图像,需要将其转换为字节数组。 Net包含一个类型转换器:
Dim imgData As Byte() ' storage for the img bytes
Dim cvt As New ImageConverter
imgData = CType(cvt.ConvertTo(myImage, GetType(Byte())), Byte())
它(通常)工作,但它也来自Object(CType)来回装箱,并且它选择了ImageFormat,它会选择我的错误。自己动手很简单:
' this is easily used from a class or converted to an extension
Public Shared Function ImgToByteArray(img As Image, imgFormat As ImageFormat) As Byte()
Dim tmpData As Byte()
Using ms As New MemoryStream()
img.Save(ms, imgFormat)
tmpData = ms.ToArray
End Using ' dispose of memstream
Return tmpData
End Function
这几乎是没有装箱的TypeConverter所做的,但代码控制着格式。然后将字节数组保存到db:
Dim imgData As Byte() ' storage for the img bytes
imgData = ImgToByteArray(PictureBox1.Image, ImageFormat.Jpeg)
...
sqlCommand.Parameters("@photo").Value = imgData
如果您想要从文件或图片框中保存更通用的过程,请创建一个接受字节数组作为参数的过程。单击“保存PictureBoxImage”按钮可以将图像转换为字节数组并将其传递,另一个按钮也可以从文件中传递:
Function SaveRecord(ingData As Byte(),...other params...) As Boolean
注意:不要使用GetBuffer。见MSDN MemoryStream.GetBuffer:
请注意,缓冲区包含可能未使用的已分配字节。 例如,如果字符串&#34; test&#34;被写入MemoryStream object,从GetBuffer返回的缓冲区的长度 是256而不是4,未使用252个字节。 ...
在一个968,012字节的文件中,.ToArray
返回正确的大小,GetBuffer
返回1,719,296,其中968012为空后的所有元素。
只需做一点工作就可以创建一类图像助手:
Public Class myImaging
' image to byte array from file name
Public Shared Function ImgToByteArray(imgFile As String,
imgFormat As ImageFormat) As Byte()
...
' the one above
Public Shared Function ImgToByteArray(img As Image,
imgFormat As ImageFormat) As Byte()
...
' going the other way Bytes() --> Image
Public Shared Function ImgFromByteArray(b As Byte()) As Image
...
' shrink img and scale it
Public Shared Function ImgToThumb(img As Image, maxSize As Size) As Image
End Class
然后它只是:imgData = myImaging.ImgToByteArray(picturebox84.Image)
答案 1 :(得分:0)
Dim a As OpenFileDialog = New OpenFileDialog
'...
SQL = "UPDATE candidate SET photo= @photo WHERE idn= @cd"
Dim sqlCommand As New MySqlCommand(SQL, sConnection)
sqlCommand.Parameters.Add("@photo", MySqlDbType.LongBlob).Value = IO.File.ReadAllBytes(a.FileName)
sqlCommand.Parameters.Add("@cd", MySqlDbtype.Int32).Value = cd
sConnection.Open()
sqlCommand.ExecuteNonQuery()
答案 2 :(得分:0)
得到了答案!
Dim cn As New MySqlConnection("server = localhost; user id = root; database = db; password = root")
Dim sqlQuery As String = "UPDATE candidate SET cpos=@cpos, cparty=@cparty, candidacy='Filed', photo=@photo WHERE idn=@cd"
Dim sqlcom As New MySqlCommand(sqlQuery, cn)
Dim ms As New IO.MemoryStream()
PictureBox1.Image.Save(ms, Imaging.ImageFormat.Jpeg)
Dim data As Byte() = ms.GetBuffer()
Dim p As New MySqlParameter("@photo", MySqlDbType.LongBlob)
p.Value = data
sqlcom.Parameters.Add(p)
sqlcom.Parameters.AddWithValue("@cpos", ComboBox1.Text)
sqlcom.Parameters.AddWithValue("@cparty", TextBox1.Text)
sqlcom.Parameters.Add("@cd", MySqlDbType.Int32).Value = cd
cn.Open()
sqlcom.ExecuteNonQuery()