Jalaj

February 14, 2007

WinAPI : GetPixel Function

Filed under: Application, Visual Basic, WinAPI — Jalaj @ 7:24 am

In previous post WinAPI : SetPixel Function we used SetPixel function to draw over a particular point with a particular color.

Similarly GetPixel function can be used to get the Color of the pixel at a given point.

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, _
    ByVal X As Long, ByVal Y As Long) As Long

The first parameter that the function takes is the handle to the device context, the next two parameters the X and Y co-ordinates of the point.

Let’s implement a palette system in our previous project. We should be able to select a color from a palette.

What about using Shapes to draw a palette? No we would not use it for few reasons, that the shapes are not true windows so will not fire events, the shapes do not have their own Device Contexts and are drawn over the device context of the parent window. Thus if not properly checked programmatically over pen can draw over the palette itself too.

Then let’s have palette prepared as a bitmap which we can show using the PictureBox or Image control.

For reason similar to Shapes Image control too is not appropriate for using for our specific purpose then let’s add a PictureBox and add a bitmap showing the palette.

Let a public variable lngPenColor save the current color, which we aill initialize to Black on Load event of the form.

Dim lngPenColor As Long

Private Sub Form_Load()

    lngPenColor = RGB(0, 0, 0)

End Sub

Then, change the code within MouseMove event of the form to draw using the color as saved with lngPenColor.

Private Sub Form_MouseMove(Button As Integer, _
    Shift As Integer, X As Single, Y As Single)

        SetPixel Form1.hdc, X / Screen.TwipsPerPixelX, _
            Y / Screen.TwipsPerPixelX, lngPenColor

End Sub

To get the pixel color on palette add the GetPixel function in the MouseDown event of the PictureBox which will also provide us the co-ordinates of the present cursor position.

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    lngPenColor = GetPixel(Picture1.hdc, X / Screen.TwipsPerPixelX, _
            Y / Screen.TwipsPerPixelX)

End Sub

getpixel.jpg

That’s all for getting started to paint in color. The complete code is reproduced below.

Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, _
    ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, _
    ByVal X As Long, ByVal Y As Long) As Long

Dim lngPenColor As Long

Private Sub Form_Load()

    lngPenColor = RGB(0, 0, 0)

End Sub

Private Sub Form_MouseMove(Button As Integer, _
    Shift As Integer, X As Single, Y As Single)

        SetPixel Form1.hdc, X / Screen.TwipsPerPixelX, _
            Y / Screen.TwipsPerPixelX, lngPenColor

End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    lngPenColor = GetPixel(Picture1.hdc, X / Screen.TwipsPerPixelX, _
            Y / Screen.TwipsPerPixelX)

End Sub

1 Comment »

  1. [...] color associated with the device context, and which we already discussed it in one of previous post WinAPI : GetPixel Function. However, the function is supported only on Windows 200 and later so you cannot use the function if [...]

    Pingback by Pen Object in Device Context « Jalaj — February 26, 2007 @ 7:45 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.