简体   繁体   English

使用Imagecomparer,如何排除ToleranceRectangle的中间部分以进行比较?

[英]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.

相关问题 如何排除子文件夹被加载? - How can i exclude subfolders from being loaded? 如何使用字典替换正在比较的 2 个字符串? - How can I use a dictionary to replace 2 strings that are being compared? 如何在非UITest项目中设置VS以使用ImageComparer? - How do I setup VS to use ImageComparer in a non-UITest project? 如何有效地从字符串中间修剪选定的字符? - How can I efficiently trim selected characters from middle of a string? 如何将某些视图和文件夹排除在已部署的ASP.NET MVC NuGet包中? - How can I exclude some views and folders from being included into the deployed ASP.NET MVC NuGet package? 是否可以使用Mongo DB C#驱动程序序列化基类中的属性或字段? - Can I exclude properties or fields in a base class from being serialized with Mongo DB C# driver? 如何从基于另一个数组的列表中排除数据? - How can I exclude data from list based on another array? 如何从代码覆盖率中排除属性中的 lambda 函数? - How can I exclude lambda functions in properties from code coverage? 如何在 TeamCity 中排除 dotCover 覆盖的类型和方法? - How do I exclude types and methods from being covered by dotCover in TeamCity? 使用MVC和Entity Framework时如何排除模型成为数据库的一部分 - How to Exclude a model from being part of the database when using MVC and Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM