ImageAttributes::ClearColorKey method (gdiplusimageattributes.h)
The ImageAttributes::ClearColorKey method clears the color key (transparency range) for a specified category.
Syntax
Status ClearColorKey(
[in, optional] ColorAdjustType type
);
Parameters
[in, optional] type
Type: ColorAdjustType
Element of the ColorAdjustType enumeration that specifies the category for which the color key is cleared. The default value is ColorAdjustTypeDefault.
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
An ImageAttributes object maintains color and grayscale settings for five adjustment categories: default, bitmap, brush, pen, and text. For example, you can specify one color key for the default category, a different color key for the bitmap category, and still a different color key for the pen category.
The default color- and grayscale-adjustment settings apply to all categories that don't have adjustment settings of their own. For example, if you never specify any adjustment settings for the pen category, then the default settings apply to the pen category.
As soon as you specify a color- or grayscale-adjustment setting for a certain category, the default adjustment settings no longer apply to that category. For example, suppose you specify a default color key that makes any color with a red component from 200 through 255 transparent and you specify a default gamma value of 1.8. If you set the color key of the pen category by calling ImageAttributes::SetColorKey, then the default color key and the default gamma value will not apply to pens. If you later clear the pen color key by calling ImageAttributes::ClearColorKey, the pen category will not revert to the default color key; rather, the pen category will have no color key. Similarly, the pen category will not revert to the default gamma value; rather, the pen category will have no gamma value.
Examples
The following example creates an Image object from a .emf file. The code also creates an ImageAttributes object. The first call to ImageAttributes::SetColorKey sets the default color key of the ImageAttributes object so that colors with a red component from 80 through 120 are transparent. The second call to ImageAttributes::SetColorKey sets the pen color key of the ImageAttributes object so that all colors with a red component from 135 through 175 are transparent.
The code calls DrawImage once to draw the image with no color adjustment. Then the code calls DrawImage three more times, each time passing the address of the Image object and the address of the ImageAttributes object. The second time the image is drawn (after the call that sets the default color key), all of the red from 80 through 120 is transparent. The third time the image is drawn (after the call that sets the pen color key), all of the red from 135 through 175 that is drawn with a pen is transparent. Also, all of the red from 80 through 120 that is not drawn with a pen is transparent. The fourth time the image is drawn (after the call to ImageAttributes::ClearColorKey), none of the red drawn with a pen is transparent. Also, all of the red from 80 through 120 that is not drawn with a pen is transparent.
VOID Example_SetClearColorKey(HDC hdc)
{
Graphics graphics(hdc);
Image image(L"TestMetafile5.emf");
ImageAttributes imAtt;
// Draw the image (metafile) using no color adjustment.
graphics.DrawImage(
&image,
Rect(0, 0, image.GetWidth(), image.GetHeight()), // dest rect
0, 0, image.GetWidth(), image.GetHeight(), // source rect
UnitPixel);
// Set the default color key.
imAtt.SetColorKey(
Color(0, 80, 0, 0),
Color(255, 120, 255, 255),
ColorAdjustTypeDefault);
// Draw the image (metafile) using default color adjustment.
// Colors with red components from 80 through 120 are transparent.
graphics.DrawImage(
&image,
Rect(0, 100, image.GetWidth(), image.GetHeight()), // dest rect
0, 0, image.GetWidth(), image.GetHeight(), // source rect
UnitPixel,
&imAtt);
// Set the pen color key.
imAtt.SetColorKey(
Color(0, 135, 0, 0),
Color(255, 175, 255, 255),
ColorAdjustTypePen);
// Draw the image (metafile) using default and pen adjustment.
// Colors drawn with a pen that have red components from 135 through 175
// are transparent. Colors not drawn with a pen that have red components
// from 80 to 120 are transparent.
graphics.DrawImage(
&image,
Rect(0, 200, image.GetWidth(), image.GetHeight()), // dest rect
0, 0, image.GetWidth(), image.GetHeight(), // source rect
UnitPixel,
&imAtt);
// Clear the pen color key.
imAtt.ClearColorKey(ColorAdjustTypePen);
// Draw the image (metafile) using only default color adjustment.
// No colors drawn with a pen are transparent. Colors not drawn with
// a pen that have red components from 80 to 120 are transparent.
graphics.DrawImage(
&image,
Rect(0, 300, image.GetWidth(), image.GetHeight()), // dest rect
0, 0, image.GetWidth(), image.GetHeight(), // source rect
UnitPixel,
&imAtt);
}
The preceding code, along with a particular file, TestMetafile5.png, produced the following output. The bars in the left column were drawn with a pen, and the bars in the right column were filled with a brush. The default color key applies to the bars filled with a brush. The color key that applies to the bars drawn with a pen varies according to the ImageAttributes::SetColorKey and ImageAttributes::ClearColorKey calls.
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 | gdiplusimageattributes.h (include Gdiplus.h) |
Library | Gdiplus.lib |
DLL | Gdiplus.dll |