Inserting and retrieving images

Tonskie01

New member
Joined
Jan 9, 2012
Messages
2
Programming Experience
3-5
hello everyone,
I'm not used to join into forums like this but I would like to try asking you something very important for me... Does anybody have an idea one how to store and retrieve images in a mysql databse?.. I have already stored some images into my database but a problem occurs like "out of memory" error when i try to retrieve it... Could it be I have stored the image in a wrong way?.. It would be very helpful if sample codes would be posted for reference... I would really appreciate any post of sample codes... thanks a lot...
 
I forgot to post my codes... sorry for that... Here are my codes...

'inserting image into mysql database
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.OpenFileDialog1.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Me.PictureBox1.Image = Image.FromFile(Me.OpenFileDialog1.FileName)
    End Sub
    Public Sub Inpic()
        Dim fsize As UInt32
        Dim mstream As New System.IO.MemoryStream()
        Me.PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim arrImage() As Byte = mstream.GetBuffer()
        fsize = mstream.Length
        Dim cmd As New MySqlCommand("INSERT INTO pictures(idPictures,Pic) VALUES('" & Me.TextBox11.Text & "', '& arrImage &')", Sconnection)
        mstream.Close()
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Sconnection.Close()
        End Try

'retrieving images form mysql database
 If Not Form14.PictureBox1.Image Is Nothing Then
            Form14.PictureBox1.Image.Dispose()
        End If
        If id = Nothing Then
            MsgBox("Please select driver to renew.", MsgBoxStyle.Information)
        Else
            If Sconnection.State = ConnectionState.Closed Then
                Sconnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD =; DATABASE = License Monitoring;"
                Sconnection.Open()
            End If
            Try
                Dim tb As New MySqlDataAdapter()
                tb.SelectCommand = New MySqlCommand("SELECT Pic FROM pictures WHERE idPictures='" & id & "'", Sconnection)
                dset = New DataSet("dset")
                tb.Fill(dset)
                Dim dtable As DataTable
                dtable = dset.Tables(0)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Dim dt As DataTable = dset.Tables(0)
            Dim datar As DataRow
            Dim FS As FileStream
            FS = New FileStream("image.jpg", FileMode.Create)
            For Each datar In dt.Rows
                Dim blob() As Byte = datar("Pic")
                FS.Write(blob, 0, blob.Length)
                FS.Close()
                FS = Nothing
                Form14.PictureBox1.Image = System.Drawing.Image.FromFile("image.jpg")
                Form14.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
                Form14.PictureBox1.Refresh()
            Next
            Sconnection.Close()
            Form14.ShowDialog()
 
Last edited by a moderator:
Back
Top