converting a text to an image

Anjali Agarwal 1,386 Reputation points
2023-03-29T02:31:41.36+00:00

I am trying to convert a text to an image. When I see the converted image, the font family is changed. This is my code that works fine on my localhost, but when I deploy the code to the server. The font family family that I specified in the image conversion is gone and is replaced by Arial font. Below is the code:

public async Task<byte[]> ConvertTextToImage(string text)

    {

        byte[] bytes = null;

        using (var stream = new MemoryStream())

        {

            Bitmap bitmap = new Bitmap(950, 200, PixelFormat.Format64bppArgb);

            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.Clear(Color.White);

            graphics.SmoothingMode = SmoothingMode.AntiAlias;

            graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

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

            graphics.Flush();

            graphics.Dispose();

             bitmap.Save(stream, ImageFormat.Jpeg);

            bitmap.Dispose();

            bytes =  stream.ToArray();

            return bytes;

        }

    }

As you can see that the font family is "Journal", but after deployment, I can see that the image is there, but the font family is Arial. I am not sure how to even troubleshoot this issue on the server because it is working fine on the localhost. I used the System.drawing.common to convert from text to image. font family "Journal" is installed on the server. any help in troubleshooting or why this is occurring only the server will be highly appreciated. I can install the .ttf file in my application, but not sure where to install this font inside the application and how to refer it inside the code.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,207 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Arun Siripuram 801 Reputation points
    2023-03-29T07:49:17.5633333+00:00

    Thank you for reaching out to Microsoft Q&A

    Please help us with more details about the OS in Server. Secondly, please confirm whether

    "Copy to Outout Directory" property of Font files is set to "Copy Always". We can also check the root directories in the Server to confirm the font files are available in the application output directories.

    0 comments No comments

  2. Arun Siripuram 801 Reputation points
    2023-03-29T07:50:05.49+00:00

    Thank you for reaching out to Microsoft Q&A

    Please help us with more details about the OS in Server. Secondly, please confirm whether

    "Copy to Output Directory" property of Font files is set to "Copy Always". We can also check the root directories in the Server to confirm the font files are available in the application output directories.


  3. Arun Siripuram 801 Reputation points
    2023-04-02T16:04:10.8533333+00:00

    @Anjali Agarwal Thought to check with you whether you got a chance to check the responses.

    0 comments No comments