简体   繁体   中英

Create bitmap to compare two images by pixel

I am trying to compare two images by pixel. I have searched on Google about bitmap but I did not clear with it.

My code is showing error Parameter is not valid .

I have try this;

var a = new Bitmap(imageurl);

but it does not work.

I am also refer this site for image comparison:

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

What I have tried:

but showing error on this line parameter is not valid .

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

I have store path in this two variable like this,

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=C:\\image\\a.jpg: filetwo=c:\\image\\b.jpg; 

That's not valid C#. And how you quote filenames is VERY important, because backslash is an escape character.

Perhaps your real code looks like

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

Then \\b is turned into a backspace character, not what you wanted. You can use an @-literal to avoid escape processing:

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

If you are getting error on

var img1 = new Bitmap(fileone);

most probably your fileone doesn't exists, please make sure that C:\\image\\a.jpg exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM