I'm comparing two images; the source is in my solution (and saving it to memory stream) and other I am downloading using 'WebClient' converting it to bytes and then saving it in a stream, then comparing the streams.
They are exactly the same image. However my code produces different hash strings, so the 'If Equals' produces a false.
I'm thinking the downloading and saving of the image is altering the string.
The code:
public void CompareImages(string icon)
{
WebClient wc = new WebClient();
MemoryStream ms = new MemoryStream();
Image expectedImage = Image.FromFile(AppConfig.ToolsFilesFolderName + string.Format("\\" + icon +".png"));
expectedImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
String firstBitmap = Convert.ToBase64String(ms.ToArray());
ms.Position = 0;
MemoryStream ms2 = null;
byte[] bytes;
var baseEventType = _driver.FindElement(By.XPath("//div[@id='EventsView2_tv']/div/div")); ///div/div/div[19] //what i need to do is expand all of them and then
int count = 0;
var eventTypeExists = baseEventType.FindElements(By.TagName("tr"));
for (int i = 0; i < eventTypeExists.Count; i++)
{
if (i>30)
return;
if (eventTypeExists[i].Text.Trim().ToLower().Equals(icon.ToLower()))
{
var imgPath = eventTypeExists[i].FindElement(By.XPath("td[3]/img"));
var imageUrl = imgPath.GetAttribute("src");
bytes = wc.DownloadData(imageUrl);
ms2 = new MemoryStream(bytes);
Image actualImage = Image.FromStream(ms2);
actualImage.Save(ms2, System.Drawing.Imaging.ImageFormat.Png);
String secondBitmap = Convert.ToBase64String(ms2.ToArray());
if (firstBitmap.Equals(secondBitmap))
{
Reporter.ReportNote(string.Format("'{0}' icon in '{1}' is correct",
icon, ScenarioContext.Current["contractName"] + "/" + eventTypeExists[i].FindElement(By.XPath("../../../../table[" + (i - 2) + "]")).Text),
Status.Pass);
}
else
{
Reporter.ReportNote(string.Format("Icons are not correct, offending image is located within '{0}'",
ScenarioContext.Current["contractName"] + "/" + eventTypeExists[i].FindElement(By.XPath("../../../../table[" + (i - 2) +"]")).Text),
Status.Done);
}
ms2.Dispose();
}
count++;
}
ms.Dispose();
wc.Dispose();
}