[英]Using Imagecomparer, How can I exclude a middle section of a ToleranceRectangle from being compared?
I am using imagecomparer in my mobile test project and I am able to compare a baseline image to a current screenshot, but the problem comes in where there is a section of the screenshot that is always changing and I would like to exclude that part from being compared. 我在移动测试项目中使用imagecomparer,并且能够将基线图像与当前屏幕截图进行比较,但是问题出在屏幕截图的某个部分始终在变化,因此我想排除一部分比较。 Here is my code: 这是我的代码:
private bool RunVisualCheck(string screen, string resultsPath, string baseline = "baseline.jpeg", string screenshot = "screenshot.jpeg")
{
GetScreenshot(resultsPath + screenshot);
var baselineImage = Image.FromFile(resultsPath + baseline);
var actualImage = Image.FromFile(resultsPath + screenshot);
Image diffImage;
int ignoreTop = 64;
var compareArea = new List<ToleranceRectangle>
{
new ToleranceRectangle()
{
Rectangle = new Rectangle(0,ignoreTop,baselineImage.Width, baselineImage.Height - ignoreTop),
Difference = new ColorDifference()
}
};
bool goodCompare = ImageComparer.Compare(actualImage, baselineImage, compareArea, out diffImage);
if (!goodCompare)
{
diffImage.Save(resultsPath + "diffImage.jpeg");
}
return goodCompare;
}
private void GetScreenshot(string pathFile)
{
System.Threading.Thread.Sleep(2000); // Temp fix to wait until page loads
var srcFiler = ((ITakesScreenshot)mobileDriver).GetScreenshot();
srcFiler.SaveAsFile(pathFile, ImageFormat.Jpeg);
}
Here is an example (not the app being tested) where I would like to exclude the area inside the red rectangle from the overall screenshot from being compared. 这是一个示例(不是正在测试的应用程序),在这里我想从整体屏幕快照中排除红色矩形内的区域以进行比较。
Mobile Screenshot Example 移动屏幕截图示例
Is there an easy way to do this? 是否有捷径可寻?
Found a better approach than trying to exclude a section from being compared. 找到了比尝试排除某个部分不进行比较的更好的方法。 Thanks to a coworkers suggestion, I am blacking out the sections that do not need comparing and then saving this image. 感谢同事的建议,我将不需要比较的部分涂黑,然后保存此图像。 Doing this on the baseline image and the screenshot will have the same effect as excluding it altogether. 在基线图像和屏幕截图上执行此操作与完全排除它具有相同的效果。 Here is the code: 这是代码:
Image image = Image.FromFile(@"C:\Screenshots\Screenshot.jpeg");
using (Graphics g = Graphics.FromImage(image))
{
SolidBrush brush = new SolidBrush(Color.Black);
Size size = new Size(image.Width, 64);
Point point = new Point(0, 0);
Rectangle rectangle;
rectangle = new Rectangle(point, size);
g.FillRectangle(brush, rectangle);
}
image.Save(@"C:\Screenshots\Screenshot.jpeg");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.