Kango
Kango

Reputation: 847

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

Upvotes: 0

Views: 2071

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283614

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";

Upvotes: 2

yogi
yogi

Reputation: 19591

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.

Upvotes: 0

Related Questions