Reputation: 229
I have a problem regarding on saving the image on database using BLOB data type, it save the image successfully but when i retrieve it using my picture box it ruins my image. ill show you the screenshot on my application. I am using vb.net.
this is my codes on saving image file in blob data type.
Dim filename As String = Me.OpenFileDialog1.FileName
Dim FileSize As UInt32
Dim conn As New MySqlConnection
conn = New MySqlConnection("server=localhost;user=root;password=;database=ticketing_system;")
conn.Open()
Dim mstream As New System.IO.MemoryStream()
Me.PbPicture.Image = Image.FromFile(Me.OpenFileDialog1.FileName)
Me.PbPicture.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
FileSize = mstream.Length
Dim sqlcmd As New MySql.Data.MySqlClient.MySqlCommand
Dim sql As String
mstream.Close()
' DBconn.Close()
sql = "INSERT INTO clientreports(img)VALUES(@File)"
Try
' DBconn.Open()
With sqlcmd
.CommandText = sql
.Connection = conn
.Parameters.AddWithValue("@File", arrImage)
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
here is my code on displaying my image on picturebox.
Dim strSQL As String
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader
conn = New MySqlConnection("server=localhost;user=root;password=;database=ticketing_system;")
Dim SQLConnection As MySqlConnection = New MySqlConnection
'Dim connection As New SqlConnection("connection string here")
Dim command As New MySqlCommand("SELECT img FROM errdeschis where err_id='31'", conn)
conn.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
conn.Close()
Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)
'Read the stream and create an Image object from the data.
PictureBox1.Image = Image.FromStream(stream)
End Using
please guys help me.
Upvotes: 0
Views: 862
Reputation: 56
Have you tried psren `s answer?
changed BLOB to LONGBLOB
It works for me!
Upvotes: 0