如何在图片框中获取屏幕的位置

时间:2021-05-19 09:00:41

I have a problem to copy a picturebox at runtime that need the current position of the screen with respect to picturebox

我有一个问题是在运行时复制一个需要屏幕当前位置的图片框

In this line of code took the position of the picturebox but I need the position with respect to the screen. Is there any way we could do?

在这行代码中占据了图片框的位置,但我需要相对于屏幕的位置。我们有什么办法吗?

// gfxScreenshot.CopyFromScreen(pictureBox1.Bounds.X, pictureBox1.Bounds.Y, 0, 0, pictureBox1.Bounds.Size, CopyPixelOperation.SourceCopy);

 if (saveScreenshot.ShowDialog() == DialogResult.OK)
    {

       bmpScreenshot = new Bitmap(pictureBox1.Bounds.Width, pictureBox1.Bounds.Height, PixelFormat.Format32bppArgb);

       gfxScreenshot = Graphics.FromImage(bmpScreenshot);

       gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

       bmpScreenshot.Save(saveScreenshot.FileName, ImageFormat.Png);

    }

1 个解决方案

#1


3  

To get the screen coordinates of a position of the picture box itself, use:

要获取图片框本身位置的屏幕坐标,请使用:

var screenPosition = form.PointToScreen(pictureBox1.Location);

To get the screen coordinates of a point within the picture box:

要获取图片框中某点的屏幕坐标:

var screenPosition = pictureBox1.PointToScreen(new Point(10, 10));

That will give you the screen coordinates of the position (10, 10) within the picture box.

这将为您提供图片框中位置(10,10)的屏幕坐标。

#1


3  

To get the screen coordinates of a position of the picture box itself, use:

要获取图片框本身位置的屏幕坐标,请使用:

var screenPosition = form.PointToScreen(pictureBox1.Location);

To get the screen coordinates of a point within the picture box:

要获取图片框中某点的屏幕坐标:

var screenPosition = pictureBox1.PointToScreen(new Point(10, 10));

That will give you the screen coordinates of the position (10, 10) within the picture box.

这将为您提供图片框中位置(10,10)的屏幕坐标。