0

So, I am working on my Project in Windows Forms and have a little Problem. In this Project I have to compare multiple Images, that are in one Folder with a Screenshot.

If Screenshot contains image, Method have to return true otherwise it have to return false. All images in Folder are in "PNG" Format and I am changing there format to "Format24bppRgb" after getting them from file in code and in any 3-rd comparison it gives me "IndexOutOfRange".

I am using following code for comparing one by one images with Screenshot :

string[] addressArray = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "Kartebi2"), "*.png");

string[] imageAddress = new string[addressArray.Length];

Bitmap[] bitmap1 = new Bitmap[36];

Bitmap[] bitmapImage = new Bitmap[36];

for (int i = 0; i < bitmap1.Length; i++)
{
     bitmap1[i] = (Bitmap)System.Drawing.Image.FromFile(addressArray[i]);

     bitmapImage[i] = Form1.ConvertToFormat(bitmap1[i], System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}


for (int i = 0; i < bitmapImage.Length; i++)
{
     if (CompareMethod.CompareImageMethod(bitmap, bitmapImage[i]))    
         imageAddress[i] = addressArray[i];
     else
         imageAddress[i] = null;
}

and I am using following Compare method:

    public static bool CompareImageMethod(Bitmap screenshot, Bitmap template)
    {
        bool result;
        ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.921f);
        TemplateMatch[] matching = tm.ProcessImage(screenshot, template);
        if (matching[0].Similarity > 0.95f)
            result = true;
        else result = false;

        return result;
    }

So now about my Problem, In any 3-rd Comparasion it gives me following Exception: enter image description here

if anyone knows how to solve this problem please tell me. I have posted a similar Question but there I was asking for Compare Method(I didn't knew how to compare Images). In this Post I am asking about Exception about Compare function, that I have found in Internet.

Dave Dave
  • 21
  • 6
  • The easiest way just read it pixel by pixel from bitmap1 and bitmap2. Compare the color. https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel(v=vs.110).aspx –  Jul 11 '16 at 15:25

1 Answers1

0

change your code like this;

if (matching.Length != 0 && matching[0].Similarity > 0.95f)
Mert
  • 6,432
  • 6
  • 32
  • 68