Reputation: 91
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
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"));
Upvotes: 1