How to Add Text to a PNG File using C#

This step-by-step guide describes how to add text to a PNG file using C#. It presents the resources to set the development environment, a list of steps to develop the application, and a runnable sample code to add text to a PNG using C#. You will learn different options to customize the rendered text as per the application requirements.

Steps to Add Text to PNG Image using C#

  1. Set the environment to use Aspose.Drawing for .NET to draw text
  2. Create a Bitmap object and set its size
  3. Create a Graphics class object using the newly created Bitmap object
  4. Define the text contents, the Brush object, and the Font object
  5. Define the rectangle for drawing the text in the bitmap
  6. Call the Graphics.DrawString() method to render text on the bitmap
  7. Save the PNG file on the disk

These steps summarize the process to write text on PNG image using C#. The Graphics class object initialized from a Bitmap is mainly used as it provides methods for drawing string using the defined text, the text font, the brush for color, and the rectangle within which text is to be rendered. Finally, the Bitmap object is saved as PNG on the disk using the Save() method in the Bitmap class.

Code to Add Text to PNG File using C#

The above sample code demonstrates the process to add text on PNG using C#. You may customize the text using different options for instance use the Graphics.Clear(Color.Green) method to set the Green background color, setting the Graphics.TextRenderingHint property to TextRenderingHint.AntiAliasGridFit for controlling anti-aliasing, and using Graphics.DrawRectangle() method to draw a rectangle around the text. Text color is set using the Brush color, rectangle color using the Pen class color, and background color using the Clear() method.

This article has guided us to add text to a PNG file using C#. If you want to learn the process of drawing different shapes, refer to the article on how to draw shapes in C#.

 English