S
S
Sidik2017-05-29 18:34:11
Windows
Sidik, 2017-05-29 18:34:11

How to get mouse cursor coordinates with multiple monitors?

When the user has multiple monitors, it is not possible to place the mouse cursor correctly on the screenshot.
The cursor is drawn relative to the main screen, but if the user has the main screen in the middle, or somewhere in the far right corner, then the cursor is placed incorrectly.
Please tell me how to correctly determine the main screen and correctly place the cursor on the screenshot?
Here is a piece of code

namespace Screenshot
{
    public partial class Form1 : Form
    {       
        public Form1()
        {
            InitializeComponent();
            CaptureDesktop();            
        }

        private static void CaptureDesktop()
        {
            Rectangle desktopRect = GetDesktopBounds();
            using (Bitmap bitmap = new Bitmap(desktopRect.Width, desktopRect.Height))
            {
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    graphics.CopyFromScreen(desktopRect.Location, Point.Empty, bitmap.Size);
                    graphics.DrawImage(Image.FromFile("cursor.png"), Cursor.Position.X, Cursor.Position.Y);                   
                    bitmap.Save("C:\\screenshots\\Test.jpeg");                    
                }
            }
        }

        private static Rectangle GetDesktopBounds()
        {
            Rectangle result = new Rectangle();
            foreach (Screen screen in Screen.AllScreens)
            {
                result = Rectangle.Union(result, screen.Bounds);
            }
            return result;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2017-05-29
@Sidik_D

At the main screen, the coordinates of the upper left corner are (0, 0).
When the main screen is not at the top and left, then negative coordinates appear.
To convert screen coordinates to image coordinates, you need to subtract desktopRect.Left and desktopRect.Top

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question