[英]VB.net - insert/retrieve picture from mysql Database directly to/from a Picturebox
I'm am having a heck of a time finding a code snippet that works for this. 我很想找一个适合这个的代码片段。 I have got to the point where it appears the picture is stored as a blob (perhaps incorrectly) by using this code.
我已经知道,通过使用此代码,图片被存储为斑点(可能不正确)。
Dim filename As String = txtName.Text + ".jpg"
Dim FileSize As UInt32
Dim ImageStream As System.IO.MemoryStream
ImageStream = New System.IO.MemoryStream
PbPicture.Image.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Jpeg)
ReDim rawdata(CInt(ImageStream.Length - 1))
ImageStream.Position = 0
ImageStream.Read(rawdata, 0, CInt(ImageStream.Length))
FileSize = ImageStream.Length
Dim query As String = ("insert into actors (actor_pic, filename, filesize) VALUES (?File, ?FileName, ?FileSize)")
cmd = New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("?FileName", filename)
cmd.Parameters.AddWithValue("?FileSize", FileSize)
cmd.Parameters.AddWithValue("?File", rawData)
cmd.ExecuteNonQuery()
MessageBox.Show("File Inserted into database successfully!", _
"Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
but on retrieving to the picturebox using the following code: 但是使用以下代码检索到图片框时:
Private Sub GetPicture()
'This retrieves the pictures from a mysql DB and buffers the rawdata into a memorystream
Dim FileSize As UInt32
Dim rawData() As Byte
Dim conn As New MySqlConnection(connStr)
conn.Open()
conn.ChangeDatabase("psdb")
Dim cmd As New MySqlCommand("SELECT actor_pic, filesize, filename FROM actors WHERE actor_name = ?autoid", conn)
Cmd.Parameters.AddWithValue("?autoid", Actor1Box.Text)
Reader = cmd.ExecuteReader
Reader.Read()
'data is in memory
FileSize = Reader.GetUInt32(Reader.GetOrdinal("filesize"))
rawData = New Byte(FileSize) {}
'get the bytes and filesize
Reader.GetBytes(Reader.GetOrdinal("actor_pic"), 0, rawData, 0, FileSize)
Dim ad As New System.IO.MemoryStream(100000)
' Dim bm As New Bitmap
ad.Write(rawData, 0, FileSize)
Dim im As Image = Image.FromStream(ad) * "error occurs here" (see below)
Actor1Pic.Image = im
Reader.Close()
conn.Close()
conn.Dispose()
ad.Dispose()
I get the error "parameter not valid" in the area noted. 在指出的区域中出现错误“参数无效”。 FYI If anyone even has some better (working) code examples than this that I can plug in versus debugging this mess that would be great too.
仅供参考,如果有人甚至可以提供一些比我更好的(工作)代码示例,而我却无法调试这些混乱的情况,那就太好了。
Well since getting no help i bashed away at the problem and got it to work finally. 好吧,既然没有帮助,我就解决了这个问题,终于使它起作用了。 Here is my working code.
这是我的工作代码。
SAVE TO MySQL out of Picturebox (pbPicture) 从Picturebox中保存到MySQL(pbPicture)
Dim filename As String = txtName.Text + ".jpg"
Dim FileSize As UInt32
conn.Close()
Dim mstream As New System.IO.MemoryStream()
PbPicture.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
FileSize = mstream.Length
Dim sqlcmd As New MySqlCommand
Dim sql As String
mstream.Close()
sql = "insert into [your table] (picture, filename, filesize)
VALUES(@File, @FileName, @FileSize)"
Try
conn.Open()
With sqlcmd
.CommandText = sql
.Connection = conn
.Parameters.AddWithValue("@FileName", filename)
.Parameters.AddWithValue("@FileSize", FileSize)
.Parameters.AddWithValue("@File", arrImage)
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
LOAD from MySQL db Back to Picturebox 从MySQL db加载回到Picturebox
Dim adapter As New MySqlDataAdapter
adapter.SelectCommand = Cmd
data = New DataTable
adapter = New MySqlDataAdapter("select picture from [yourtable]", conn)
NOTE!! 注意!! can only put once picture in picturebox so obvoiusly this query can only return one record for you
只能在Picturebox中放置一次图片,如此麻烦的查询只能为您返回一条记录
commandbuild = New MySqlCommandBuilder(adapter)
adapter.Fill(data)
Dim lb() As Byte = data.Rows(0).Item("picture")
Dim lstr As New System.IO.MemoryStream(lb)
PbPicture.Image = Image.FromStream(lstr)
PbPicture.SizeMode = PictureBoxSizeMode.StretchImage
lstr.Close()
I just wrote this code as part of my contribution to this forum though not a member but love to help. 我只是写此代码,作为对这个论坛的贡献的一部分,虽然不是成员,但乐于帮助。 This code search for multiple records and their corresponding images as you keep searching for record instead of search for one record and close the app to search again.
当您继续搜索记录而不是搜索一条记录并关闭应用程序以再次搜索时,此代码将搜索多条记录及其对应的图像。 This code allows you to search a record, clear the fields enter the search criteria and search again & again.
此代码使您可以搜索记录,清除字段,输入搜索条件并再次搜索。
If TextBox3.Text = "" Then ' This is the search field to be used it could be any field from your database that will match the value from the database. Either firstname, phone or email etc
MsgBox("Nothing to search for from the database", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Oop!")
End If
Try
conn.Open()
Dim data As New MySqlDataAdapter("SELECT * FROM users_data WHERE phoneno = '" & TextBox3.Text & "' ", conn)
Dim dTable As New DataTable
data.Fill(dTable)
If dTable.Rows.Count > 0 Then
TextBox1.Text = dTable.Rows(0).Item("firstname")
TextBox2.Text = dTable.Rows(0).Item("lastname")
'Fetching the corresponding image to this member
Dim arrImage As Byte()
Dim myMS As New IO.MemoryStream
If Not IsDBNull(dTable.Rows(0).Item("myimage")) Then
arrImage = dTable.Rows(0).Item("myimage")
For Each ar As Byte In arrImage
myMS.WriteByte(ar)
Next
PictureBox1.Image = System.Drawing.Image.FromStream(myMS)
End If
Else
MsgBox("No record found for this Phone No: " & TextBox3.Text & " Enter a valid Phone No or consult the Admin Manager", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Record not found")
clear()
End If
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
Finally
conn.Close()
End Try
This code could work for MYSQL Server database and Microsoft SQL databases as well. 该代码也可以用于MYSQL Server数据库和Microsoft SQL数据库。 The only difference is is changing this statement Dim data As New MySqlDataAdapter which is for MYSQL Server to Dim data As New SqlDataAdapter for Microsoft SQL server.
唯一的不同是将此语句更改为MySQL Server的Dim data as New MySqlDataAdapter更改为Microsoft SQL Server的Dim data as New SqlDataAdapter 。 Good evening all StackOverflowers
晚上好,所有StackOverflowers
VB.NET - insert/retrieve picture from MySQL Database directly to/from a Picturebox VB.NET-从MySQL数据库直接向/从Picturebox插入/检索图片
Example Procedure VISUAL BASIC. 示例过程VISUAL BASIC。 NET 2017: I'm sharing: 1° Configuration of the table MySQL and variables.
NET 2017:我要分享:1°表MySQL和变量的配置。 2° Visual design 3° code.
2°视觉设计3°代码。
Overview of the procedure: The user put an imagen in a picturebox. 过程概述:用户将图像放入图片框。 The imagen located in picturebox is SAVED in MySQL table.
位于图片框中的图像已保存在MySQL表中。 Another option is give the ID of the image (textbox1.text) and then LOAD the image from the MySQL table.
另一个选项是提供图像的ID(textbox1.text),然后从MySQL表中加载图像。 There is another button to clear the picturebox but is not strictly necessary.
还有另一个按钮可以清除图片框,但并非必须如此。 It is not the perfect procedure it has being made only to understand the flow of images load/save to MySQL table AND I SHARE my knoledge with you.
这并不是一个完美的过程,只是为了了解将图像流加载/保存到MySQL表并与您共享我的知识。
"idimagen" as INT. “ idimagen”为INT。 (Primary key)
(首要的关键)
"imagen" as LONGBLOB (allow null) ------->>>> Container of the image. “ imagen”为LONGBLOB(允许为null)------- >>>>图像的容器。
Mysql table configuration example: mysql表配置示例:
Add reference: MySQL.data 6.10.5.0 You need to import: MySql.Data.MySqlClient AND Imports System.IO 添加参考:MySQL.data 6.10.5.0您需要导入:MySql.Data.MySqlClient和Imports System.IO
Public Class Form1
Dim Server As String = "XX.XX.XXX.XXX"
Dim UserID As String = "XXXXXXXXXXXXXX"
Dim Password As String = "PASSWORD"
Dim Database As String = "DATABASE NAME"
Dim Port As Integer = 3306
Dim AllowUserVariables As Boolean = True
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ULTIMOID As Integer = 0
Try
Dim xxxxcon As New MySqlConnectionStringBuilder()
xxxxcon.Database = Database
xxxxcon.Server = Server
xxxxcon.UserID = UserID
xxxxcon.Password = Password
xxxxcon.Port = 3306
xxxxcon.AllowUserVariables = True
Dim con As New MySqlConnection(xxxxcon.ToString)
Dim dbsql As String = "SELECT MAX(idimagen) AS 'ULTIMOID' FROM imagenes;"
Dim cmdMy As New MySqlCommand(dbsql, con)
con.Open()
cmdMy.ExecuteNonQuery()
Try
ULTIMOID = Convert.ToInt32(cmdMy.ExecuteScalar())
Catch ex As Exception
ULTIMOID = 0
End Try
con.Close()
Catch ex As Exception
MsgBox("Problemas leyendo la BASE para obtener el último ID. Error: " & ex.Message)
End Try
Try
Dim xxxxcon As New MySqlConnectionStringBuilder()
xxxxcon.Database = Database
xxxxcon.Server = Server
xxxxcon.UserID = UserID
xxxxcon.Password = Password
xxxxcon.Port = 3306
xxxxcon.AllowUserVariables = True
Dim FileSize As UInt32
Dim con As New MySqlConnection(xxxxcon.ToString)
Dim mstream As New System.IO.MemoryStream()
PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
FileSize = mstream.Length
Dim sqlcmd As New MySqlCommand
Dim sql As String
mstream.Close()
sql = "insert into imagenes (idimagen, imagen) VALUES(@id, @imagen)"
Try
con.Open()
With sqlcmd
.CommandText = sql
.Connection = con
.Parameters.AddWithValue("@id", ULTIMOID + 1)
.Parameters.AddWithValue("@imagen", arrImage)
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
OpenFileDialog2.Filter = "image file (*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png| all files (*.*) | *.* "
If OpenFileDialog2.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog2.FileName)
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
PictureBox1.Image = Nothing
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim xxxxcon As New MySqlConnectionStringBuilder()
xxxxcon.Database = Database
xxxxcon.Server = Server
xxxxcon.UserID = UserID
xxxxcon.Password = Password
xxxxcon.Port = 3306
xxxxcon.AllowUserVariables = True
Dim con As New MySqlConnection(xxxxcon.ToString)
Try
Dim ds As New DataSet
Dim dbsql As String = "SELECT * FROM imagenes WHERE idimagen = " & TextBox1.Text & ";"
Dim cmdMy As New MySqlCommand(dbsql, con)
con.Open()
Dim da As New MySqlDataAdapter(dbsql, con)
da.Fill(ds, "Imagenes")
con.Close()
If ds.Tables("imagenes").Rows.Count > 0 Then
Dim bytes As [Byte]() = ds.Tables("imagenes").Rows(0).Item(1)
Dim ms As New MemoryStream(bytes)
PictureBox1.Image = Image.FromStream(ms)
Else
MsgBox("No record found for this Phone No: " & TextBox1.Text)
End If
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
End Sub
End Class
好吧,您正在尝试在'Image.FromStream'期望System.IO.Stream时强制转换MemoryStream类型
What happens if you switch this: 如果切换此选项,将会发生什么:
Dim ad As New System.IO.MemoryStream(100000)
to: 至:
Dim ad As New System.IO.MemoryStream()
EDIT 编辑
VB array sizes are different than other programming languages, I think you need to do a minus 1: VB数组的大小与其他编程语言不同,我认为您需要做一件事:
rawData = New Byte(FileSize - 1) {}
EDIT 2 编辑2
Alright, lets look over what you have for raw binary data. 好了,让我们看一下原始二进制数据的内容。 All JPGs should start with
FFD8
and end with FFD9
. 所有JPG都应以
FFD8
开头,并以FFD9
。 Insert the following after you set the rawData
array. 设置
rawData
数组后,插入以下内容。 If it throws an error then your JPEG information is corrupt. 如果抛出错误,则说明您的JPEG信息已损坏。
If (rawData(0) = &HFF) AndAlso (rawData(1) = &HD8) Then
Trace.WriteLine("File start OK")
Else
Throw New ApplicationException("Invalid jpg header")
End If
If (rawData(rawData.Length - 2) = &HFF) AndAlso (rawData(rawData.Length - 1) = &HD9) Then
Trace.WriteLine("File end OK")
Else
Throw New ApplicationException("Invalid jpg footer")
End If
EDIT 3 编辑3
We're going to need to see what the first few bytes of the data look like. 我们将需要查看数据的前几个字节是什么样的。 Run this and post what gets outputed:
运行此命令并发布输出内容:
For I = 0 To 20
Trace.Write(Convert.ToString(rawData(I), 16).ToUpperInvariant())
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.