Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: Does Magick.Net support "caption:" functionality?

$
0
0
I have done some more debugging and found that raising the FontPointSize with 0.1 was causing the performance hit. I've changed it to 1 and now Draw is actually faster.

I did see another thing with caption though. It does not honor line breaks so that an entire line can only fit on on a single line in the image (which is what i want).

Anyway my code sample:
string fontFamily = "Droid-Serif-Bold-Italic";
string content = "Hello World!\nsecond line\nThird g";
            
var bgColor   = new MagickColor("#F0F0F0");
var fillColor = new MagickColor("#000000");
using (MagickImage image = new MagickImage(bgColor, 500, 500))
{
    var gravity       = new DrawableGravity(Gravity.Northeast);
    var textAntialias = new DrawableTextAntialias(true);
    var text          = new DrawableText(0, 0, content);

    image.Font      = fontFamily;
    image.FillColor = fillColor;

    CalculateFontPointSize(image, content);

    image.Draw(
        textAntialias,
        text,
        gravity);
}

privatevoid CalculateFontPointSize(MagickImage image, string text)
{
    // FontType Metrics only works for single lines?// set font size to smallest size
    image.FontPointsize = 0;
    double calculatedHeight = 0;
    double calculatedWidth  = 0;

    // get longest linevar lines = text.Split(new [] {"\n" }, StringSplitOptions.None);
    var longestLine = lines.OrderByDescending(x => x.Length).First();

    var metrics = image.FontTypeMetrics(longestLine);

    while (calculatedWidth < image.Width && calculatedHeight < image.Height)
    {
        image.FontPointsize += 1;

        metrics = image.FontTypeMetrics(longestLine);

        calculatedHeight = metrics.TextHeight * lines.Length;
        calculatedWidth  = metrics.TextWidth;
    }
}
VS
using (MagickImage image = new MagickImage(bgColor, 500, 500))
{
    image.Font      = fontFamily;
    image.FillColor = fillColor;

    var textImage = CreateText(fontFamily, 500, 500, content);
    image.Composite(textImage, 0, 0, CompositeOperator.Over);

}

private MagickImage CreateText(string fontFamily, int width, int height, string text)
{
    var image = new MagickImage(MagickColor.Transparent, width, height);
    
    image.FillColor = new MagickColor("#000000");
    image.Font = fontFamily;

    image.Read(string.Format("caption:{0}", text));

    return image;
}

Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>