draw-text-image-csharp

We see or create a lot of images for visual information. Sometimes you need to add or draw text on an image in such a manner that nobody else can modify that text. Image watermark is one of the most common examples. Likewise, labeling or reviewing images may also require drawing text. Let us explore the further uses of drawing text on images:

Add, Insert, or Draw Formatted Text on Image – C# API Installation

Aspose.Drawing for .NET API is a 2D graphics library that is compatible with System.Drawing package. It supports rendering rectangles, curves, lines, text, and other graphics onto the images. It is not dependent on any 3rd party library thus can be used in any environment. For example, it can also be used to render graphics in Linux or Azure Functions V2 where System.Drawing library is not supported. You can install Aspose.Drawing for .NET API by downloading it from the Downloads section, or via the following installation command:

PM> Install-Package Aspose.Drawing

Draw Text as Watermark on Existing Image Programmatically using C#

Text watermark on Image is often used to show ownership, affiliation, or purpose of the picture. You can add or draw text as watermark on an existing image with the following steps:

  1. Load existing input image
  2. Initialize Graphics class object
  3. Set text formatting and text
  4. Draw text
  5. Save output image

The following code shows how to draw text as watermark on existing image programmatically using C#:

// Load existing input image
Bitmap bitmap = new Bitmap(dataDir + @"Test.png");
// Initialize Graphics class object
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Brush brush = new SolidBrush(Color.FromKnownColor(KnownColor.Black));
// Declare brush while specifying color
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 1);
// Set text font
Font arial = new Font("Arial", 20, FontStyle.Regular);
// Set text
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus.";
Rectangle rectangle = new Rectangle(100, 100, 800, 600);
graphics.DrawRectangle(pen, rectangle);
// Draw text
graphics.DrawString(text, arial, brush, rectangle);
// Save output image
bitmap.Save(RunExamples.GetDataDir() + @"TextFonts\DrawText_out.png");

Draw Text on New Image from Scratch Programmatically in C#

You may need to draw some text in image format for different purposes. Like you may want to keep others from modifying or copying text contents. You need to follow the following steps for drawing text on new image:

  1. Initialize new image from scratch
  2. Initialize Brush class object
  3. Set font style, size, etc.
  4. Draw text on the image
  5. Save output image

The code snippet below demonstrates how to draw text on new image from scratch programmatically using C# language:

// Initialize new image from scratch
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.Clear(Color.FromKnownColor(KnownColor.White));
// Initialize Brush class object
Brush brush = new SolidBrush(Color.FromKnownColor(KnownColor.Black));
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 1);
// Set font style, size, etc.
Font arial = new Font("Arial", 20, FontStyle.Regular);
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus.";
Rectangle rectangle = new Rectangle(100, 100, 800, 600);
graphics.DrawRectangle(pen, rectangle);
// Draw text on the image
graphics.DrawString(text, arial, brush, rectangle);
// Save output image
bitmap.Save(RunExamples.GetDataDir() + @"TextFonts\DrawText_out.png");

Conclusion

In this article, we have learned how to add or draw text on images. You can draw text watermark in existing as well as new images from scratch without needing to install any 3rd party application. Moreover, you can process a lot of image formats including JPG, PNG, BMP, Icon, Tiff, etc. The API offers several features for processing drawings and graphics as per your requirements. You may visit the Example Project that we have designed as a showcase for different scenarios. Please feel free to contact us anytime via the Free Support Forums in case of any concerns. Cheers!

See Also

Info: Aspose recently developed a FREE Text to GIF Converter.