简体   繁体   English

使用Magick ++向文本添加背景

[英]Add background to the text using Magick++

Im trying to add red background under the text using Magick++. 我试图使用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. 它会打印文本OK,但是文本没有红色背景。 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: 而不是一一画画,而是绘制一个Drawable项列表,然后一次绘制所有内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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