Ron 11011011
Ron 11011011

Reputation: 21

Add background to the text using Magick++

Im trying to add red background under the text using Magick++. My simple code is:

Magick::Image img( Geometry(800,200), Color("white") );

img.strokeWidth(12);
img.font("Helvetica");
img.fontPointsize(font_size);

img.draw(Magick::DrawableTextUnderColor(Magick::Color("red")));
img.draw(Magick::DrawableText(25, 25,  "Some text") );

img.write("file.png");

It prints text OK, but the text does not have red background. Current result is this:

enter image description here

However, I would like to have the text with background, something like this (background photo shopped for the example)

enter image description here

Upvotes: 0

Views: 1278

Answers (2)

Sagar Patel
Sagar Patel

Reputation: 852

  • Here is a solution of your problem,

    Image img(Geometry(800, 800), Color("white"));
    img.font("Helvetica");
    img.fillColor(Color("firebrick"));
    img.strokeColor(Color("red"));
    img.draw(Magick::DrawableText(25, 25,  "Some text") );
    img.write("text.png");
    

Upvotes: 2

Marcin
Marcin

Reputation: 238209

This should work. Instead of drawing things one-by-one, make a list of Drawable items, and than draw everything at once:

list<Magick::Drawable> to_draw;

to_draw.push_back(Magick::DrawableText(25, 25,  "Some text"));
to_draw.push_back(Magick::DrawableTextUnderColor("red"));

img.draw(to_draw);

Upvotes: 2

Related Questions