How to save picturebox1 image to mysql database? [duplicate] - mysql

Hi I posted this earlier and got some help but still no working solution. I have determined thanks to the last q & a that there is something wrong with my "save to db" code as well as my "retrieve to picture" code. Even If I manually save the pic in the db it stil wont retreive. This is code i patched together from 3 or 4 examples around the net. Ideally if someone had some known good code and could direct me to it that would be the best.
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)
![enter image description here][1]
'*****retieving 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()

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)
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
Dim adapter As New MySqlDataAdapter
adapter.SelectCommand = Cmd
data = New DataTable
adapter = New MySqlDataAdapter("select picture from [yourtable]", conn)
NOTE!! can only put one picture in picturebox so obviously this query can only return one record for you
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()

The accepted and upvoted answer may work, but it is suboptimal and quite wasteful:
If the image to save is on disk, there is no reason to use a UI control and a MemoryStream to get the image into a byte array.
The code also appears to reuse a single global connection object; these should be created and disposed of rather than reused.
Consider saving just the file name to the database, perhaps hashed, with the image saved to a special folder. Saving image data bloats the DB and takes longer to convert.
Finally, .GetBuffer() is quite incorrect:
The memstream buffer will often include unused, allocated bytes. With a 25k test file, ToArray() returns 25434 bytes - the correct size of the image - while GetBuffer() returns 44416. The larger the image, the more empty bytes there will be.
This uses MySQL provider objects since it is so-tagged, but the data provider (MySQL, SQLServer, OleDB etc) used doesnt matter: they all work the same.
In cases where the image source is a PictureBox, use a MemoryStream:
Dim picBytes As Byte()
Using ms As New MemoryStream()
picBox1.Image.Save(ms, imgFormat)
picBytes = ms.ToArray() ' NOT GetBuffer!
End Using
Since the image had to come from somewhere, if it is a file, this is all you need:
picBytes = File.ReadAllBytes(filename)
Once you have the image as bytes, to save:
Dim SQL = "INSERT INTO <YOUR TBL NAME> (picture, filename, filesize) VALUES(#Pic, #FileName, #FileSize)"
Using conn As New MySqlConnection(connstr)
Using cmd As New MySqlCommand(SQL, conn)
conn.Open()
cmd.Parameters.Add("#Pic", MySqlDbType.Blob).Value = picBytes
cmd.Parameters.Add("#FileName", MySqlDbType.String).Value = filename
cmd.Parameters.Add("#FileSize", MySqlDbType.Int32).Value = FileSize
cmd.ExecuteNonQuery()
End Using
End Using ' close and dispose of Connection and Command objects
Loading Image from DB
Dim imgData As Byte()
'... open connection, set params etc
Using rdr As MySqlDataReader = cmd.ExecuteReader
If rdr.HasRows Then
rdr.Read()
imgData = TryCast(rdr.Item("Image"), Byte())
' in case this record has no image
If imgData IsNot Nothing Then
' ToDo: dispose of any previous Image
' create memstream from bytes
Using ms As New MemoryStream(imgData)
' create image from stream, assign to PicBox
picBox1.Image = CType(Image.FromStream(ms), Image)
End Using
End If
End If
End Using
Note that Bitmaps and Images must be disposed of. If you repeatedly create new images as the user browses the database your app will leak and eventually crash. If you convert back and forth a lot, you can write a helper or extension method to convert images to bytes and vice versa.
DBConnection and DBCommand objects also need to be disposed of. The Using block does this for us.
References, Resources:
Reuse or Dispose DB Connections? sample GetConnection helper
Connection Pooling
MemoryStream.GetBuffer()
Can we stop using AddWithValue()

Tested Code for Store and Retrieve Images using MySQL and VB.NET
Public Class FMImage
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
Dim filename As String = TextBox1.Text
Dim FileSize As UInt32
Dim Conn As MySql.Data.MySqlClient.MySqlConnection
Conn = New MySql.Data.MySqlClient.MySqlConnection
Try
If Conn.State = ConnectionState.Open Then Conn.Close()
Conn.ConnectionString = MySQLConnectionString
Conn.Open()
Catch ex As Exception
MessageBox.Show(ex.ToString, "Connect")
End Try
Dim mstream As System.IO.MemoryStream = ConvertImageFiletoMemoryStream(filename)
PbPicture.Image.Save(mstream, Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = ConvertImageFiletoBytes(filename)
FileSize = mstream.Length
Dim sqlcmd As New MySql.Data.MySqlClient.MySqlCommand
Dim sql As String
mstream.Close()
'CREATE TABLE `actors` ( `actor_pic` longblob,`filesize` bigint(20) default NULL,`filename` varchar(150) default NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
sql = "insert into actors (actor_pic, filesize, filename) VALUES(#File, #FileName, #FileSize)"
Try
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)
End Try
Dim adapter As New MySql.Data.MySqlClient.MySqlDataAdapter
adapter.SelectCommand = New MySql.Data.MySqlClient.MySqlCommand("SELECT actor_pic, filesize, filename FROM actors", Conn)
Dim Data As New DataTable
'adapter = New MySql.Data.MySqlClient.MySqlDataAdapter("select picture from [yourtable]", Conn)
Dim commandbuild As New MySql.Data.MySqlClient.MySqlCommandBuilder(adapter)
adapter.Fill(Data)
MsgBox(Data.Rows.Count)
Dim lb() As Byte = Data.Rows(Data.Rows.Count - 1).Item("actor_pic")
Dim lstr As New System.IO.MemoryStream(lb)
PbPicture.Image = Image.FromStream(lstr)
PbPicture.SizeMode = PictureBoxSizeMode.StretchImage
lstr.Close()
End If
End Sub
Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
Dim _tempByte() As Byte = Nothing
If String.IsNullOrEmpty(ImageFilePath) = True Then
Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
Return Nothing
End If
Try
Dim _fileInfo As New IO.FileInfo(ImageFilePath)
Dim _NumBytes As Long = _fileInfo.Length
Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
Dim _BinaryReader As New IO.BinaryReader(_FStream)
_tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
_fileInfo = Nothing
_NumBytes = 0
_FStream.Close()
_FStream.Dispose()
_BinaryReader.Close()
Return _tempByte
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function ConvertBytesToMemoryStream(ByVal ImageData As Byte()) As IO.MemoryStream
Try
If IsNothing(ImageData) = True Then
Return Nothing
'Throw New ArgumentNullException("Image Binary Data Cannot be Null or Empty", "ImageData")
End If
Return New System.IO.MemoryStream(ImageData)
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function ConvertImageFiletoMemoryStream(ByVal ImageFilePath As String) As IO.MemoryStream
If String.IsNullOrEmpty(ImageFilePath) = True Then
Return Nothing
' Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
End If
Return ConvertBytesToMemoryStream(ConvertImageFiletoBytes(ImageFilePath))
End Function
End Class

I am having problem using mr #dMo 's program its showing me an error "Column 'picture' cannot be null"
here is my code.
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim filename As String = TextBoxSave.Text + ".jpg"
Dim FileSize As UInt32
conn.Close()
Dim mstream As New System.IO.MemoryStream()
PictureBoxSave.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 employeedetails (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
End Sub
P.S. Im sorry for posting this adnand answer I have no enough reputation to comment to this post

The code below inserts a record of vehicle information in a table. The Image of the car selected is converted into memory stream and saved to the Database as Varbinary.
A function is used to convert the image to memory stream.
' Upload new vehicle image
Private Sub BtnUpload_Click(sender As Object, e As EventArgs) Handles
BtnUpload.Click
Dim imgBinary As Byte()
With Me.OpenFileDialog1
.FileName = ""
.Filter = "Image Files(*.BMP;*.JPG;*.JEPG;*.GIF)|*.BMP;*.JPG;*.JEPG;*.GIF|All files (*.*)|*.*"
.RestoreDirectory = True
.ValidateNames = True
.CheckFileExists = True
If .ShowDialog = Windows.Forms.DialogResult.OK Then
'Me.PicImage.Image.Dispose()
Me.PicImage.Image = System.Drawing.Image.FromFile(.FileName)
End If
End With
imgBinary = ConvertImage(PicImage.Image)
Dim command As New SqlCommand("insert into MyCars(CarId, Manufacture, CarModel, CarClass, CarImage) values(#CarId, #Manufacture, #CarModel, #CarClass, #CarImage)", connection)
command.Parameters.Add("#CarId", SqlDbType.VarChar).Value = CInt(TxtID.Text)
command.Parameters.Add("#Manufacture", SqlDbType.VarChar).Value = TxtManufacturer.Text
command.Parameters.Add("#CarModel", SqlDbType.VarChar).Value = TxtModel.Text
command.Parameters.Add("#CarClass", SqlDbType.VarChar).Value = TxtClass.Text
command.Parameters.Add("#CarImage", SqlDbType.VarBinary).Value = imgBinary
Try
connection.Open()
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Car # " & TxtID.Text & " successfully added to database.")
Else
MessageBox.Show("Car not added!")
End If
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
connection.Close()
End Try
End Sub
The following function converts the image into memory stream.
' Convert Image from Memory Stream
Public Function ConvertImage(ByVal myImage As Image) As Byte()
Dim mstream As New MemoryStream
myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim myBytes(mstream.Length - 1) As Byte
mstream.Position = 0
mstream.Read(myBytes, 0, mstream.Length)
Return myBytes
End Function
The code below is used to display the image from the database. Use the ID (as an integer) to display the image.
Private Sub BtnShowImg_Click(sender As Object, e As EventArgs) Handles BtnShowImg.Click
Dim command As New SqlCommand("select * from MyCars where CarId = #CarId", connection)
command.Parameters.Add("CarId", SqlDbType.VarChar).Value = TxtID.Text
Dim table As New DataTable()
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(table)
If table.Rows.Count <= 0 Then
MessageBox.Show("No Image for the Selected Id")
Else
TxtID.Text = table.Rows(0)(0).ToString() ' Col 0 = CarId
TxtManufacturer.Text = table.Rows(0)(1).ToString() ' Col 1 = Manufacturer
TxtModel.Text = table.Rows(0)(2).ToString ' Col 2 = Model
TxtClass.Text = table.Rows(0)(3).ToString() ' Col 3 = Vehicle Class
Dim img() As Byte
img = table.Rows(0)(4) ' Col 4 = Img
Dim ms As New MemoryStream(img)
PicImage.Image = Image.FromStream(ms)
End If
End Sub
Interface - Hope It Helps

Related

image not retrieving from mysql using vb.net [duplicate]

Hi I posted this earlier and got some help but still no working solution. I have determined thanks to the last q & a that there is something wrong with my "save to db" code as well as my "retrieve to picture" code. Even If I manually save the pic in the db it stil wont retreive. This is code i patched together from 3 or 4 examples around the net. Ideally if someone had some known good code and could direct me to it that would be the best.
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)
![enter image description here][1]
'*****retieving 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()
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)
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
Dim adapter As New MySqlDataAdapter
adapter.SelectCommand = Cmd
data = New DataTable
adapter = New MySqlDataAdapter("select picture from [yourtable]", conn)
NOTE!! can only put one picture in picturebox so obviously this query can only return one record for you
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()
The accepted and upvoted answer may work, but it is suboptimal and quite wasteful:
If the image to save is on disk, there is no reason to use a UI control and a MemoryStream to get the image into a byte array.
The code also appears to reuse a single global connection object; these should be created and disposed of rather than reused.
Consider saving just the file name to the database, perhaps hashed, with the image saved to a special folder. Saving image data bloats the DB and takes longer to convert.
Finally, .GetBuffer() is quite incorrect:
The memstream buffer will often include unused, allocated bytes. With a 25k test file, ToArray() returns 25434 bytes - the correct size of the image - while GetBuffer() returns 44416. The larger the image, the more empty bytes there will be.
This uses MySQL provider objects since it is so-tagged, but the data provider (MySQL, SQLServer, OleDB etc) used doesnt matter: they all work the same.
In cases where the image source is a PictureBox, use a MemoryStream:
Dim picBytes As Byte()
Using ms As New MemoryStream()
picBox1.Image.Save(ms, imgFormat)
picBytes = ms.ToArray() ' NOT GetBuffer!
End Using
Since the image had to come from somewhere, if it is a file, this is all you need:
picBytes = File.ReadAllBytes(filename)
Once you have the image as bytes, to save:
Dim SQL = "INSERT INTO <YOUR TBL NAME> (picture, filename, filesize) VALUES(#Pic, #FileName, #FileSize)"
Using conn As New MySqlConnection(connstr)
Using cmd As New MySqlCommand(SQL, conn)
conn.Open()
cmd.Parameters.Add("#Pic", MySqlDbType.Blob).Value = picBytes
cmd.Parameters.Add("#FileName", MySqlDbType.String).Value = filename
cmd.Parameters.Add("#FileSize", MySqlDbType.Int32).Value = FileSize
cmd.ExecuteNonQuery()
End Using
End Using ' close and dispose of Connection and Command objects
Loading Image from DB
Dim imgData As Byte()
'... open connection, set params etc
Using rdr As MySqlDataReader = cmd.ExecuteReader
If rdr.HasRows Then
rdr.Read()
imgData = TryCast(rdr.Item("Image"), Byte())
' in case this record has no image
If imgData IsNot Nothing Then
' ToDo: dispose of any previous Image
' create memstream from bytes
Using ms As New MemoryStream(imgData)
' create image from stream, assign to PicBox
picBox1.Image = CType(Image.FromStream(ms), Image)
End Using
End If
End If
End Using
Note that Bitmaps and Images must be disposed of. If you repeatedly create new images as the user browses the database your app will leak and eventually crash. If you convert back and forth a lot, you can write a helper or extension method to convert images to bytes and vice versa.
DBConnection and DBCommand objects also need to be disposed of. The Using block does this for us.
References, Resources:
Reuse or Dispose DB Connections? sample GetConnection helper
Connection Pooling
MemoryStream.GetBuffer()
Can we stop using AddWithValue()
Tested Code for Store and Retrieve Images using MySQL and VB.NET
Public Class FMImage
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
Dim filename As String = TextBox1.Text
Dim FileSize As UInt32
Dim Conn As MySql.Data.MySqlClient.MySqlConnection
Conn = New MySql.Data.MySqlClient.MySqlConnection
Try
If Conn.State = ConnectionState.Open Then Conn.Close()
Conn.ConnectionString = MySQLConnectionString
Conn.Open()
Catch ex As Exception
MessageBox.Show(ex.ToString, "Connect")
End Try
Dim mstream As System.IO.MemoryStream = ConvertImageFiletoMemoryStream(filename)
PbPicture.Image.Save(mstream, Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = ConvertImageFiletoBytes(filename)
FileSize = mstream.Length
Dim sqlcmd As New MySql.Data.MySqlClient.MySqlCommand
Dim sql As String
mstream.Close()
'CREATE TABLE `actors` ( `actor_pic` longblob,`filesize` bigint(20) default NULL,`filename` varchar(150) default NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
sql = "insert into actors (actor_pic, filesize, filename) VALUES(#File, #FileName, #FileSize)"
Try
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)
End Try
Dim adapter As New MySql.Data.MySqlClient.MySqlDataAdapter
adapter.SelectCommand = New MySql.Data.MySqlClient.MySqlCommand("SELECT actor_pic, filesize, filename FROM actors", Conn)
Dim Data As New DataTable
'adapter = New MySql.Data.MySqlClient.MySqlDataAdapter("select picture from [yourtable]", Conn)
Dim commandbuild As New MySql.Data.MySqlClient.MySqlCommandBuilder(adapter)
adapter.Fill(Data)
MsgBox(Data.Rows.Count)
Dim lb() As Byte = Data.Rows(Data.Rows.Count - 1).Item("actor_pic")
Dim lstr As New System.IO.MemoryStream(lb)
PbPicture.Image = Image.FromStream(lstr)
PbPicture.SizeMode = PictureBoxSizeMode.StretchImage
lstr.Close()
End If
End Sub
Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
Dim _tempByte() As Byte = Nothing
If String.IsNullOrEmpty(ImageFilePath) = True Then
Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
Return Nothing
End If
Try
Dim _fileInfo As New IO.FileInfo(ImageFilePath)
Dim _NumBytes As Long = _fileInfo.Length
Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
Dim _BinaryReader As New IO.BinaryReader(_FStream)
_tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
_fileInfo = Nothing
_NumBytes = 0
_FStream.Close()
_FStream.Dispose()
_BinaryReader.Close()
Return _tempByte
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function ConvertBytesToMemoryStream(ByVal ImageData As Byte()) As IO.MemoryStream
Try
If IsNothing(ImageData) = True Then
Return Nothing
'Throw New ArgumentNullException("Image Binary Data Cannot be Null or Empty", "ImageData")
End If
Return New System.IO.MemoryStream(ImageData)
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function ConvertImageFiletoMemoryStream(ByVal ImageFilePath As String) As IO.MemoryStream
If String.IsNullOrEmpty(ImageFilePath) = True Then
Return Nothing
' Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
End If
Return ConvertBytesToMemoryStream(ConvertImageFiletoBytes(ImageFilePath))
End Function
End Class
I am having problem using mr #dMo 's program its showing me an error "Column 'picture' cannot be null"
here is my code.
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim filename As String = TextBoxSave.Text + ".jpg"
Dim FileSize As UInt32
conn.Close()
Dim mstream As New System.IO.MemoryStream()
PictureBoxSave.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 employeedetails (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
End Sub
P.S. Im sorry for posting this adnand answer I have no enough reputation to comment to this post
The code below inserts a record of vehicle information in a table. The Image of the car selected is converted into memory stream and saved to the Database as Varbinary.
A function is used to convert the image to memory stream.
' Upload new vehicle image
Private Sub BtnUpload_Click(sender As Object, e As EventArgs) Handles
BtnUpload.Click
Dim imgBinary As Byte()
With Me.OpenFileDialog1
.FileName = ""
.Filter = "Image Files(*.BMP;*.JPG;*.JEPG;*.GIF)|*.BMP;*.JPG;*.JEPG;*.GIF|All files (*.*)|*.*"
.RestoreDirectory = True
.ValidateNames = True
.CheckFileExists = True
If .ShowDialog = Windows.Forms.DialogResult.OK Then
'Me.PicImage.Image.Dispose()
Me.PicImage.Image = System.Drawing.Image.FromFile(.FileName)
End If
End With
imgBinary = ConvertImage(PicImage.Image)
Dim command As New SqlCommand("insert into MyCars(CarId, Manufacture, CarModel, CarClass, CarImage) values(#CarId, #Manufacture, #CarModel, #CarClass, #CarImage)", connection)
command.Parameters.Add("#CarId", SqlDbType.VarChar).Value = CInt(TxtID.Text)
command.Parameters.Add("#Manufacture", SqlDbType.VarChar).Value = TxtManufacturer.Text
command.Parameters.Add("#CarModel", SqlDbType.VarChar).Value = TxtModel.Text
command.Parameters.Add("#CarClass", SqlDbType.VarChar).Value = TxtClass.Text
command.Parameters.Add("#CarImage", SqlDbType.VarBinary).Value = imgBinary
Try
connection.Open()
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Car # " & TxtID.Text & " successfully added to database.")
Else
MessageBox.Show("Car not added!")
End If
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
connection.Close()
End Try
End Sub
The following function converts the image into memory stream.
' Convert Image from Memory Stream
Public Function ConvertImage(ByVal myImage As Image) As Byte()
Dim mstream As New MemoryStream
myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim myBytes(mstream.Length - 1) As Byte
mstream.Position = 0
mstream.Read(myBytes, 0, mstream.Length)
Return myBytes
End Function
The code below is used to display the image from the database. Use the ID (as an integer) to display the image.
Private Sub BtnShowImg_Click(sender As Object, e As EventArgs) Handles BtnShowImg.Click
Dim command As New SqlCommand("select * from MyCars where CarId = #CarId", connection)
command.Parameters.Add("CarId", SqlDbType.VarChar).Value = TxtID.Text
Dim table As New DataTable()
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(table)
If table.Rows.Count <= 0 Then
MessageBox.Show("No Image for the Selected Id")
Else
TxtID.Text = table.Rows(0)(0).ToString() ' Col 0 = CarId
TxtManufacturer.Text = table.Rows(0)(1).ToString() ' Col 1 = Manufacturer
TxtModel.Text = table.Rows(0)(2).ToString ' Col 2 = Model
TxtClass.Text = table.Rows(0)(3).ToString() ' Col 3 = Vehicle Class
Dim img() As Byte
img = table.Rows(0)(4) ' Col 4 = Img
Dim ms As New MemoryStream(img)
PicImage.Image = Image.FromStream(ms)
End If
End Sub
Interface - Hope It Helps

How can I retrieve a file that I saved on a MySQL database?

Here's my current code:
MySqlConnection.Open()
command.Connection = MySqlConnection
command.CommandText = "select * from add_document"
READER = command.ExecuteReader
READER.Read()
Dim filebytes() As Byte = CType(READER(1), Byte())
Dim FStream As New FileStream(READER(0).ToString, FileMode.Create, FileAccess.Write)
FStream.Write(filebytes, 0, filebytes.Length)
FStream.Close()
MessageBox.Show("File Retrieved")
I am receiving the following exception:
Unable to cast object of type'system.string' to type 'system.byte[]

Too many arguments to 'Public Sub New() VB .NET

I use MySqlBackup.NET_2.0.9.2 to backup a MySQL database.
I use this code:
Private Sub cmdExportBazaDate_Click(sender As System.Object, e As System.EventArgs) Handles cmdExportBazaDate.Click
Dim constring As String = Parametriiconexiune
Dim zi As String = data
Dim file As String = cale_salvari + data + "_baza" + ".sql"
Dim conn As MySqlConnection
conn = New MySqlConnection(constring)
Dim cmd As MySqlCommand
cmd = New MySqlCommand
cmd.Connection = conn
conn.Open()
Dim mb As MySqlBackup
mb = New MySqlBackup(cmd)
mb.ExportToFile(file)
conn.Close()
End Sub
I get the error:
Too many arguments to 'Public Sub New()'
For this line:
mb = New MySqlBackup(cmd)
How to fix please to bypass the error.

Updating Multiple Selected rows in DGV in VB.NET

I have a DataBound DGV it has 3 Columns namely ID(not pk), Name, and Status. I have 2 Buttons Add and Post. Add, adds Data to the DGV and DataBase(MySql) but in Status, it adds "No". What I want to do is for example I have 3 Rows in my DGV and highlight those 3 and click Post, the value in Status will be changed to "Yes". Here is what I have done so far, but I'm having trouble with the syntax for the UPDATE QUERY. THIS CODE WORKS NOW
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the TestDataSet.testing table. You can move, or remove it, as needed.
Me.TestingTableAdapter.Fill(Me.TestDataSet.testing)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using cmd
With cmd
MsgBox("Connection Established")
.Connection = _conn
.Parameters.Clear()
.CommandText = "Select Max(TransactionID) from testing"
_conn.Open()
Dim dr As MySqlDataReader
dr = cmd.ExecuteReader()
If dr.Read() Then
If IsDBNull(dr.Item(0)) Then
txtNumber.Text = "1"
Else
txtName.Text = dr(0).ToString() + 1
End If
End If
End With
End Using
End Using
FillGrid()
End Sub
Private Sub FillGrid()
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _comm As New MySqlCommand
With _comm
.CommandText = " SELECT `ID`, `TransactionID`, `Name`, `Posted` FROM `testing`"
.Connection = _conn
End With
Using _adapter As New MySqlDataAdapter(_comm)
Try
_conn.Open()
Dim _ds As New DataSet
_adapter.Fill(_ds)
GVTransaction.DataSource = _ds.Tables(0)
Catch ex As Exception
End Try
End Using
End Using
End Using
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Using conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using cmd
With cmd
MsgBox("Connection Established")
.Connection = conn
.Parameters.Clear()
.CommandText = "INSERT INTO testing(TransactionID, Name, Posted) VALUES (#ID, #iName, #iPosted)"
.Parameters.Add(New MySqlParameter("#ID", txtNumber.Text))
.Parameters.Add(New MySqlParameter("#iName", txtName.Text))
.Parameters.Add(New MySqlParameter("#iPosted", "No"))
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
MsgBox("Data Added to the Database")
Me.TestingTableAdapter.Dispose()
Me.TestingTableAdapter.Fill(Me.TestDataSet.testing)
End Sub
Private Sub btnPost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPost.Click
Dim _ID As New List(Of String)
Dim _Name As New List(Of String)
For Each _x As DataGridViewRow In TestingDataGridView.SelectedRows
_ID.Add(_x.Cells("GVTransactionID").Value.ToString())
_Name.Add("'" & _x.Cells("GVName").Value.ToString() & "'")
Next
Dim inClause As String = String.Join(",", _ID.ToArray())
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE testing SET Posted = #Posted WHERE TransactionID IN ({0}) AND Name IN ({1})", inClause, inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _commm As New MySqlCommand()
With _commm
.CommandText = _sqlUpdate
.Connection = _conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("#Posted", "YES")
End With
Try
_conn.Open()
_commm.ExecuteNonQuery()
FillGrid()
Catch ex As MySqlException
MsgBox(ex.StackTrace.ToString)
End Try
End Using
End Using
End Sub
End Class
Any Helps or tips is greatly appreciated. Please and Thank you
Try this one,
Dim _ID As New List(Of String)
Dim _Name As New List(Of String)
For Each _x As DataGridViewRow In TestingDataGridView.SelectedRows
_ID.Add(_x.Cells("TransactionID").Value.ToString())
_Name.Add("'" & _x.Cells("Name").Value.ToString() & "'")
Next
Dim inClause As String = String.Join(",", _ID.ToArray())
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE testing SET Posted = #Posted WHERE TransactionID IN ({0}) AND Name IN ({1})", inClause, inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _comm As New MySqlCommand()
With _comm
.CommandText = _sqlUpdate
.Connection = _conn
End With
Try
_conn.Open()
_comm.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
I believe you can simply use this
For Each Row As DataGridViewRow In TestingDataGridView.SelectedRows
.CommandText = "UPDATE testing SET Posted = #iChange WHERE ID = " & Row.Cells(0) & ", Name = " & Row.Cells(1)
.CommandType = CommandType.Text
.Parameters.AddWithValue("#iChange", "Yes")
End For
Hope this helps :)

Store image to database blob; retrieve from db into Picturebox

Hi I posted this earlier and got some help but still no working solution. I have determined thanks to the last q & a that there is something wrong with my "save to db" code as well as my "retrieve to picture" code. Even If I manually save the pic in the db it stil wont retreive. This is code i patched together from 3 or 4 examples around the net. Ideally if someone had some known good code and could direct me to it that would be the best.
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)
![enter image description here][1]
'*****retieving 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()
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)
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
Dim adapter As New MySqlDataAdapter
adapter.SelectCommand = Cmd
data = New DataTable
adapter = New MySqlDataAdapter("select picture from [yourtable]", conn)
NOTE!! can only put one picture in picturebox so obviously this query can only return one record for you
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()
The accepted and upvoted answer may work, but it is suboptimal and quite wasteful:
If the image to save is on disk, there is no reason to use a UI control and a MemoryStream to get the image into a byte array.
The code also appears to reuse a single global connection object; these should be created and disposed of rather than reused.
Consider saving just the file name to the database, perhaps hashed, with the image saved to a special folder. Saving image data bloats the DB and takes longer to convert.
Finally, .GetBuffer() is quite incorrect:
The memstream buffer will often include unused, allocated bytes. With a 25k test file, ToArray() returns 25434 bytes - the correct size of the image - while GetBuffer() returns 44416. The larger the image, the more empty bytes there will be.
This uses MySQL provider objects since it is so-tagged, but the data provider (MySQL, SQLServer, OleDB etc) used doesnt matter: they all work the same.
In cases where the image source is a PictureBox, use a MemoryStream:
Dim picBytes As Byte()
Using ms As New MemoryStream()
picBox1.Image.Save(ms, imgFormat)
picBytes = ms.ToArray() ' NOT GetBuffer!
End Using
Since the image had to come from somewhere, if it is a file, this is all you need:
picBytes = File.ReadAllBytes(filename)
Once you have the image as bytes, to save:
Dim SQL = "INSERT INTO <YOUR TBL NAME> (picture, filename, filesize) VALUES(#Pic, #FileName, #FileSize)"
Using conn As New MySqlConnection(connstr)
Using cmd As New MySqlCommand(SQL, conn)
conn.Open()
cmd.Parameters.Add("#Pic", MySqlDbType.Blob).Value = picBytes
cmd.Parameters.Add("#FileName", MySqlDbType.String).Value = filename
cmd.Parameters.Add("#FileSize", MySqlDbType.Int32).Value = FileSize
cmd.ExecuteNonQuery()
End Using
End Using ' close and dispose of Connection and Command objects
Loading Image from DB
Dim imgData As Byte()
'... open connection, set params etc
Using rdr As MySqlDataReader = cmd.ExecuteReader
If rdr.HasRows Then
rdr.Read()
imgData = TryCast(rdr.Item("Image"), Byte())
' in case this record has no image
If imgData IsNot Nothing Then
' ToDo: dispose of any previous Image
' create memstream from bytes
Using ms As New MemoryStream(imgData)
' create image from stream, assign to PicBox
picBox1.Image = CType(Image.FromStream(ms), Image)
End Using
End If
End If
End Using
Note that Bitmaps and Images must be disposed of. If you repeatedly create new images as the user browses the database your app will leak and eventually crash. If you convert back and forth a lot, you can write a helper or extension method to convert images to bytes and vice versa.
DBConnection and DBCommand objects also need to be disposed of. The Using block does this for us.
References, Resources:
Reuse or Dispose DB Connections? sample GetConnection helper
Connection Pooling
MemoryStream.GetBuffer()
Can we stop using AddWithValue()
Tested Code for Store and Retrieve Images using MySQL and VB.NET
Public Class FMImage
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
Dim filename As String = TextBox1.Text
Dim FileSize As UInt32
Dim Conn As MySql.Data.MySqlClient.MySqlConnection
Conn = New MySql.Data.MySqlClient.MySqlConnection
Try
If Conn.State = ConnectionState.Open Then Conn.Close()
Conn.ConnectionString = MySQLConnectionString
Conn.Open()
Catch ex As Exception
MessageBox.Show(ex.ToString, "Connect")
End Try
Dim mstream As System.IO.MemoryStream = ConvertImageFiletoMemoryStream(filename)
PbPicture.Image.Save(mstream, Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = ConvertImageFiletoBytes(filename)
FileSize = mstream.Length
Dim sqlcmd As New MySql.Data.MySqlClient.MySqlCommand
Dim sql As String
mstream.Close()
'CREATE TABLE `actors` ( `actor_pic` longblob,`filesize` bigint(20) default NULL,`filename` varchar(150) default NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
sql = "insert into actors (actor_pic, filesize, filename) VALUES(#File, #FileName, #FileSize)"
Try
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)
End Try
Dim adapter As New MySql.Data.MySqlClient.MySqlDataAdapter
adapter.SelectCommand = New MySql.Data.MySqlClient.MySqlCommand("SELECT actor_pic, filesize, filename FROM actors", Conn)
Dim Data As New DataTable
'adapter = New MySql.Data.MySqlClient.MySqlDataAdapter("select picture from [yourtable]", Conn)
Dim commandbuild As New MySql.Data.MySqlClient.MySqlCommandBuilder(adapter)
adapter.Fill(Data)
MsgBox(Data.Rows.Count)
Dim lb() As Byte = Data.Rows(Data.Rows.Count - 1).Item("actor_pic")
Dim lstr As New System.IO.MemoryStream(lb)
PbPicture.Image = Image.FromStream(lstr)
PbPicture.SizeMode = PictureBoxSizeMode.StretchImage
lstr.Close()
End If
End Sub
Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
Dim _tempByte() As Byte = Nothing
If String.IsNullOrEmpty(ImageFilePath) = True Then
Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
Return Nothing
End If
Try
Dim _fileInfo As New IO.FileInfo(ImageFilePath)
Dim _NumBytes As Long = _fileInfo.Length
Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
Dim _BinaryReader As New IO.BinaryReader(_FStream)
_tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
_fileInfo = Nothing
_NumBytes = 0
_FStream.Close()
_FStream.Dispose()
_BinaryReader.Close()
Return _tempByte
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function ConvertBytesToMemoryStream(ByVal ImageData As Byte()) As IO.MemoryStream
Try
If IsNothing(ImageData) = True Then
Return Nothing
'Throw New ArgumentNullException("Image Binary Data Cannot be Null or Empty", "ImageData")
End If
Return New System.IO.MemoryStream(ImageData)
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function ConvertImageFiletoMemoryStream(ByVal ImageFilePath As String) As IO.MemoryStream
If String.IsNullOrEmpty(ImageFilePath) = True Then
Return Nothing
' Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
End If
Return ConvertBytesToMemoryStream(ConvertImageFiletoBytes(ImageFilePath))
End Function
End Class
I am having problem using mr #dMo 's program its showing me an error "Column 'picture' cannot be null"
here is my code.
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim filename As String = TextBoxSave.Text + ".jpg"
Dim FileSize As UInt32
conn.Close()
Dim mstream As New System.IO.MemoryStream()
PictureBoxSave.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 employeedetails (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
End Sub
P.S. Im sorry for posting this adnand answer I have no enough reputation to comment to this post
The code below inserts a record of vehicle information in a table. The Image of the car selected is converted into memory stream and saved to the Database as Varbinary.
A function is used to convert the image to memory stream.
' Upload new vehicle image
Private Sub BtnUpload_Click(sender As Object, e As EventArgs) Handles
BtnUpload.Click
Dim imgBinary As Byte()
With Me.OpenFileDialog1
.FileName = ""
.Filter = "Image Files(*.BMP;*.JPG;*.JEPG;*.GIF)|*.BMP;*.JPG;*.JEPG;*.GIF|All files (*.*)|*.*"
.RestoreDirectory = True
.ValidateNames = True
.CheckFileExists = True
If .ShowDialog = Windows.Forms.DialogResult.OK Then
'Me.PicImage.Image.Dispose()
Me.PicImage.Image = System.Drawing.Image.FromFile(.FileName)
End If
End With
imgBinary = ConvertImage(PicImage.Image)
Dim command As New SqlCommand("insert into MyCars(CarId, Manufacture, CarModel, CarClass, CarImage) values(#CarId, #Manufacture, #CarModel, #CarClass, #CarImage)", connection)
command.Parameters.Add("#CarId", SqlDbType.VarChar).Value = CInt(TxtID.Text)
command.Parameters.Add("#Manufacture", SqlDbType.VarChar).Value = TxtManufacturer.Text
command.Parameters.Add("#CarModel", SqlDbType.VarChar).Value = TxtModel.Text
command.Parameters.Add("#CarClass", SqlDbType.VarChar).Value = TxtClass.Text
command.Parameters.Add("#CarImage", SqlDbType.VarBinary).Value = imgBinary
Try
connection.Open()
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Car # " & TxtID.Text & " successfully added to database.")
Else
MessageBox.Show("Car not added!")
End If
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
connection.Close()
End Try
End Sub
The following function converts the image into memory stream.
' Convert Image from Memory Stream
Public Function ConvertImage(ByVal myImage As Image) As Byte()
Dim mstream As New MemoryStream
myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim myBytes(mstream.Length - 1) As Byte
mstream.Position = 0
mstream.Read(myBytes, 0, mstream.Length)
Return myBytes
End Function
The code below is used to display the image from the database. Use the ID (as an integer) to display the image.
Private Sub BtnShowImg_Click(sender As Object, e As EventArgs) Handles BtnShowImg.Click
Dim command As New SqlCommand("select * from MyCars where CarId = #CarId", connection)
command.Parameters.Add("CarId", SqlDbType.VarChar).Value = TxtID.Text
Dim table As New DataTable()
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(table)
If table.Rows.Count <= 0 Then
MessageBox.Show("No Image for the Selected Id")
Else
TxtID.Text = table.Rows(0)(0).ToString() ' Col 0 = CarId
TxtManufacturer.Text = table.Rows(0)(1).ToString() ' Col 1 = Manufacturer
TxtModel.Text = table.Rows(0)(2).ToString ' Col 2 = Model
TxtClass.Text = table.Rows(0)(3).ToString() ' Col 3 = Vehicle Class
Dim img() As Byte
img = table.Rows(0)(4) ' Col 4 = Img
Dim ms As New MemoryStream(img)
PicImage.Image = Image.FromStream(ms)
End If
End Sub
Interface - Hope It Helps