有没有办法以编程方式将文本添加到System.Drawing.Image?

时间:2011-08-22 15:20:52

标签: c# gdi+

我有一个System.Drawing.Image,我用System.Drawing.Graphics DrawImage函数显示。图像是一辆警车,我想在警车上画一个单位号码。是否有捷径可寻?

2 个答案:

答案 0 :(得分:5)

您可以使用DrawString方法将文字写入图像。

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

答案 1 :(得分:4)

目前尚不清楚您是要尝试将文字添加到图像还是仅在图像上显示文字:

到图像:

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

或在图片上:

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