This project has moved and is read-only. For the latest updates, please go here.

Drawing Italic / Different font text

Jul 19, 2013 at 4:31 AM
Edited Jul 19, 2013 at 3:11 PM
This doesn't seem to work to draw italic, bold Tahoma text:
using (var working = new MagickImage(@"c:\temp\filename.jpg"))
{
    // Draw text
    working.StrokeColor = new MagickColor("#FFFFFF");
    working.FillColor = working.StrokeColor;
    working.Font = "Tahoma";
    working.FontPointsize = 24f;

    using (var piFont = new DrawableFont("Tahoma", FontStyleType.Italic, FontWeight.Weight700, FontStretch.Normal))
    {
        working.Draw(piFont);               
        var metrics = working.FontTypeMetrics("Why hello there!");
        using (var piText = new DrawableText(10, working.Height - metrics.TextHeight - 10, "Why hello there!"))
        {
            working.Draw(piText);
        }
    }

}
Any suggestions?
Jul 19, 2013 at 5:45 AM
If you want to do multiple draw operations you should use the overload of Draw that accepts an IEnumerable<DrawableBase>. I posted an example here: https://magick.codeplex.com/discussions/445822. I will change the method signature of Draw(Drawable) to Draw(params Drawable) to make this more obvious.
Jul 19, 2013 at 3:03 PM
Edited Jul 19, 2013 at 3:11 PM
Thank you for the quick response. However, that still didn't work - see image. It is not using the specified font family and it is not honoring the italics, font weight etc.
using (var working = new MagickImage(@"c:\temp\filename.jpg"))
{
    // Draw text
    working.StrokeColor = new MagickColor("#FFFFFF");
    working.FillColor = working.StrokeColor;
    working.Font = "Tahoma";
    working.FontPointsize = 24f;

    using (var piFont = new DrawableFont("Tahoma", FontStyleType.Italic, FontWeight.Weight700, FontStretch.Normal))
    {
        var list = new List<Drawable>();
        list.Add(piFont);
        var metrics = working.FontTypeMetrics("Why hello there!");
        using (var piText = new DrawableText(10, working.Height - metrics.TextHeight - 10, "Why hello there!"))
        {
            list.Add(piText);
            working.Draw(list);
        }
    }
}
Also when doing it as a list like that, how do you measure text (FontTypeMetrics) with the correct font? An italic, heavy font will be wider than a non-italic light font.

Image
Jul 19, 2013 at 6:50 PM
Edited Jul 19, 2013 at 6:51 PM
Found a workaround. You can't make any arbitrary font bold or italic and you can't use DrawableFont at all. but just using the Font and FontPointsize properties works as long as you format the font name like ImageMagick expects:
using (var working = new MagickImage(@"c:\temp\filename.jpg"))
{
    // Draw text
    working.StrokeColor = new MagickColor("#FFFFFF");
    working.FillColor = working.StrokeColor;
    working.Font = "Arial-Bold-Italic";
    working.FontPointsize = 24f;

    var metrics = working.FontTypeMetrics("Why hello there!");
    using (var piText = new DrawableText(10, working.Height - metrics.TextHeight - 10, "Why hello there!"))
    {
            working.Draw(piText);
    }
}
Jul 19, 2013 at 7:18 PM
Are you aware of the 'DrawableGravity' class? You can use that to position your text. Then you won't have to do the calculations yourself.
Jul 19, 2013 at 9:55 PM
I was aware of it, but I didn't know how to handle padding. Now that I've figured that out it does make it significantly easier to place the text. Thank you