Make a tool that creates PNG files with transparent backgrounds in C#

When you use the File menu's Open command, the following code lets you select a image file.

// The image.
private Bitmap Bm = null;

// Offset for displaying the image.
private const int Offset = 10;

// Open a file.
private void mnuFileOpen_Click(object sender, EventArgs e)
{
    if (ofdFile.ShowDialog() == DialogResult.OK)
    {
        try
        {
            Bm = new Bitmap(ofdFile.FileName);
            picImage.ClientSize = new Size(
                Bm.Width + 2 * Offset,
                Bm.Height + 2 * Offset);
            picImage.Visible = true;
            picImage.Refresh();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

This code displays an OpenFileDialog. If the user selects a file and clicks Open, the code sets Bm to a new Bitmap created from the file.

The program then sizes the picImage PictureBox control to fit the Bitmap plus a margin. When the code then refreshes the PictureBox, the following Paint event handler executes.

// Draw the picture.
private void picImage_Paint(object sender, PaintEventArgs e)
{
    if (Bm == null) return;
    e.Graphics.DrawImage(Bm, Offset, Offset);
}

At design time, I set the PictureBox's background image to the blue cloud design shown in the picture. The Paint event handler draws the Bitmap on top of the PictureBox, leaving a margin around the picture so you can see the background.

If you click on the picture, the following code makes the pixels with the clicked color transparent.

// Set the transparent pixel.
private void picImage_MouseClick(object sender, MouseEventArgs e)
{
    // Get the color clicked.
    Color color = Bm.GetPixel(e.X - Offset, e.Y - Offset);

    // Make that color transparent.
    Bm.MakeTransparent(color);

    // Show the result.
    picImage.Refresh();
}

This code uses the Bitmap's GetPixel method to get the color of the clicked pixel. It then calls the Bitmap's MakeTransparent method to make all pixels of that color transparent. Finally the code refreshes the PictureBox so you can see the image with its transparent pixels.

The last piece of the program executes when you select the File menu's Save command.

// Save the file.
private void mnuFileSave_Click(object sender, EventArgs e)
{
    if (sfdFile.ShowDialog() == DialogResult.OK)
    {
        try
        {
            Bm.Save(sfdFile.FileName, ImageFormat.Png);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

This code displays a SaveFileDialog. If the user selects a file name and clicks Save, the program calls the Bitmap's Save method, passing it the parameter Png to indicate that it should save the Bitmap in a PNG file. (The program includes a "using System.Drawing.Imaging" statement so the code doesn't have to say "System.Drawing.Imaging.ImageFormat.Png.")

The program only saves in the PNG format because that's the only format it can use that saves transparent pixels. (It's also provides decent compression.)

Note that image formats such as GIF and most JPGs sometimes smoothly shade areas that should be solid color so if you load them into this program, you may have trouble clicking on all of the pixels that you want to make transparent.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.