Write text to an image using C#.NET

Write text to an image using C#.NET

In this post we are going to know how to write a text to an image using C#.NET. It was required in one of the projects that we were working on, the ability to write a dynamic string that would be entered by the user to an image.

Please find below the code that write a text to an image:

using System.Drawing;
using System.Drawing.Imaging;

namespace NB.BlogCode
{
	public class Program
	{
	 static void Main(string[] args)
	 {
            DrawText(Color.Black, Color.White, "Arial", 20, "this is a test text", 250, 250, @"D:/test.jpg");
         }

         public static void DrawText(Color foreColor, Color backColor, string fontName, int fontSize, string txt, int height, int width, string imagePath)
         {
            Bitmap img = new Bitmap(height, width);
            Graphics Gimg = Graphics.FromImage(img);
            Font imgFont = new Font(fontName, fontSize);
            PointF imgPoint = new PointF(5, 5);
            SolidBrush bForeColor = new SolidBrush(foreColor);
            SolidBrush bBackColor = new SolidBrush(backColor);

            Gimg.FillRectangle(bBackColor, 0, 0, width, height);
            Gimg.DrawString(txt, imgFont, bForeColor, imgPoint);
            img.Save(imagePath, ImageFormat.Jpeg);
         }
    }
}

https://www.designrush.com/agency/software-development/new-york

Share this post

Comment (1)

  • Dionne Marva Reply

    I’m really enjoying the theme/design of your web site.
    Do you ever run into any browser compatibility problems? A few of my blog visitors have complained about my site not operating
    correctly in Explorer but looks great in Safari. Do you have any ideas to
    help fix this issue?

    October 29, 2021 at 1:12 PM

Leave a Reply

Your email address will not be published. Required fields are marked *