2017-09-17 123 views
0

我想比較兩個圖像,看看他們是通過檢查圖像的像素值相同的圖像。 DB有一個表格,其中的圖像保存爲BLOB類型。如何解決「參數無效」異常

我將圖片上傳到圖片框中,然後使用數據庫檢查圖片,以檢索圖片框圖片的像素值與來自數據庫的圖片的像素值相似時名爲「CID」的列中的值。

該代碼是在使用MySQL和VisualStudio的C#。

然而,它給人的Parameter is not valid

異常我在做什麼錯在這裏?任何幫助將不勝感激。

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); 
      } 

     } 
    } 

該代碼在開頭處也有以下部分;

Bitmap img1,img2; 
int count1 = 0, count2 = 0; 
bool flag = true; 
+0

哪條線你會得到例外? –

+0

@AnkitVijay - 這是「 'System.ArgumentException' 發生在System.Drawing.dll程序的線;位圖IMG1 =(位圖)tc.ConvertFrom(img_Byte); –

+0

難道你有看這裏https://stackoverflow.com? /問題/ 629955 /參數 - 不合法的異常加載系統繪圖圖像根據您的鏈接(S)的域/ URL –

回答

-1

我建議轉換爲base64字符串比較容易。 請參考我的網站鏈接下方它可以幫助你轉換圖像爲Base64

我希望這將是一個幫助。

my Website link here.

+0

是相同的,或包含,您的用戶名,你似乎已經鏈接如果你這樣做,你*必須披露它是你的網站*如果你沒有透露它是你自己的網站,那麼它通常被認爲是垃圾郵件,請參閱:[**什麼意思是「良好」的自我推銷? **](// meta.stackexchange.com/q/182212)和[如何不是垃圾郵件發送者(// stackoverflow.com/help/promotion)。 – Makyen

+0

如何,這實際上解決了該OP已要求問題關於? – Makyen

+0

我真的不確定它是否真的可以幫到這裏。我還沒有嘗試過,但我認爲比較它使用像素。爲什麼不把它轉換成字符串並進行比較。因爲它比較容易。 –