Is there a way to programmatically add text to a System.Drawing.Image?

喜夏-厌秋 提交于 2019-12-20 02:52:44

问题


I have a System.Drawing.Image that I display with System.Drawing.Graphics DrawImage function. The image is a police car, and I would like to draw a unit number on top of the police car. Is there an easy way to do this?


回答1:


You can use the DrawString method to write text out onto an image.

// Create string to draw.
String drawString = "Sample Text";

// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);

// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(150.0F, 150.0F);

// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);



回答2:


It's not clear if you are trying to add the text to the image or just display text on the image:

To the image:

using (Graphics g = Graphics.FromImage(yourImage))
{
  g.DrawString(...);
}

or On the image:

e.Graphics.DrawImage(...);
e.Graphics.DrawString(...);


来源:https://stackoverflow.com/questions/7149694/is-there-a-way-to-programmatically-add-text-to-a-system-drawing-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!