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.

        // 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 see additional details such as how the program loads and saves files.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in graphics, image processing and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *