// Create an Image object based on a BMP file.
// The image has three horizontal stripes.
// The color of the top stripe has RGB components (90, 90, 20).
// The color of the middle stripe has RGB components (150, 150, 150).
// The color of the bottom stripe has RGB components (130, 130, 40).
Image image(L"ColorKeyTest.bmp");
// Create an ImageAttributes object, and set its color key.
ImageAttributes imAtt;
// 如果只有一种颜色,可以设置两个完全相同的颜色作为范围
imAtt.SetColorKey(
Color(100, 95, 30),
Color(250, 245, 60),
ColorAdjustTypeBitmap);
// Draw the image. Apply the color key.
// The bottom stripe of the image will be transparent because
// 100 <= 130 <= 250 and
// 95 <= 130 <= 245 and
// 30 <= 40 <= 60.
graphics.DrawImage(
&image,
Rect(20, 20, image.GetWidth(), image.GetHeight()), // dest rect
0, 0, image.GetWidth(), image.GetHeight(), // source rect
UnitPixel,
&imAtt);
}