如何比较用C#.NET Image对象? [英] How to compare Image objects with C# .NET?

查看:469
本文介绍了如何比较用C#.NET Image对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以比较两个图片用C#对象?例如,检查它们是否相等,甚至更好的检查是多么相似的像素?

Can we compare two Image objects with C#? For example, check whether they are equal, or even better check how similar are their pixels?

如果可能的话,怎么办?

if possible, how?

推荐答案

您可以使用一套工具称为 TestApi ,这是一个开放源码库,以帮助单元测试。一个这样的API被称为视觉验证API ,它不会完全你所需要的 - 它可以比较两个图像,并告诉你,如果他们是平等的:

You can use a set of tools called TestApi, which is an open-source library to aid unit testing. One of such API is called Visual Verification API, and it does exactly what you need - it can compare two images and tell you if they are equal:

// 1. Capture the actual pixels from a given window
Snapshot actual = Snapshot.FromRectangle(new Rectangle(0, 0, 100, 100));

// 2. Load the reference/master data from a previously saved file
Snapshot expected = Snapshot.FromFile("Expected.png"));

// 3. Compare the actual image with the master image
//    This operation creates a difference image. Any regions which are identical in 
//    the actual and master images appear as black. Areas with significant 
//    differences are shown in other colors.
Snapshot difference = actual.CompareTo(expected);

// 4. Configure the snapshot verifier - It expects a black image with zero tolerances
SnapshotVerifier v = new SnapshotColorVerifier(Color.Black, new ColorDifference());

// 5. Evaluate the difference image
if (v.Verify(difference) == VerificationResult.Fail)
{
    // Log failure, and save the diff file for investigation
    actual.ToFile("Actual.png", ImageFormat.Png);
    difference.ToFile("Difference.png", ImageFormat.Png);
}

这篇关于如何比较用C#.NET Image对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆