[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 an image with rounded corners and a transparent background in C#

[Make an image with rounded corners and a transparent background in C#]

Recently I wanted to make an image for a gift card that had rounded corners. Unfortunately the Graphics class does not have a method to draw rectangles with rounded corners. Fortunately, I have already described the key method in my earlier post Draw rounded rectangles in C#. That post describes a MakeRoundedRect method that creates a GraphicsPath representing a rectangle that has rounded corners. You can then draw or fill it as needed.

This example uses the following code to create and display an image with rounded corners and a transparent background.

// Make and display the image with rounded corners. private void ShowImage() { // If the corners are not rounded, // just use the original image. if ((scrXRadius.Value == 0) || (scrYRadius.Value == 0)) { picImage.Image = OriginalImage; return; } // Make a bitmap of the proper size. int width = OriginalImage.Width; int height = OriginalImage.Height; Bitmap bm = new Bitmap(width, height); using (Graphics gr = Graphics.FromImage(bm)) { // Clear with a transparent background. gr.Clear(Color.Transparent); gr.SmoothingMode = SmoothingMode.AntiAlias; gr.InterpolationMode = InterpolationMode.High; // Make the rounded rectangle path. GraphicsPath path = MakeRoundedRect( new Rectangle(0, 0, width, height), scrXRadius.Value, scrYRadius.Value, true, true, true, true); // Fill with the original image. using (TextureBrush brush = new TextureBrush(OriginalImage)) { gr.FillPath(brush, path); } } picImage.Image = bm; }

As you can probably guess, the original image is stored in the variable OriginalImage.

The scrXRadius and scrYRadius scroll bars let the user select the radii used to draw the rounded corners. If either the X or Y radii of the rectangle's rounded corners are zero, then the result should not have rounded corners. In that case, the method simply displays the original image and returns.

If the X and Y radii are not zero, the method gets the image's dimensions, makes a Bitmap of the same size, and creates an associated Graphics object. It clears the bitmap with the color Transparent and sets the InterpolationMode to AntiAliass so any shapes that it draws have smooth, anti-aliased edges.

Next, the code calls the MakeRoundedRect method to get a path representing a rectangle with rounded corners. The method then creates a TextureBrush that fills with copies of the original image. It uses the brush to fill the path that defines the rounded rectangle.

The code finishes by displaying the new image in the picImage PictureBox.

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

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