This article guides on how to create transparent image in C#. It has the details to set the IDE, a list of steps, and a sample code for drawing pictures with transparent background in C#. You will learn to create complex shapes by adding multiple objects and customizing the output PNG image according to the requirements.
Steps to Create Transparent PNG in C#
- Set the environment to use Aspose.Drawing for .NET to create a transparent image
- Create a bitmap and use it for creating a Graphics object to enable drawing
- Clear the bitmap and set its background to transparent
- Instantiate the GraphicsPath object for drawing complex shapes
- Add circles, lines, polygons, etc. to the path
- Create a colored brush and use it to fill all the shapes
- Set the entire bitmap transparent and save the image on the disk
These steps describe how to make picture transparent in C#. Create a bitmap, generate a Graphics object from the bitmap to enable drawing, set the bitmap to be transparent, and instantiate the GraphicsPath to create complex shapes. Add as many objects as required, including circles, lines, ellipses, arcs, etc., to the path, create a brush and fill shapes with it, and finally make the whole bitmap transparent before saying it on the disk.
Code to Make Image Transparent in C#
using Aspose.Drawing; // For graphics and drawing features. | |
using Aspose.Drawing.Drawing2D; // For advanced 2D graphics and drawing tools. | |
class TransparentImageCreator // Class named "TransparentImageCreator" | |
{ | |
static void Main() // The entry point of the program | |
{ | |
new License().SetLicense("license.lic");// Avoid evaluation watermarks or limitations | |
using (Bitmap bmp = new Bitmap(340, 340)) // Create a 340x340 pixel bitmap | |
{ | |
using (Graphics g = Graphics.FromImage(bmp)) // Create a Graphics object from the bitmap | |
{ | |
g.Clear(Color.Transparent);// Clear the bitmap and set its background to transparent | |
GraphicsPath path = new GraphicsPath(); // Create a GraphicsPath object for complex shapes | |
path.AddEllipse(new RectangleF(20, 20, 100, 100));// Add a circle to the path | |
path.AddEllipse(new RectangleF(200, 200, 100, 100));// Add another circle to the path | |
using (Brush brush = new SolidBrush(Color.Blue)) // Create a SolidBrush object with blue color | |
{ | |
g.FillPath(brush, path);// Fill the defined path (both ellipses) with the blue brush | |
} | |
} | |
bmp.MakeTransparent();// Make the entire bitmap transparent | |
bmp.Save("pentagonImage.png");// Save the bitmap to a file | |
} | |
} | |
} |
This code has demonstrated the development of a transparent image maker in C#. You can add multiple shapes to the path such as Arc, Bezier, Closed curve, Line, Pie, Rectangle, and String. If you do not want to make the image transparent and set some specific color, use the Graphics.Clear() method with the desired color.
This article has taught us to draw complex transparent images. To add text to the PNG, refer to the article How to add text to a PNG file using C#.