The following example creates a
Bitmap object based on a JPEG file. The code calls the
GdipBitmapGetPixel function to obtain the color of a pixel in the bitmap and then fills a rectangle with the retrieved color.
C++
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));
}
PowerBASIC
SUB GDIP_GetPixel (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pBitmap AS DWORD
LOCAL pSolidBrush AS DWORD
LOCAL strFileName AS STRING
LOCAL pixelColor AS DWORD
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a Bitmap object from a JPEG file.
strFileName = UCODE$("climber.jpg")
hStatus = GdipCreateBitmapFromFile(STRPTR(strFileName), pBitmap)
' // Get the value of a pixel from myBitmap.
hStatus = GdipBitmapGetPixel(pBitmap, 10, 10, pixelColor)
' // Fill a rectangle with the pixel color.
hStatus = GdipCreateSolidFill(pixelColor, pSolidBrush)
hStatus = GdipFillRectangleI(pGraphics, pSolidBrush, 0, 0, 100, 100)
' // Cleanup
IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
IF pBitmap THEN GdipDisposeImage(pBitmap)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipBitmapGetPixel.png)