Search code examples
c#fontsresourcesconsole-application

How can I add a custom font into my .NET console application?


I created a program to add a text on an image with a custom font "Lucida Calligraphy". I have added the font file into my solution folder. However, at runtime it does not reflect the font. What am I missing?

  1. I have added font file in my solution folder

    Property set as BuildAction: Resource and Copy to Output Directory: Copy Always

  2. My .csproj reflects this as shown below:

<ItemGroup>
    <None Include="App.config" />
    <Resource Include="Fonts\Lucida-Calligraphy-Italic.ttf">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Resource>
  </ItemGroup>

My Program.cs code looks like this:

// Load the image
Bitmap bitmap = new Bitmap(@"E:\PrintImage\2.png");
Graphics graphics = Graphics.FromImage(bitmap);

// Define text color
Brush brush = new SolidBrush(Color.Red);

// Define text font
Font arial = new Font("{StaticResource Lucida-Calligraphy-Italic}", 45);

// Text to display
string text = "Hellow World!!";

// Define rectangle
Rectangle rectangle = new Rectangle(400, 1500, 1600, 150);

// Specify rectangle border
//Pen pen = new Pen(Color.White, 2);

// Draw rectangle
//graphics.DrawRectangle(pen, rectangle);

// Draw text on image
graphics.DrawString(text, arial, brush, rectangle);

// Save the output file
bitmap.Save(@"E:\OutputImage\2.png");

When I run the program, an output image with text on it is created, but the font doesn't correspond to the one I added as a resource. How can I get it to show the text in the corresponding font?


Solution

  • First, set your font file to Build action - Content. You also need to install "Copy to Output Directory: Copy Always".

    Use the PrivateFontCollection class, which allows you to create a font from a file. In the AddFontFile method, specify the name of the font file. You can specify the full path, or a relative.

    Since you are working with unmanaged resources, such as Bitmap, Graphics and PrivateFontCollection and so on, do not forget to wrap them in a using block, or call the Dispose method to avoid memory leaks.

    using (Bitmap bitmap = new Bitmap(@"1.png"))
    {
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            using (var privateFontCollection = new PrivateFontCollection())
            {
                privateFontCollection.AddFontFile(@"Lucida Calligraphy Italic.ttf");
    
                using (Brush brush = new SolidBrush(Color.Red))
                {
                    // Provide the path to the font on the filesystem
                    using (Font customFont = new Font(privateFontCollection.Families.First(), 45))
                    {
                        // Text to display
                        string text = "Hellow World!!";
    
                        // Define rectangle
                        Rectangle rectangle = new Rectangle(0, 0, 500, 500);
    
                        // Specify rectangle border
                        //Pen pen = new Pen(Color.White, 2);
    
                        // Draw rectangle
                        //graphics.DrawRectangle(pen, rectangle);
    
                        // Draw text on image
                        graphics.DrawString(text, customFont, brush, rectangle);
    
                        // Save the output file
                        bitmap.Save(@"2.png");
                    }
                }
            }
        }
    }