using custom font inside C# .net core application

Anjali Agarwal 1,531 Reputation points
2023-03-29T19:35:14.7966667+00:00

I have the following code in my C# application that coverts text to image. In this code, I am using a custom font. I am specifying the path of the custom font, the custom font resides in my wwwroot folder. below is the code where I am using custom font.

string fontFam = Path.Combine(_environment.WebRootPath, "Font");

fontfam = fontfam + "\" + "journal.ttf";

graphics.DrawString(text, new Font(fontfam , 100, FontStyle.Regular), new SolidBrush(Color.FromArgb(0, 0, 255)), new PointF(0.4F, 2.4F));

below is where custom font resides:

User's image

I am passing the path to customfont in fontfam, but when I convert the text to image, the font is "Microsoft Sans Serif" and I can see the name Microsoft Sans serif in below screen too:

User's image

How can I make the font "Journal" in the C# code.

Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Chen Li - MSFT 1,231 Reputation points
    2023-03-30T02:59:07.84+00:00

    Hi @Anjali Agarwal ,

    If you use System.Drawing, then you need to install this font in your computer. in the msdn documents on the Font Class you can see: If you attempt to use a font that is not supported, or the font is not installed on the machine that is running the application, the Microsoft Sans Serif font will be substituted.

    You can copy your journal.ttf file to the computer's C:\Windows\Fonts directory, which will install your fonts on the computer.

    In addition, Font cannot recognize the path, it can only recognize the corresponding familyName:

    User's image

    So you just need to pass the name of the font after installing it:

    new Font("Journal", 100, FontStyle.Regular)
    

    Result:

    User's image

    If you don't want to install fonts, and just want to get fonts through the corresponding path, you can use GrapeCity.Documents.Imaging Nuget Package. Below is mt test code, you can refer to it.

    //Create GcBitmap
    var bmp = new GcBitmap(1024, 1024, true, 96, 96);
    //Create a graphics for drawing
    GcBitmapGraphics g = bmp.CreateGraphics();
    
    //Add a radial gradient
    RadialGradientBrush r = new RadialGradientBrush(Color.Beige,
    Color.RosyBrown, new PointF(0.5f, 0.5f), true);
    
    //Draw a rectangle
    var rc = new RectangleF(0, 0, bmp.Width, bmp.Height);
    
    //Fill the rectangle using specified brush
    g.FillRectangle(rc, r);
    
    // Create a text format for the "Hello World!" string:
    TextFormat tf = new TextFormat()
    {
        Font = Font.FromFile(Path.Combine(_environment.WebRootPath, "Font/JOURNAL.TTF")),
        FontSize = 36
    };
    
    //Draw the string (text)
    g.DrawString("Hello World!", tf, rc, TextAlignment.Center,
            ParagraphAlignment.Center, false);
    
    //Save bitmap
    bmp.SaveAsJpeg(Path.Combine(_environment.WebRootPath, "Image/Helloworld.jpg"));
    

    Result:

    User's image

    For more detail about GrapeCity.Documents.Imaging, you can refer to this document.


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Chen Li

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.