Compare two images in C#

Atir Tahir
2 min readSep 17, 2019

--

One of the most efficient ways to compare two images is to do a pixel to pixel comparison. Its quite hard to develop your own algorithm or a technique (from scratch) to make a good image comparison tool.

However, there are a lot of third party tools/API that can help you in this regard. In this post, we will have a look at GroupDocs.Comparison for .NET API.

How it woks?
The comparison starts with the upper left corner and compares the entire image pixel by pixel. If the pixels are the same, it moves on to the next one and so on until it finds a different one. The changed pixels are given an ID. It is defined as dabbed, deleted or changed. They are highlighted in a specific color (blue, red or green according to the standard settings).

Source Image

This image doesn’t contain any mug or glasses

Target Image

This image has two additional objects on table (a mug and glasses)

Resultant Image

This resultant image highlights the difference in green color

API successfully detects the changes and highlight them in the output/resultant file. It always highlight the changes. In this case it highlighted the inserted item change.
This is how you do image comparison using GroupDocs.Comparison for .NET

//set detail level 
DetailLevel zonesDetailLevel = DetailLevel.Hight; //heigh, low, middle
ComparisonSettings settings = new ComparisonSettings();
settings.DetailLevel = zonesDetailLevel;
settings.UseFramesForDelInsElements = true;
settings.DeletedItemsStyle.FontColor = Color.Green;
settings.StyleChangedItemsStyle.FontColor = Color.Red;
ICompareResult compareResult = null;
Comparer comparer = new Comparer(); compareResult =
comparer.Compare(source path/file, target file/path, settings);
string resultPath = @"D:\GroupDocsTesting";
if (!Directory.Exists(resultPath)) Directory.CreateDirectory(resultPath);
compareResult.SaveImages(resultPath);

Download source code from GitHub.

--

--

No responses yet