[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 a tool that creates PNG files with transparent backgrounds in C#

[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 control'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 class's GetPixel method to get the color of the clicked pixel. It then calls the Bitmap object'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 newly 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 object's Save method, passing it the parameter Png to indicate that it should save the Bitmap in a PNG file.

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

Note that image formats such as GIF and most JPGs sometimes smoothly shade areas that should be a 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.

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

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