1

I'm trying to use the following code to copy a portion of the screen to a new location on my Windows form.

private void Form1_Paint(object sender, PaintEventArgs e)
{
    var srcPoint = new Point(0,0);
    var dstPoint = new Point(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
    var copySize = new Size(100, 100);

    e.Graphics.CopyFromScreen(srcPoint, dstPoint, copySize, CopyPixelOperation.SourceCopy);
}

The CopyFromScreen function appears to ignore any clips set before it.

e.SetClip(new Rectangle(srcPoint.X, srcPoint.Y, 20, 20));

Am I doing something wrong or is this just the wrong approach.

For context: I'm trying to mitigate a UI widescreen game issue by copying the HUD at the edges and to be centered closer to the middle.

I am aware of FlawlessWidescreen, but it doesn't support many less popular games. I suppose poking around in memory (what flawless does) could also work but is almost always against TOS.

Edit: final goal is to copy some arbitrary path as the shape rather than a simple rectangle (I was hoping from an image mask).

Edit #2: So I have an irregular shape being drawn every 100ms. It turns out it just bogs the game down until I slow it down to every 500ms. But still the game isn't smooth. Is this operation of copying and drawing an image just going to be too heavy of an operation in GDI+? I was thinking it was simple enough to not bog anything down.

Thoughts before I mark the answer as accepted?

4

2 回答 2

2

我想这确实是错误的方法。

ClippingRegion仅用于剪裁DrawXXXandFillXXX命令,包括( DrawImage! )

然而,CopyFromScreen将使用给定的Points不是剪辑源。Size

对于一个Rectangle区域,这没有问题,因为您可以通过选择正确的值PointSize值来获得相同的结果。

但是,一旦您打算使用更有趣的剪辑区域,您将不得不使用Bitmap从屏幕复制到其上的中间体,然后您可以从中使用DrawImage到剪辑区域中。

为此,您可以创建或多或少复杂GraphicsPaths的 .

这是一个代码示例:

将剪辑坐标放入 a Rectangleor后,GraphicsPath clip您可以编写如下内容:

e.Graphics.SetClip(clip);

using (Bitmap bitmap = new Bitmap(ClientSize.Width, ClientSize.Height))
{
    using (Graphics G = Graphics.FromImage(bitmap))
            G.CopyFromScreen(dstPoint, srcPoint, 
                             copySize, CopyPixelOperation.SourceCopy);
    e.Graphics.DrawImage(bitmap, 0, 0);
}
于 2015-02-15T12:59:18.710 回答
0

难道不应该

 e.Graphics.Clip = myRegion;
于 2015-02-15T11:09:39.100 回答