Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom TTF font for DrawString image rendering

Tags:

I am using GDI+ on the server-side to create an image which is streamed to the user's browser. None of the standard fonts fit my requirements and so I want to load a TrueType font and use this font for drawing my strings to the graphics object:

using (var backgroundImage = new Bitmap(backgroundPath))
using (var avatarImage = new Bitmap(avatarPath))
using (var myFont = new Font("myCustom", 8f))
{
    Graphics canvas = Graphics.FromImage(backgroundImage);
    canvas.DrawImage(avatarImage, new Point(0, 0));

    canvas.DrawString(username, myFont,
        new SolidBrush(Color.Black), new PointF(5, 5));

    return new Bitmap(backgroundImage);
}

myCustom represents a font that is not installed on the server, but for which I have the TTF file for.

How can I load the TTF file so that I can use it in GDI+ string rendering?

like image 542
Llyle Avatar asked Feb 07 '09 06:02

Llyle


2 Answers

I've found a solution to using custom fonts.

// 'PrivateFontCollection' is in the 'System.Drawing.Text' namespace
var foo = new PrivateFontCollection();
// Provide the path to the font on the filesystem
foo.AddFontFile("...");

var myCustomFont = new Font((FontFamily)foo.Families[0], 36f);

Now myCustomFont can be used with the Graphics.DrawString method as intended.

like image 72
Llyle Avatar answered Oct 16 '22 14:10

Llyle


Just to give a more complete solution

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Text;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string fontName = "YourFont.ttf";
        PrivateFontCollection pfcoll = new PrivateFontCollection();
        //put a font file under a Fonts directory within your application root
        pfcoll.AddFontFile(Server.MapPath("~/Fonts/" + fontName));
        FontFamily ff = pfcoll.Families[0];
        string firstText = "Hello";
        string secondText = "Friend!";

        PointF firstLocation = new PointF(10f, 10f);
        PointF secondLocation = new PointF(10f, 50f);
        //put an image file under a Images directory within your application root
        string imageFilePath = Server.MapPath("~/Images/YourImage.jpg");
        Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(imageFilePath);//load the image file

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            using (Font f = new Font(ff, 14, FontStyle.Bold))
            {
                graphics.DrawString(firstText, f, Brushes.Blue, firstLocation);
                graphics.DrawString(secondText, f, Brushes.Red, secondLocation);
            }
        }
        //save the new image file within Images directory
        bitmap.Save(Server.MapPath("~/Images/" + System.Guid.NewGuid() + ".jpg"));
        Response.Write("A new image has been created!");
    } 
}
like image 45
user890255 Avatar answered Oct 16 '22 15:10

user890255