Color Transformation Applications in C# GDI+ Programming - Pulling Out the Foreground Image Demo
(Page 2 of 4 )
In this sample, the code creates a color remap table that consists of a single ColorMap structure. The oldColor member of the ColorMap structure is blue and the newColor member is white. The image is drawn once without remapping and once with remapping. This will help us indirectly pull the foreground image out of the original one. The remapping process changes all the blue pixels to white.
Take a look at the running-time snapshot, as is shown in Figure 1.
Figure 1—the running-time snapshot
_html_279b40b0.png)
Apparently, the right image in Figure 1 corresponds to the one in which the blue background is cleared away after the color remapping.
Look more carefully at the associated programming:
Graphics graphics = this.CreateGraphics();
graphics.Clear(Color.White);
//load the original image with blue background
Bitmap image = new Bitmap("Nemo_Blue.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
//substitute blue for white to achieve the expected aim
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 0, 255);
colorMap.NewColor = Color.FromArgb(255, 255, 255, 255);
//set up color remapping table
ColorMap[] remapTable = { colorMap };
//set up the color info for the image
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
//render the source image
graphics.DrawImage(image, 50, 50, width, height);
//render the new image with the background color weeded out
graphics.DrawImage(image,
new Rectangle(width + 80, 50, width, height), //target rectangle
0, 0, // upper left corner of the source image
width, // width of the source image
height, // height of the source image
GraphicsUnit.Pixel,
//color info
imageAttributes);
Note that we’ve only built one ColorMap object, meaning we only execute the color remapping once. So, if you want to eleminate other colors you have to define more ColorMap objects and continue to apply the related color remapping.
With the color remapping application discussed, let’s take a look at another use of color transformation — isolating the color channel.
Next: About the Color Channel >>
More C# Articles
More By Xianzhong Zhu