Write Text on JPG in C#

This article guides on how to write text on JPG in C#. It has the IDE settings, a list of steps, and a detailed sample code demonstrating how to add text to a JPG file in C#. You will learn to decorate the text in the JPG by setting various text properties and image backgrounds.

Steps to Add Text to JPG in C#

  1. Set the environment to use Aspose.Drawing for .NET to render text on JPG
  2. Create the Bitmap object and use it in creating a Graphics object for drawing on it
  3. Customize the Graphics object to set the rendering quality
  4. Define the background gradient colors in the Graphics object
  5. Create a font and draw the string twice for a shadow effect
  6. Draw an optional border around the text and save it on the disk

These steps describe how to add text in JPG file in C#. You can draw text using the DrawString() method by creating the bitmap, linking it with the Graphics object, and creating a brush, font, and bounding rectangle. Optional settings are setting the rendering quality, background gradient colors, and text style.

Code to Insert Text in JPG in C#

using System;
using Aspose.Drawing;
using Aspose.Drawing.Drawing2D;
using Aspose.Drawing.Text;
class AddTextToJpg
{
static void Main()
{
new License().SetLicense("license.lic");// License for decorated image creation
// Define dimensions for the output image
int imageWidth = 800;// Output image width
int imageHeight = 600;// Output image height
// Create the bitmap
using (Bitmap canvas = new Bitmap(imageWidth, imageHeight))
{
using (Graphics graphics = Graphics.FromImage(canvas))//Graphics object for the canvas
{
graphics.SmoothingMode = SmoothingMode.HighQuality;// Smoothing mode
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;// Rendering hint
// Define the background gradient colors and fill the canvas
Color gradientStart = Color.SkyBlue;
Color gradientEnd = Color.Navy;
using (LinearGradientBrush gradientBrush = new LinearGradientBrush(//The gradient brush
new PointF(0, 0),// Starting point
new PointF(imageWidth, imageHeight),// Ending Point
gradientStart,// Gradient color start
gradientEnd)) // Gradient color end
{
graphics.FillRectangle(gradientBrush, 0, 0, imageWidth, imageHeight);
}
string message = "The sample decorated text with shadow";
Font textFont = new Font("Tahoma", 24, FontStyle.Bold | FontStyle.Italic);// Text style
SizeF textSize = graphics.MeasureString(message, textFont);// Text size
float textX = (imageWidth - textSize.Width) / 2; // Text center X
float textY = (imageHeight - textSize.Height) / 2; // Text center Y
Color shadowColor = Color.FromArgb(100, 0, 0, 0); // Semi-transparent black
using (Brush shadowBrush = new SolidBrush(shadowColor))// Shadow brush
{
graphics.DrawString(message, textFont, shadowBrush, textX + 5, textY + 5);
}
Color mainTextColor = Color.White;//main text with white color
using (Brush textBrush = new SolidBrush(mainTextColor))
{
graphics.DrawString(message, textFont, textBrush, textX, textY);
}
Color borderColor = Color.White;// Border color
int borderThickness = 4; // Border thickness in pixel
using (Pen borderPen = new Pen(borderColor, borderThickness))
{
graphics.DrawRectangle(borderPen, 0, 0, imageWidth - 1, imageHeight - 1);
}
}
string outputFilePath = "TextOnImage.jpg";// Image output
canvas.Save(outputFilePath, Aspose.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}

This code has demonstrated how to add text to a JPEG file in C#. For adding gradient color, we have used LinearGradientBrush which takes starting position, ending position, starting color, and ending color for the gradient. To add the shadow effect, the target string is painted twice with different colors.

This article has taught us the process of creating a JPEG image and adding decorated text to it. For scaling images, refer to the article on Scale image in C#.

 English