简体   繁体   English

从X和Y裁剪图像?

[英]Crop image from X and Y?

var ssbytearray = ((ITakesScreenshot)excelSession).GetScreenshot().AsByteArray;
var screenshot = new Bitmap(new MemoryStream(ssbytearray));
var croppedScreenshot = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
screenshot = screenshot.Clone(croppedScreenshot, screenshot.PixelFormat);

SaveScreenShot(saveLocation, screenshot);

In my code, I'm currently taking a screenshot of a Microsoft Office application that I called, "element" and it saves the image as its meant to. 在我的代码中,我正在拍摄一个我称之为“元素”的Microsoft Office应用程序的屏幕截图,它将图像保存为其意图。 Unfortunately if I want to crop the image from the top or left side, it isn't as easy as replacing the X and Y coordinates with a number when creating the Rectangle. 不幸的是,如果我想从顶部或左侧裁剪图像,那么在创建矩形时用数字替换X和Y坐标并不容易。 If I do try to change the X or Y coordinate with a value I get a SystemOutOfMemory exception. 如果我尝试使用值更改X或Y坐标,我将获得SystemOutOfMemory异常。 However if I want to crop it from the width and height and I try to change the value I'm able to. 但是,如果我想从宽度和高度裁剪它,我会尝试更改我能够的值。

I'd like to be able to crop the screenshot taken from the top and left side. 我希望能够裁剪从顶部和左侧拍摄的截图。 Currently I'm only able to crop the screenshot from the bottom or right side using the width & height. 目前我只能使用宽度和高度从底部或右侧裁剪屏幕截图。

Any help's appreciated. 任何帮助表示赞赏。

I solved this issue by changing the x, y, height & width values when creating the Rectangle. 我通过在创建Rectangle时更改x,y,height和width值来解决此问题。 It will produce a System.OutOfMemory exception if the values you create the Rectangle with are bigger than the element you're trying to capture. 如果您创建Rectangle的值大于您尝试捕获的元素,它将产生System.OutOfMemory异常。

However this causes problems when changing resolutions. 但是,这会在更改分辨率时导致问题 Currently trying to solve that issue now. 目前正试图解决这个问题。

EDIT: To fix the second issue I came across I am changing the resolution of the system before & after the test runs so it can run on a resolution that's common on most systems, then change it back to its original. 编辑:为了解决我遇到的第二个问题,我在测试运行之前和之后更改了系统的分辨率,因此它可以运行在大多数系统上常见的分辨率,然后将其更改回原始分辨率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM