2012-06-09 80 views
0

我想比較兩個像素的圖像。 我在Google上搜索過關於位圖的信息,但我沒有清楚。創建位圖來比較兩個圖像像素

我的密碼顯示錯誤Parameter is not valid

我試過這個;

var a = new Bitmap(imageurl);

但它不起作用。

我也請參閱本網站圖像比較:

http://www.c-sharpcorner.com/uploadfile/prathore/image-comparison-using-C-Sharp/

我曾嘗試:在這條線parameter is not valid

但顯示錯誤。

var img1 = new Bitmap(fileone); 
var img2 = new Bitmap(filetwo); 

我在這兩個可變這樣的存儲路徑,

fileone=C:\image\a.jpg: 
filetwo=c:\image\b.jpg; 

var img1 = new Bitmap(fileone); 
var img2 = new Bitmap(filetwo); 

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++; 
     } 
     // progressBar1.Value++; 
    } 

    if (flag == false) 
     Response.Write("Sorry, Images are not same , " + count2 + " wrong pixels found"); 
    else 
     Response.Write(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found"); 
} 
else 
    Response.Write("can not compare this images"); 

this.Dispose(); 
+1

您在編譯或運行程序時是否收到錯誤消息?代碼看起來不錯,所以我猜測它是在運行時,並且fileone/filetwo的值不正確(也許沒有正確轉義?)。 –

+0

我已檢查fileone和filetwo存儲正確的路徑以及該文件夾中也可用的圖像。 – Kango

+0

如果看到錯誤,請告訴我們異常詳細信息 –

回答

0

如果您在收到錯誤

var img1 = new Bitmap(fileone); 

最有可能你的fileone不存在,請確保那C:\image\a.jpg存在。

+0

我檢查過的圖像是在該文件夾中可用。 – Kango

2
fileone=C:\image\a.jpg: 
filetwo=c:\image\b.jpg; 

這不是有效的C#。你如何引用文件名是非常重要的,因爲反斜槓是一個轉義字符。

也許你真正的代碼看起來像

fileone="C:\image\a.jpg"; 
filetwo="C:\image\b.jpg"; 

然後\b變成退格字符,你想不是。您可以使用@literal來避免轉義處理:

[email protected]"C:\image\a.jpg"; 
[email protected]"C:\image\b.jpg"; 
+0

或使用「\\」而不是「\」 –