0

I want to compare two images to see if they are the same images by checking the pixel values of the images. DB has a table with a column with images saved as BLOB type.

I upload an image into a picture box, then checks it with the DB to retrieve the value in the column named "CID" when the pixel values of the picture box image is similar to pixel values of an image from the DB.

The code is in C# using MySql and VisualStudio.

However, it gives the exception of Parameter is not valid

What am I doing wrong here? Any help would be highly appreciated.

private void search_botton_Click_1(object sender, EventArgs e)
{
        if (leftRadio.Checked == true)
        {
            try
            {

                string MyConnection = "datasource=127.0.0.1;port=3306;username=root;password=;database=ahbis";
                string sql = "SELECT * FROM criminal";


                MySqlConnection MyConn = new MySqlConnection(MyConnection);
                MySqlCommand MyCommand = new MySqlCommand(sql, MyConn);
                MySqlDataReader MyReader;
                MyConn.Open();
                MyReader = MyCommand.ExecuteReader();

                while (MyReader.Read())
                {
                    //byte[] should be converted to Bitmap

                    byte[] img_Byte = (byte[])(MyReader["palmHistogram_A_L"]);

                    Image img1_IMAGE = (Bitmap)((new ImageConverter()).ConvertFrom(img_Byte));
                    //img1 = new Bitmap(img1_IMAGE);

                    Bitmap img1 = (Bitmap)new ImageConverter().ConvertTo(img1_IMAGE, typeof(Bitmap[]));


                    img2 = new Bitmap(histogram_pictureBox.Image);

                    string img1_ref, img2_ref;

                    if (img1.Width == img2.Width && img1.Height == img2.Height)
                    {
                        for (int i = 0; i < img1.Width; i++)
                        {
                            for (int j = 0; j < img1.Height; j++)
                            {
                                img1_ref = img1.GetPixel(i, j).ToString();
                                img2_ref = img2.GetPixel(i, j).ToString();
                                if (img1_ref != img2_ref)
                                {
                                    count2++;
                                    flag = false;
                                    break;
                                }
                                count1++;
                            }

                        }

                        if (flag == false)
                            MessageBox.Show("No matches found!");

                        else
                            MessageBox.Show("Match found!");

                            string cid = MyReader.GetString("CID");
                            textBox1.Text = cid;
                    }
                    else
                         MessageBox.Show("Something went wrong!");

                }
                MyConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }

The code also has the below part at the beginning;

 Bitmap img1,img2;
 int count1 = 0, count2 = 0;
 bool flag = true;
Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53
  • Which line do you get the exception? – Ankit Vijay Sep 17 '17 at 23:49
  • @AnkitVijay - it's "'System.ArgumentException' occurred in System.Drawing.dll in the line; Bitmap img1 = (Bitmap)tc.ConvertFrom(img_Byte); –  Sep 18 '17 at 02:27
  • Did you a have look here? https://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system-drawing-image – Ankit Vijay Sep 18 '17 at 03:47

1 Answers1

-1

I suggest Converting to base64 string would be easier to compare. Refer to my websites link below it might help you for converting image to Base64

I hope it would be a help.

my Website link here.

  • Based on the domain/URL of your link(s) being the same as, or containing, your user name, you appear to have linked to your own site. If you do so, you *must disclose that it's your site*. If you don't disclose that it's your own site, it's often considered spam. See: [**What signifies "Good" self promotion?**](//meta.stackexchange.com/q/182212) and [How to not be a spammer](//stackoverflow.com/help/promotion). – Makyen Sep 18 '17 at 03:18
  • How does this actually solve the problem which the OP has asked about? – Makyen Sep 18 '17 at 03:20
  • I'm not really sure if it can really help here. and i haven't tried it yet but i think instead of comparing it using pixel. why not convert it into string and compare it. by that it would be easier to compare . – Regie Baquero Sep 18 '17 at 03:27
  • @RegieBaquero - I don't think you've understood the purpose of the code here. It's feature extraction of images, so histogram images have been generated and needed to see if the content of images are the same, that's why the pixels are used. I don't want to check if the image I added into the picture box is already saved in the DB. –  Sep 18 '17 at 03:32
  • @AaaliaSajana - I understand now. my bad i am totally blinded with the sole purpose of the code and ended up in comparing the actual image. – Regie Baquero Sep 18 '17 at 03:44