Writing text to an image using C#

· Christopher Hoelter's Blog

#programming #c# #dotnet

A functioning example can be found here.

Below is a demonstration of how to write text to an image (jpeg) using C# and a custom font.

# ImageWriter Class

 1using System.Drawing;
 2using System.Drawing.Drawing2D;
 3using System.Drawing.Imaging;
 4using System.Drawing.Text;
 5
 6namespace WritingTextOnImage
 7{
 8    public class ImageWriter
 9    {
10        public void WriteTextToImage(Bitmap image, string savePath, string textToWrite, FontFamily fontFamily)
11        {
12            using (var graphics = Graphics.FromImage(image))
13            {
14                graphics.SmoothingMode = SmoothingMode.AntiAlias;
15                // Image is 444 x 248 pixels, this should place text center
16                PointF writeLocation = new PointF(222f, 124f);
17                var format = new StringFormat { Alignment = StringAlignment.Center };
18
19                using (var font = new Font(fontFamily, 15, FontStyle.Regular))
20                {
21                    graphics.DrawString(textToWrite, font, Brushes.White, writeLocation, format);
22                }
23                image.Save(savePath, ImageFormat.Jpeg);
24            }
25        }
26
27        public Bitmap LoadImage(string imagePath)
28        {
29            Bitmap image = new Bitmap(imagePath);
30            return image;
31        }
32
33        public FontFamily LoadFont(string fontPath, string fontName)
34        {
35            PrivateFontCollection fonts = new PrivateFontCollection();
36            fonts.AddFontFile(fontPath);
37            FontFamily font = fonts.Families[0];
38            return font;
39        }
40    }
41}

These are methods to load up the image and font to be passed in as parameters to WriteTextToImage, the method actually writing the string to the image file. The resulting bitmap can also be directed elsewhere and used from memory instead of saving to a file once the string is written.

# Using the ImageWriter

 1using System;
 2using System.Drawing;
 3using System.IO;
 4
 5namespace WritingTextOnImage
 6{
 7    class Program
 8    {
 9        static void Main(string[] args)
10        {
11            string rootProjectPath = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName;
12            string imagePath = Path.Combine(rootProjectPath + "\\prometheus.jpg");
13            string fontPath = Path.Combine(rootProjectPath + "\\Chunkfive.otf");
14            string savePath = Path.Combine(rootProjectPath + "\\new_prometheus.jpg");
15
16            var imageWriter = new ImageWriter();
17            Bitmap image = imageWriter.LoadImage(imagePath);
18            FontFamily font = imageWriter.LoadFont(fontPath, "chunkfive");
19
20            imageWriter.WriteTextToImage(image, savePath, "Prometheus", font);
21        }
22    }
23}

Quick example of using the ImageWriter class with a console program.


Please leave a comment or drop a message at https://lists.sr.ht/~hoelter/public-inbox. Email me directly at [email protected].