使用两个点来截取屏幕截图

3

如何通过给定两个点来截取屏幕并创建矩形截图或其他内容?

我使用了这个链接: 如何使用两个PointF创建RectangleF?

但是它似乎没有得到我想要的矩形截图,而是把屏幕的角落给我了。

    private void KListener_KeyDown(object sender, RawKeyEventArgs args)
    {
        if (args.Key.ToString() == "F5")
        {
            Program.FirstPos = System.Windows.Forms.Cursor.Position;
            System.Media.SystemSounds.Asterisk.Play();
        }
        else if (args.Key.ToString() == "F6")
        {
            Program.SecondPos = System.Windows.Forms.Cursor.Position;
            System.Media.SystemSounds.Asterisk.Play();
        }
    }

    public Bitmap CaptureScreen()
    {
        RectangleF rect2 = GetRectangle(Program.FirstPos, Program.SecondPos);
        var image = new Bitmap((int)rect2.Width, (int)rect2.Height, PixelFormat.Format24bppRgb);
        var gfx = Graphics.FromImage(image);
        gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        image.Save("C:\\Users\\DreTaX\\Documents\\teszt", ImageFormat.Jpeg);
        return image;
    }

你有考虑到坐标是基于屏幕左上角吗?此外,展示你所写的代码将增加获得帮助的机会。 - Jacobr365
就是这样了。我所做的就是保存两个点(它们是不同的,我已经测试过),然后使用该方法生成一个图像。 - DreTaX
不要认为这会改变任何事情,但是你的图像变量是位图,所以它可以是“Bitmap image = ...”,而gfx的类型是Graphics,因此它可以是“Graphics gfx = ...”,在运行代码时没有使用var的理由。当您设置rect2时,矩形位置的值是什么? - Jacobr365
gfx.CopyFromScreen(rect2.Left, rect2.Top, 0, 0, image.Size, CopyPixelOperation.SourceCopy); 请参见此处:https://dev59.com/oXA75IYBdhLWcg3wboUz - Jacobr365
@Jacobr365,无论我使用内置类型与否都没有关系。我将在一秒钟内检查它。顺便说一下,这是我得到的图像:https://db.tt/GHmyfJ0S 因此,第二个位置是完美的,但是第一个坐标却被反转了。它正在反射。 - DreTaX
这行代码可行,干杯!将其作为答案推送。 - DreTaX
1个回答

1

gfx.CopyFromScreen(rect2.Left, rect2.Top, 0, 0, image.Size, CopyPixelOperation.SourceCopy);

从屏幕中复制像素到图像对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接