1 Replies - 2361 Views - Last Post: 12 July 2012 - 10:06 AM Rate Topic: -----

#1 onlyshiv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-April 12

Save and Fetch Image from database

Posted 12 July 2012 - 09:39 AM

I have tried to save image in my database using the code below.
But as soon as I click on add button to add image and select image from file to save it shows me an error

A generic error occurred in GDI+.

Here is my code
Imports MySql.Data.MySqlClient
Imports MySql.Data

Public Class Form1
    Dim OpenFileDialog1 As New OpenFileDialog
    Dim connection As String = "server =localhost; userid=root; database=youtube"
    Dim conn As New MySqlConnection
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn.ConnectionString = connection
    End Sub
    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

            Try
                If conn.State = ConnectionState.Open Then conn.Close()
                conn.Open()

            Catch ex As Exception
                MessageBox.Show(ex.ToString, "Connect")
            End Try


            Dim mstream As System.IO.MemoryStream = ConvertImageFiletoMemoryStream(filename)
            PictureBox1.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, @FileSize,@FileName)"

            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)
            PictureBox1.Image = System.Drawing.Image.FromStream(lstr)
            PictureBox1.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




It is showing me an error on this line
PictureBox1.Image.Save(mstream, Drawing.Imaging.ImageFormat.Jpeg)


thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Save and Fetch Image from database

#2 drec  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 42
  • Joined: 06-July 12

Re: Save and Fetch Image from database

Posted 12 July 2012 - 10:06 AM

when i searched for generic error in GDI, i found this

http://social.msdn.m...1-92c786cca4e6/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1