Bitmap::SetPixel method (gdiplusheaders.h)

The Bitmap::SetPixel method sets the color of a specified pixel in this bitmap.

Syntax

Status SetPixel(
  [in]      INT           x,
  [in]      INT           y,
  [in, ref] const Color & color
);

Parameters

[in] x

Type: INT

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

[in] y

Type: INT

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

[in, ref] color

Type: const Color

Reference to a Color object that specifies the color to set.

Return value

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 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 whose color depth is 16 bits per pixel, information could be lost in 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 draws the bitmap once unaltered. Then the code calls the Bitmap::SetPixel method to create a checkered pattern of black pixels in the bitmap and draws the altered bitmap.

VOID Example_SetPixel(HDC hdc)

{
   Graphics graphics(hdc);

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

   // Draw the bitmap.
   graphics.DrawImage(&myBitmap, 0, 0);

   // Create a checkered pattern with black pixels.
   for (UINT row = 0; row < myBitmap.GetWidth(); row += 2)
   {
      for (UINT col = 0; col < myBitmap.GetHeight(); col += 2)
      {
         myBitmap.SetPixel(row, col, Color(255, 0, 0, 0));
      }
   }

   // Draw the altered bitmap.
   graphics.DrawImage(&myBitmap, 200, 0);
}

Requirements

Requirement Value
Minimum supported client Windows XP, Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header gdiplusheaders.h (include Gdiplus.h)
Library Gdiplus.lib
DLL Gdiplus.dll

See also

Bitmap

Bitmap::GetPixel

Color

Image

Images, Bitmaps, and Metafiles

Using Images, Bitmaps, and Metafiles