ProgrammingRookie
ProgrammingRookie

Reputation: 53

Color at mouse location is incorrect

Public Class Form1
Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
Dim BMP As New Bitmap(1, 1)
Dim G As Graphics = Graphics.FromImage(BMP)
Dim XCoorLabel As New Label
Dim YCoorLabel As New Label
Dim ColorLabel As New Label
Dim ColorExam As New Panel

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Preparing the Form
    Me.Width = 500
    Me.Height = 150
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
    'Setting up all the labels and 1 panel
    With XCoorLabel
        .Location = New Point(12, 9)
        .Name = "xCoorlabel"
        .Size = New Drawing.Size(50, 25)
        .Visible = True
        .ForeColor = Color.DarkGray
    End With
    With YCoorLabel
        .Location = New Point(69, 9)
        .Name = "YCoorLabel"
        .Size = New Drawing.Size(50, 25)
        .Visible = True
        .ForeColor = Color.DarkGray
    End With
    With ColorLabel
        .Location = New Point(12, 36)
        .Name = "ColorLabel"
        .Size = New Drawing.Size(200, 25)
        .Visible = True
        .ForeColor = Color.DarkGray
    End With
    With ColorExam
        .Location = New Point(12, 66)
        .Name = "ColorLabel"
        .Size = New Drawing.Size(20, 10)
        .Visible = True
    End With
    'Adding everything to the form
    Me.Controls.Add(XCoorLabel)
    Me.Controls.Add(YCoorLabel)
    Me.Controls.Add(ColorLabel)
    Me.Controls.Add(ColorExam)
    'Starting the timer
    Timer1.Enabled = True
End Sub
Private Sub keyPressed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    'Stopping the timer when 'W' is pressed
    If e.KeyValue = Keys.W Then
        Timer1.Enabled = False
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Getting the cursor position and place it in the text label
    Dim mousepos As Point = Cursor.Position
    XCoorLabel.Text = "X" & mousepos.X
    YCoorLabel.Text = "Y" & mousepos.Y
    'Getting the screenimage and checking what color is on the location
    G.CopyFromScreen(mousepos, Point.Empty, BMP.Size)
    'placing the RGB color values in text in the label
    ColorLabel.Text = BMP.GetPixel(0, 0).ToString
    'Change the panels color according to the color that was found on the position
    Dim Icolor As Integer = GetPixel(GetDC(0), mousepos.X, mousepos.Y)
    ColorExam.BackColor = System.Drawing.ColorTranslator.FromOle(Icolor)
    End Sub
End Class

THE CODE CAN BE PASTED INTO VISUAL STUDIO 2013, AND WILL FUNCTION IMMEDIATLY (No need to add any labels or stuff yourself to see it function, altering the code is allowed obviously)

Above here is my code. I wanted to try some stuff with finding the current mouse location, and which color is at that location (Not just from the background, from the screen itself).

The thing is, that this code actually works (might not be the best code, but it does function). Sadly I have the problem that when I hover the mouse over a certain place, the color it shows does not match, when I checked this, I noticed that it is off.

In X it seems to be about 400-500 pixels off

In Y it seems to be about 90 pixels off

Does anybody have any idea where this comes from, and how I could fix this?

Upvotes: 0

Views: 110

Answers (1)

Daniel
Daniel

Reputation: 1

I had the same issue. I think it is because of DPI scaling. Set the scale to 100% and you will probably get the correct color at the cursor position

Upvotes: 0

Related Questions