C# 裁剪图像返回错误的坐标 [英] C# Cropping image return wrong coordinates

查看:29
本文介绍了C# 裁剪图像返回错误的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来,我一直在尝试使用 Selenium 和不同的裁剪方法来裁剪特定的图像.

I've been trying to crop a specific image using Selenium and different cropping methods for a few days.

在我的代码之前的一个重要说明 - 以下方法在 2 周前曾经有效,但由于某种原因它现在返回坐标错误的图像

// Go to site
Driver.Navigate().GoToUrl("http://google.com");
Screenshot screenshot = driver.GetScreenshot();

using (var ms = new MemoryStream(screenshot.AsByteArray))
using (var imgShot = Image.FromStream(ms))
using (var src = new Bitmap(imgShot))
      {
        IWebElement element = driver.FindElement(By.XPath("//canvas"));
        Rectangle cropRect = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);

        var clone = src.Clone(cropRect, src.PixelFormat);
        clone.Save(filePath);
      }

我尝试过的事情:

1) 为此,我通常使用 Firefox 驱动程序,我尝试改用 ChromeDriver 并得到相同的结果.

1) I usually use Firefox driver for this purpose, I tried using ChromeDriver instead and got the same result.

2) 我使用以下控制台命令检查了元素的坐标:$0.getBoundingClientRect() 并且我在代码中得到的位置与它匹配​​.

2) I checked for the element's coordiantes using the following console command: $0.getBoundingClientRect() and the position I got in my code matches it.

3) 我尝试了 4 种不同的裁剪方法,包括这个:

3) I tried 4 different cropping methods including this one:

IWebElement element = Driver.FindElement(By.XPath("//canvas"));
string filename = @"C:\Users\User\Desktop\test.png";
Screenshot screenshot = Driver.GetScreenshot();
screenshot.SaveAsFile(filename, ImageFormat.Png);

Rectangle cropRect = new Rectangle(element.Location.X, element.Location.Y,
    element.Size.Width, element.Size.Height);

using (Image imgShot = Image.FromFile(filename))
using (Bitmap original = new Bitmap(imgShot))
using (Bitmap target = new Bitmap(original, new Size(cropRect.Width, cropRect.Height)))
using (Graphics g = Graphics.FromImage(target))
{
    g.DrawImage(original, new Rectangle(0, 0, target.Width, target.Height),
        cropRect,
        GraphicsUnit.Pixel);
    target.Save(@"C:\Users\User\Desktop\test1.png", ImageFormat.Png);
}

为了清楚起见,我得到的图像完全是空白的.在另一个网站上,我得到的图像不是空白的,所以我可以看出它只是在错误的坐标中.

Just to be clear, the image I get is totally blank. In a different website the image I get is not blank so I can tell it's just in the wrong coordinates.

4) 我尝试了不同的网站和不同的元素,但它们都在错误的坐标中.

4) I tried a different website and different elements and they were all in the wrong coordinates.

5) 我试着用谷歌搜索,发现有很多不同的方法都行不通.然而,这个 answer 说了一些关于分辨率的事情,这是我最好的猜测.我尝试同时使用原始分辨率和目标分辨率,但没有发现任何区别.在创建 Graphics 变量之前或之后调用设置分辨率方法,并且仍然为零更改.

5) I tried to Google it and found so many different approaches that didn't work. This answer however, says something about resolution which was my best guess. I tried playing with both the original and the target's resolution and saw no difference. The set resolution method was called either before or after the Graphics variable was created and still, zero change.

有趣的是,它曾经在 2 周前工作过,但我从未更改过代码...

The funny thing is, it used to work 2 weeks ago but I never changed the code...

推荐答案

很明显,两周前真正改变的不是我的代码,而是我机器上安装的 Firefox 版本.当前版本 - v52,返回坐标错误的元素.卸载它并重新安装以前的版本 - v47 解决了问题

So apparently the thing that actually changed 2 weeks ago wasn't my code but the Firefox version installed on my machine. The current version - v52, return elements in wrong coordinates. Uninstalling it and reinstalling the previous version - v47 solved the issue

这篇关于C# 裁剪图像返回错误的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆