[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Make light pixels transparent in an image in C#

[Make light pixels transparent in an image in C#]

Recently I had an image and I wanted to make all of the light pixels transparent. Ideally I could simply make the white pixels transparent, but the image was scanned so few of the pixels were pure white. What I really needed to do was make pixels that were mostly white transparent.

This example lets you load an image. When you adjust the scroll bar, it finds the pixels with red, green, and blue color components that are all greater than the cutoff value and it makes them transparent.

The following ShowImage method creates and displays the adjusted image.

// Make an image setting pixels brighter // than the cutoff value to magenta. private void ShowImage() { if (OriginalImage == null) return; // Get the cutoff. int cutoff = scrBrightness.Value; // Prepare the ImageAttributes. Color low_color = Color.FromArgb(cutoff, cutoff, cutoff); Color high_color = Color.FromArgb(255, 255, 255); ImageAttributes image_attr = new ImageAttributes(); image_attr.SetColorKey(low_color, high_color); // Make the result image. int wid = OriginalImage.Width; int hgt = OriginalImage.Height; Bitmap bm = new Bitmap(wid, hgt); // Process the image. using (Graphics gr = Graphics.FromImage(bm)) { // Fill with magenta. gr.Clear(Color.Magenta); // Copy the original image onto the result // image while using the ImageAttributes. Rectangle dest_rect = new Rectangle(0, 0, wid, hgt); gr.DrawImage(OriginalImage, dest_rect, 0, 0, wid, hgt, GraphicsUnit.Pixel, image_attr); } // Display the image. picResult.Image = bm; }

If no image has yet been loaded, the method simply returns. If it continues, the gets the cutoff value from the scroll bar scrBrightness.

Next the method creates two colors low_color and high_color to represent the lowest and highest color values that it will convert to transparent. It creates a new ImageAttributes object and calls its SetColorKey method to give it the low and high color values.

Nxet the program creates a Bitmap with the same size as the original image. It creates an associated Graphics object and clears it with the color magenta. At this point the new image is completely magenta.

Next the code uses the Graphics object's DrawImage method to draw the original image on top of the magenta background. The final parameter to DrawImage is the ImageAttributes object.

If a pixel's red, green, and blue color components are all between the corresponding values of the low and high colors saved in the ImageAttributes object's color key, the result pixel is transparent. Because the bitmap's background is magenta, the magenta shows through at these pixels. (I use magenta instead of allowing those pixels to be transparent so you can see them easily.)

The method finishes by displaying the resulting bitmap.

When you use the File menu's Save command, the program copies the result bitmap, converts the magenta pixels to transparent, and saves the result.

Note that the PNG file format supports transparency but most other formats such as BMP and JPG do not. For that reason, be sure to save the file in PNG format.

Note also that there are other ways you can set the pixel transparency. For example, you can use the Bitmap object's GetPixel and SetPixel methods or you can use the Bitmap32 class that I've used in other posts. The ImageAttributes object is much faster.

For the best results if you will later display the image on a light-colored background, adjust the cutoff value so a few of the pixels near the dark shapes are not transparent. You can see them in the picture above as the light-colored pixels between the dark shapes and the magenta background. Those pixels will provide a smooth edge between the shapes and the background.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.