soyxan
soyxan

Reputation: 91

GraphicsMagick++ draw text and auto-resize canvas

I am planning to generate an image with Magick (GraphicsMagick++) that have some text and I want the image canvas resized automatically based on the text drawn.

This is my code:

    bool LoadText(const std::string& text, const std::string& fontface, int pointsize, Magick::Color color) {

    image = Magick::Image( Magick::Geometry(1,1), Magick::Color("black"));
    image.font(fontface);
    image.fillColor(color);
    image.strokeColor(color);
    image.fontPointsize(pointsize);
    image.read("CAPTION:" + text);
    //image.annotate(text,CenterGravity);
    return true;
}

With "annotate()" the canvas is not resized but the text color and background is right.

If I use "CAPTION:" protocol the canvas is resized but the fontcolor and background color is not respected.

What am I doing wrong?

Upvotes: 0

Views: 670

Answers (1)

emcconville
emcconville

Reputation: 24419

You shouldn't have to allocated a blank image when reading from the caption protocol, but set the background color directly.

using namespace Magick;

bool LoadTextCaption(const std::string& text,
                     const std::string& fontface,
                     int pointsize,
                     Magick::Color color)
{
    Image image;                           // Allocated but not initialized.
    image.font(fontface);
    image.fillColor(color);
    image.strokeColor(color);
    image.fontPointsize(pointsize);
    image.backgroundColor(Color("BLACK")); // <- Set background
    image.read("CAPTION:" + text);
    return true;
}
// ... 
LoadTextCaption("Hello Caption!", "TimesNewRoman", 32, Color("RED"));

Hello Caption!

Upvotes: 1

Related Questions