Bitmap.GetPixel method

Applies to: desktop apps only

The Bitmap::GetPixel method gets the color of a specified pixel in this bitmap.

Syntax

Status GetPixel(
  [in]   INT x,
  [in]   INT y,
  [out]  Color *color
);

Parameters

x [in]

Type: INT

Integer that specifies the x-coordinate (column) of the pixel.

y [in]

Type: INT

Integer that specifies the y-coordinate (row) of the pixel.

color [out]

Type: Color*

Pointer to a Color object that receives the color of the specified pixel.

Return value

Type:

Type: Status

If the method succeeds, it returns Ok, which is an element of the Status enumeration.

If the method fails, it returns one of the other elements of the Status enumeration.

Remarks

Depending on the format of the bitmap, Bitmap::GetPixel might not return the same value as was set by Bitmap::SetPixel. For example, if you call Bitmap::SetPixel on a Bitmap object whose pixel format is 32bppPARGB, the pixel's RGB components are premultiplied. A subsequent call to Bitmap::GetPixel might return a different value because of rounding. Also, if you call Bitmap::SetPixel on a Bitmap object whose color depth is 16 bits per pixel, information could be lost during the conversion from 32 to 16 bits, and a subsequent call to Bitmap::GetPixel might return a different value.

Examples

The following example creates a Bitmap object based on a JPEG file. The code calls the Bitmap::GetPixel method to obtain the color of a pixel in the bitmap and then fills a rectangle with the retrieved color.


VOID Example_GetPixel(HDC hdc)

{

   Graphics graphics(hdc);

   // Create a Bitmap object from a JPEG file.
   Bitmap myBitmap(L"Climber.jpg");

   // Get the value of a pixel from myBitmap.
   Color pixelColor;
   myBitmap.GetPixel(25, 25, &pixelColor);

   // Fill a rectangle with the pixel color.
   SolidBrush brush(pixelColor);
   graphics.FillRectangle(&brush, Rect(0, 0, 100, 100));
}

Requirements

Minimum supported client

Windows XP, Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Product

GDI+ 1.0

Header

Gdiplusheaders.h (include Gdiplus.h)

Library

Gdiplus.lib

DLL

Gdiplus.dll

See also

Bitmap
Color
Image
Bitmap::LockBits
Bitmap::SetPixel
Using Images, Bitmaps, and Metafiles
Images, Bitmaps, and Metafiles

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.