Friday, March 25, 2016

Comparing two images without compromising performance.

During automation, I was given a task to compare two images. The catch here is to do it with fast running code which do not compromise performance.
Here I have found and tweaked a little code snippet. It is written in c#.
In this code, we are converting two Image objects into Base64 String. By Comparing the Base64 string together , we will know that whether images are same. The Code is below.
******start of the code*********
public static bool ImageCompareString(Image firstImage, Image secondImage)
{
var ms = new MemoryStream();
firstImage.Save(ms, ImageFormat.Png);
String firstBitmap = Convert.ToBase64String(ms.ToArray());
ms.Position = 0;
secondImage.Save(ms, ImageFormat.Png);
String secondBitmap = Convert.ToBase64String(ms.ToArray());
if (firstBitmap.Equals(secondBitmap))
{
return true;
}
else
{
return false;
}
}
*****end of code********

No comments:

Post a Comment