Capturing the Pixel Color at the Cursor Position Doesn't Return Correct Color on Different DPI Monitors with Multiple Monitor Setup

Karthick Arjunan 0 Reputation points
2024-12-02T07:16:57.55+00:00

Hello,

In the provided sample, we are trying to capture the pixel color at the cursor position. This works correctly in a single monitor setup and in multiple monitors with the same DPI settings. However, when used in a multi-monitor setup with different screen DPI settings, the color captured is incorrect and does not match the color at the cursor's position. We have attached the sample in the below mentioned link.

Sample Link: Sample

**
Steps to Reproduce:**

  1. Run the attached sample in a multiple monitor setup with different DPI settings.
  2. Click on the EyeDropper icon and drag the cursor to get the color at the cursor's position.
  3. Drag the cursor over the color boxes with various colors to check the captured color, which is displayed in the selected color box. The color picking works correctly at this stage.
  4. Move the application window to another screen with a different DPI setting and repeat the process.
  5. Observe that the selected color does not match the actual color where the cursor is placed.

Expected Behavior: The color at the cursor's position should be accurately captured irrespective of the monitor's DPI setting.

Actual Behavior: The color captured is incorrect when the application window is on a monitor with a different DPI setting than the primary monitor.

Thank you!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,007 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,804 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 118.6K Reputation points
    2024-12-02T11:54:09.1266667+00:00

    Try to right-click the project in Solution Explorer, select Add, New Item, and add a new “Application Manifest File (Windows Only)”.

    It will contain a commented section that includes a <dpiAware> element. Uncomment it, and add the <dpiAwareness> element. It will look like this:

    <application xmlns="urn:schemas-microsoft-com:asm.v3">
        <windowsSettings>
            <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
            <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
            <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
        </windowsSettings>
    </application>
    

    See also:

    The <longPathAware> element, if exists, can be kept too.

    It is probably possible to avoid external functions like GetCursorPos, GetPixel if the pixels are always got from your image.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.