The following example creates an Image object based on a .bmp file and then draws the image. The code creates an
ImageAttributes object and sets its default remap table so that red is converted to blue. Then the code draws the image again using the color adjustment specified by the remap table.
C++
VOID Example_SetRemapTable(HDC hdc)
{
Graphics graphics(hdc);
// Create an Image object based on a .bmp file.
// The image has one red stripe and one green stripe.
Image image(L"RedGreenStripes.bmp");
// Create an ImageAttributes object and set its remap table.
ImageAttributes imageAtt;
ColorMap cMap;
cMap.oldColor = Color(255, 255, 0, 0); // red
cMap.newColor = Color(255, 0, 0, 255); // blue
imageAtt.SetRemapTable(12, &cMap,
ColorAdjustTypeDefault);
// Draw the image with no color adjustment.
graphics.DrawImage(&image, 10, 10, image.GetWidth(), image.GetHeight());
// Draw the image with red converted to blue.
graphics.DrawImage(&image,
Rect(100, 10, image.GetWidth(), image.GetHeight()), // dest rect
0, 0, image.GetWidth(), image.GetHeight(), // source rect
UnitPixel,
&imageAtt);
}
PowerBASIC
SUB GDIP_SetRemapTable (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pImage AS DWORD
LOCAL pImgAttr AS DWORD
LOCAL strFileName AS STRING
LOCAL cMap AS GDIP_ColorMap
LOCAL nWidth AS DWORD
LOCAL nHeight AS DWORD
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create an Image object based on a .bmp file.
' // The image has one red stripe and one green stripe.
strFileName = UCODE$("RedGreenStripes.bmp")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
' // Create an ImageAttributes object and set its remap table.
cMap.oldColor = GDIP_ARGB(255, 255, 0, 0) ' // red
cMap.newColor = GDIP_ARGB(255, 0, 0, 255) ' // blue
hStatus = GdipCreateImageAttributes(pImgAttr)
hStatus = GdipSetImageAttributesRemapTable(pImgAttr, %ColorAdjustTypeDefault, %TRUE, 1, cMap)
' // Draw the image with no color adjustment.
hStatus = GdipGetImageWidth(pImage, nWidth)
hStatus = GdipGetImageHeight(pImage, nHeight)
hStatus = GdipDrawImageRectI(pGraphics, pImage, 10, 10, nWidth, nHeight)
' // Draw the image with red converted to blue.
hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
100, 10, nWidth, nHeight, _ ' // dest rect
0, 0, nWidth, nHeight, _ ' // source rect
%UnitPixel, pImgAttr, %NULL, %NULL)
' // Cleanup
IF pImgAttr THEN GdipDisposeImageAttributes(pImgAttr)
IF pImage THEN GdipDisposeImage(pImage)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
The following illustration shows the output of the preceding code.
(http://www.jose.it-berater.org/captures/GdipSetImageAttributesRemapTable.png)