比较两个图像,下载一个图像

时间:2016-02-25 11:51:07

标签: c# image image-comparison

我比较两张图片;源代码在我的解决方案中(并将其保存到内存流)和其他我正在使用' WebClient'将其转换为字节,然后将其保存在流中,然后比较流。

它们是完全相同的图像。但是我的代码会生成不同的哈希字符串,所以' If Equals'产生错误。

我认为下载和保存图像正在改变字符串。

代码:

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


    }

0 个答案:

没有答案