1

I have an image loaded into a picture box and some images saved as BLOB in MySQL. I want to retrieve some data from the row if this image in the picture box is same as the one saved in the database.

There should be only one match and if it doesn't match, should prompt a message as "No match".

I'm using C#, MySQL and Visual Studio.

I don't know how to do this exactly, I'm new to C# and I don't get the output as I want. Maybe I should first convert the IMAGE in the picture box to something else??

Here is what I've come up so far. Any suggestion is highly appreciated.

private void button8_Click(object sender, EventArgs e)
{
    // Check button
    Image ori_histoImage = histogram_pictureBox.Image;
    byte[] histo = (Byte[])new ImageConverter().ConvertTo(ori_histoImage, typeof(byte[]));
    string histoS = System.Convert.ToBase64String(histo);

    try
    {
        string MyConnection = "datasource=127.0.0.1;port=3306;username=root;password=;database=ahbis";

        string sql = "SELECT * FROM criminal WHERE palmHistogram= '" + histo + "';";

        MySqlConnection MyConn = new MySqlConnection(MyConnection);
        MySqlCommand MyCommand = new MySqlCommand(sql, MyConn);

        int count = Convert.ToInt32(MyCommand.ExecuteScalar());

        MySqlDataReader MyReader;

        MyConn.Open();
        MyReader = MyCommand.ExecuteReader();

        while (MyReader.Read())
        {
            if(count !=1)
            {
                MessageBox.Show("No match");

                string cid = (MyReader["CID"].ToString());
                textBox1.Text = cid;  
            }

            else
                MessageBox.Show("No match");
        }   
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • 1
    Why saving the image as a BLOB into a database.. if you want to compare if the images are the same you should be using a md5/sha1 checksum or something like that... MySQL has a limit on his packet size you can get in trouble with that.. the defualt packet size is 1 Mb i think and base64 makes the image request size 33% bigger. – Raymond Nijland Sep 17 '17 at 13:34
  • what are the outputs you get? are you getting any errors as of now or can you atleast see when there is no match? – Neville Nazerane Sep 17 '17 at 13:34
  • @NevilleNazerane - According to this code, I should be get the value in the "CID" column into a text box. It does give an output, but it doesn't give the correct CID value. –  Sep 17 '17 at 13:37
  • and there is supposed to be only one CID record for the search right (if there is)? – Neville Nazerane Sep 17 '17 at 13:42
  • @RaymondNijland - I need them to be saved as BLOB. However, I think the conversion where I try to convert IMAGE to some data type to be match with BLOB is where I went wrong. I've tried many conversions, here I'm trying to convert it to byte[] and then to string and match with BLOB. –  Sep 17 '17 at 13:43
  • @NevilleNazerane - Yes. Only one. I think the conversion where I try to convert IMAGE to some data type to be match with BLOB is where I went wrong. I've tried many conversions, here I'm trying to convert it to byte[] and then to string and match with BLOB. –  Sep 17 '17 at 13:44
  • try printing your sql variable and try running the generated sql in the database. just to make sure the sql is generated right. – Neville Nazerane Sep 17 '17 at 13:47
  • by the way raymond is right. I just realized you are trying to pass an entire image in the query. Is is really not recommended pass an entire image as a query all the way to the db. That would be a really huge query. – Neville Nazerane Sep 17 '17 at 13:49
  • @NevilleNazerane - oh.. okay, I didn't knew that. I though the problem was with the conversion of IMGE in the picture box to be compared with BLOB. Because I don't know how to compare with a BLOB. –  Sep 17 '17 at 13:57
  • Do print the variable and try running it to check what exactly the problem is. by the way, are the blobs inserted by your c# code as well? – Neville Nazerane Sep 17 '17 at 14:01
  • @NevilleNazerane - Yes, in another UI they are added to DB. –  Sep 17 '17 at 14:04

1 Answers1

0

When you save the blob into the db, as Raymond suggested, also store a checksum field in your database. You can check this link Calculate MD5 checksum for a file

When trying to look for the blob, redo the function to create the checksum. Now instead of comparing the two blobs, you can compare the two check sums. Once again, if it doesn't work, print the sql and make sure the sql is created right.

Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79