简体   繁体   中英

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:

在此处输入图片说明

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

在此处输入图片说明

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);
  • 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"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM