Control.CompareImage does not work

Bug reports.
chhb_fancy
Posts: 14
Joined: Thu Aug 14, 2008 2:09 am

Control.CompareImage does not work

Post by chhb_fancy » Mon Aug 18, 2008 2:18 am

The following is my code:

Form Calculator = Application.FindFormTitle("calculator", SearchMatchMode.MatchExact,true,5000);

if(Calculator != null){

Calculator.CaptureImage("D:\\RanorexProject\\Test\\test.bmp");
bool bResult = Calculator.CompareImage("D:\\RanorexProject\\Test\\test.bmp");

}

But the result of 'bResult' alreadys returns false.
Is this a bug or haven't I used the function correctly?

I'm using v1.5 trial version on Windows XP SP3.

Any help is appreciated.

Skarlso
Posts: 27
Joined: Thu Aug 07, 2008 9:11 am

Post by Skarlso » Mon Aug 18, 2008 1:37 pm

First:

YOu don't have to use if(Calculator != null){ because ranorex throws an error if the requested windows does not appear in the set time. By you it's 5000 milliseconds.

Second:

I agreed. This indeed does not work.. I tried:

form.CaptureImage();
bool compare = form.CompareImage();

if (compare) {
Logger.Info("Test Passed");
} else {
Logger.Error("Test Failed");
}

This should compare the same images, but it throws an error that they are not identical. I thougth maybe it is the opposite and also tried a sure to fail test. Which failed. So something else is the problem.

chhb_fancy
Posts: 14
Joined: Thu Aug 14, 2008 2:09 am

Post by chhb_fancy » Tue Aug 19, 2008 1:56 am

Thanks Skarlso for your response.

Is there any solution for this?

Skarlso
Posts: 27
Joined: Thu Aug 07, 2008 9:11 am

Post by Skarlso » Tue Aug 19, 2008 7:58 am

Unfortunatley no... I could not get it to work..

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Tue Aug 19, 2008 9:25 am

Sorry, there's an error in the documentation of the Control.CaptureImage(string fileName) method. Unlike stated in the documentation of this method, the image format is not(!) determined by the file extension. In fact, the saved image is always encoded as a PNG image.

Unfortunately, the CompareImage and FindImage methods only take BMP images as valid inputs. However, there's a workaround using the Control.CaptureImage() method that returns a Bitmap instance:

Code: Select all

// old code
//Calculator.CaptureImage("D:\\RanorexProject\\Test\\test.bmp"); 

// workaround
Calculator.CaptureImage().Save("D:\\RanorexProject\\Test\\test.bmp",
    System.Drawing.Imaging.ImageFormat.Bmp);
That should fix the CompareImage/FindImage problems on single-display systems. There's another known issue with multiple-display systems that occurs if the control to compare the image to is not located on the primary display. This bug is fixed in the upcoming V1.5.1 release.

Regards,
Alex
Ranorex Support Team

Skarlso
Posts: 27
Joined: Thu Aug 07, 2008 9:11 am

Post by Skarlso » Tue Aug 19, 2008 9:54 am

Great! That worked!

Thank you very much!
Gergely.

chhb_fancy
Posts: 14
Joined: Thu Aug 14, 2008 2:09 am

Post by chhb_fancy » Tue Aug 19, 2008 10:08 am

Thanks Alex for your answer.

Now is working. :-)

If I just wanna compare a rectangle of the image, not entire image.

for example:
Entire image rect: (2, 114) - (1261, 970) 1259 x 856
I wanna verify rect: (100, 100) - (200, 200) 100x 100

Is there any method for this? Any help is appreciated.

Skarlso
Posts: 27
Joined: Thu Aug 07, 2008 9:11 am

Post by Skarlso » Tue Aug 19, 2008 12:47 pm

CompareImage supports a parameter called Location which defines the upper left corner of the image to be compared..
And then you can add the Size and the Offset to. This should give you enough coordinates to enter a rectangle you want to compare.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Tue Aug 19, 2008 1:07 pm

Well, the CompareImage method supports a Location parameter, however, only for comparing a control with an image on screen.

For comparing a control to an image loaded from a file you should first generate the clipped image. Then use the CompareImage(string imageFileName, Point offset) method to compare that image to the control at a certain offset in the control.

E.g. first make the 100 x 100 image and then call CompareImage("image.bmp", new Point(50, 50)) to compare pixels (50, 50) to (150, 150) of the control to the image.

Regards,
Alex
Ranorex Support Team

chhb_fancy
Posts: 14
Joined: Thu Aug 14, 2008 2:09 am

Post by chhb_fancy » Tue Aug 26, 2008 6:08 am

Thanks Alex for your response.

But how to generate the clipped image?

I couldn't find any method to do it. Do you mean that I should use the third-party tool to do so, such as Photoshop?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Tue Aug 26, 2008 8:40 am

Well, you could use a 3rd party tool, but .NET works as well. There are dozens of samples on the internet on how to crop an image.

Regards,
Alex
Ranorex Support Team

chhb_fancy
Posts: 14
Joined: Thu Aug 14, 2008 2:09 am

Post by chhb_fancy » Tue Aug 26, 2008 9:03 am

Thank you very much. Alex :-)