GetPixel Function

Declare Function GetPixel Lib "gdi32.dll" (ByVal hdc As Long, ByVal nXPos As Long, ByVal nYPos As Long) As Long

Platforms

Description & Usage

GetPixel determines the color of a specific pixel on a device. The function finds the RGB value of the pixel which is checked.

Return Value

If an error occured, the function returns &HFFFF. If successful, the function returns the RGB value of the pixel which was checked.

Visual Basic-Specific Issues

None.

Parameters

hdc
A handle to a device context to the device to check a pixel of.
nXPos
The x-coordinate of the pixel to determine the color of.
nYPos
The y-coordinate of the pixel to determine the color of.

Example

' This code is licensed according to the terms and conditions listed here.

' Fill all of window Form1 with the color which is located at
' coordinate (75,100).  Fill the window by filling its rectangle using a
' brush derived from the color of the pixel.
Dim rgbVal As Long  ' RGB color of the pixel
Dim hBrush As Long  ' handle to the brush we'll create
Dim winrect As RECT  ' rectangle of Form1
Dim retval As Long  ' return value

' Determine the color of the pixel on Form1 at (75,100).
rgbVal = GetPixel(Form1.hDC, 75, 100)

' Get the dimensions of the rectangle of window Form1.
retval = GetWindowRect(Form1.hWnd, winrect)
' Create a solid brush of that color.
hBrush = CreateSolidBrush(rgbVal)
' Use that brush to fill in all of Form1 (its entire rectangle).
retval = FillRect(Form1.hDC, winrect, hBrush)
' Delete the brush we created to save resources.
retval = DeleteObject(hBrush)

See Also

SetPixel, SetPixelV

Category

Bitmaps

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: September 2, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getpixel.html