0

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();


    }
marwaha.ks
  • 540
  • 1
  • 7
  • 19
  • Saving as a PNG rewrites the image, there may be differences introduced by the encoder. Take the locally saved png, copy it to the server, re-download and see if there is still a difference. – Alex K. Feb 25 '16 at 11:53
  • @AlexK. Done that, the result is that the code thinks its different thus different streams. – marwaha.ks Feb 25 '16 at 11:55
  • When comparing Images comparing pixels is also a nice way to do it. just loop over the X-Axis and inside loop over the Y-Axis so you can compare each Image. Check if Width and Height are the same before. – Felix D. Feb 25 '16 at 12:03
  • Compare these 2 byte array first: `bytes = wc.DownloadData(imageUrl);` and `b = File.ReadAllBytes("yourLocalImage");` and see if it equal. – NoName Feb 25 '16 at 12:26

0 Answers0