【问题标题】:Create bitmap to compare two images by pixel创建位图以按像素比较两个图像
【发布时间】:2012-06-13 05:54:24
【问题描述】:

我正在尝试按像素比较两个图像。 我在谷歌上搜索过位图,但我没有弄清楚。

我的代码显示错误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();

【问题讨论】:

  • 您是否在编译或运行程序时遇到错误?代码看起来不错,所以我猜它是在运行时,并且 fileone/filetwo 的值不正确(可能没有正确转义?)。
  • 我检查了 fileone 和 filetwo 存储正确的路径,并且该文件夹中也有可用的图像。
  • 如果您看到错误,请向我们提供异常详细信息
  • [ArgumentException: Parameter is not valid.] System.Drawing.Bitmap..ctor(String filename) +375973 validate.Day_8_campairTwoImageUpload.Button1_Click(Object sender, EventArgs e) in C:\Users\saisoftex \Documents\Visual Studio 2008\Projects\validate\validate\Day_8_campairTwoImageUpload.aspx.cs:120 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
  • 您的路径 'C:\image\a.jpg' 可能需要用额外的 \s 转义 'C:\\image\\a.jpg'

标签: c#


【解决方案1】:

如果您在

上遇到错误
var img1 = new Bitmap(fileone);

很可能您的 fileone 不存在,请确保 C:\image\a.jpg 存在。

【讨论】:

  • 我检查了该文件夹中有可用的图像。
【解决方案2】:
fileone=C:\image\a.jpg:
filetwo=c:\image\b.jpg;

这不是有效的 C#。并且如何引用文件名非常重要,因为反斜杠是转义字符。

也许你的真实代码看起来像

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

然后\b 变成一个退格字符,而不是你想要的。您可以使用@-literal 来避免转义处理:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 2022-01-02
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多