marduck19
marduck19

Reputation: 54

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

    }

Upvotes: 0

Views: 2988

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 133975

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.

Upvotes: 4

Related Questions