Wednesday, December 17, 2008

[vb6] Retrieve pixel color with GetPixel

The color of a pixel on a form or picturebox can be read with the GetPixel API. The function expects X and Y in pixels, so it is necessary to convert from whatever scalemode is in effect to pixels. The code below reads the pixel values from a picturebox when a mouse button is pressed and uses the ScaleX and ScaleY methods of the picturebox control to make the call to GetPixel independent of the scalemode property.
    Private Declare Function GetPixel Lib "gdi32" _
(ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long

Private Sub Pict1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Debug.Print GetPixel(Pict1.hdc, Pict1.ScaleX(X, _
Pict1.ScaleMode, 3), Pict1.ScaleY(Y, Pict1.ScaleMode, 3))
End Sub

1 comment:

  1. i want to read pixel from image...like omr and want to know weather it is black or white?

    ReplyDelete