How to get image in MySQL longblob database?

I use Windows Form and MySQL for my project. In this I want to save the image and get it.

I created a table called 'image' in which

CREATE TABLE `image` ( `id` INT(15) NOT NULL AUTO_INCREMENT, `extension` VARCHAR(50) NOT NULL, `image` LONGBLOB NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM ROW_FORMAT=DEFAULT AUTO_INCREMENT=2 

AND

 OpenFileDialog open = new OpenFileDialog(); // image filters open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png"; if (open.ShowDialog() == DialogResult.OK) { txt_imagePath.Text = open.FileName; } hp.getConnStr(); try { MySqlConnection connection = new MySqlConnection(hp.myConnStr); MySqlCommand command = connection.CreateCommand(); command.CommandText = "insert into image (image) values ('"+txt_imagePath.Text +"')"; command.Connection.Open(); command.ExecuteNonQuery(); command.Connection.Close(); } 

in the event of clicking on the "Browse" button, ..

File saved successfully. Now I want to get this photo and show in the image window. So I try this code below, ..

  MySqlConnection connection = new MySqlConnection(hp.myConnStr); MySqlCommand command = connection.CreateCommand(); MySqlDataReader Reader; command.CommandText = "select image from image"; connection.Open(); Reader = command.ExecuteReader(); while (Reader.Read()) { pictureBox1.Image = new Bitmap(Reader[0].ToString()); } connection.Close(); 

but not used.

Please help me.

0
source share
3 answers

This example is exactly what you need.

http://www.dhirajranka.com/?p=421

You should read the blob field as a MemoryStream and set the Image property of the control using Image.FromStream() .

0
source

Hope something like this helps:

  public Bitmap loadImage (int imgID)
         {

             MySqlDataReader myData;
             MySqlCommand cmd = new MySqlCommand ();

             string SQL;
             byte [] rawData;
             MemoryStream ms;
             UInt32 FileSize;
             Bitmap outImage;

             SQL = "SELECT ImageName, ImageSize, Image FROM Images WHERE ImageID =";
             SQL + = imgID.ToString ();

             try
             {
                 cmd.Connection = connection;
                 cmd.CommandText = SQL;

                 myData = cmd.ExecuteReader ();

                 if (! myData.HasRows)
                     throw new Exception ("There are no blobs to save");

                 myData.Read ();

                 FileSize = myData.GetUInt32 (myData.GetOrdinal ("ImageSize"));
                 rawData = new byte [FileSize];

                 myData.GetBytes (myData.GetOrdinal ("Image"), 0, rawData, 0, (Int32) FileSize);


                 ms = new MemoryStream (rawData);
                 outImage = new Bitmap (ms);
                 ms.Close ();
                 ms.Dispose ();

                 myData.Close ();
                 myData.Dispose ();

                 cmd.Dispose ();

                 return outImage;


             }
             catch (MySqlException ex)
             {
                 MessageBox.Show (ex.Message);
                 return null;
             }

         }
0
source
 while (Reader.Read()) { pictureBox1.Image = new Bitmap(new MemoryStream((byte[])Reader.GetValue(0))); } 

it should do it. reader.GetValue () returns an array of bytes in MySQL blobs, this does the trick.

0
source

All Articles