简体   繁体   English

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

[英]How to get the position of the screen in a picturebox

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);

    }

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)的屏幕坐标。

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

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