C#在位图上写入文本

82

我有个问题。我想在C# Windows表单中制作一些图形。我想要将位图读入我的程序,然后在该位图上写入一些文本。最后,我希望这张图片加载到pictureBox中。这就是我的问题。我该怎么做?

例如,它应该如何工作:

Bitmap a = new Bitmap(@"path\picture.bmp");
a.makeTransparent();
// ? a.writeText("some text", positionX, positionY);
pictuteBox1.Image = a;

有可能实现这个吗?

5个回答

160
Bitmap bmp = new Bitmap("filename.bmp");

RectangleF rectf = new RectangleF(70, 90, 90, 50);

Graphics g = Graphics.FromImage(bmp);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf);

g.Flush();

image.Image=bmp;

6
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; //添加此行以使文本更加清晰。 - rockXrock
4
实际上,您想要将 TextRenderingHint 设置为 TextRenderingHint.AntiAliasGridFit。每像素单个比特的模式是锯齿状的:https://msdn.microsoft.com/en-us/library/a619zh6z(v=vs.110).aspx - iCollect.it Ltd
4
所有这些答案中缺少的一点是“我怎么知道字符串是否适合?”(或“如何完美地将位图调整到字符串中?”)。答案是使用Graphics.MeasureString - BlueRaja - Danny Pflughoeft
g.DrawString() 不支持 Unicode 字符,我该如何处理? - ilCosmico

43

这是一个很老的问题,但我今天为一个应用程序构建了这个功能并发现其他答案中显示的设置不会生成清晰的图像(可能是因为后来在 .Net 版本中添加了新选项)。

假设你想将文本居中显示在位图中,可以这样做:

// Load the original image
Bitmap bmp = new Bitmap("filename.bmp");

// Create a rectangle for the entire bitmap
RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height);

// Create graphic object that will draw onto the bitmap
Graphics g = Graphics.FromImage(bmp);

// ------------------------------------------
// Ensure the best possible quality rendering
// ------------------------------------------
// The smoothing mode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing). 
// One exception is that path gradient brushes do not obey the smoothing mode. 
// Areas filled using a PathGradientBrush are rendered the same way (aliased) regardless of the SmoothingMode property.
g.SmoothingMode = SmoothingMode.AntiAlias;

// The interpolation mode determines how intermediate values between two endpoints are calculated.
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

// Use this property to specify either higher quality, slower rendering, or lower quality, faster rendering of the contents of this Graphics object.
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

// This one is important
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

// Create string formatting options (used for alignment)
StringFormat format = new StringFormat()
{
    Alignment = StringAlignment.Center,
    LineAlignment = StringAlignment.Center
};

// Draw the text onto the image
g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf, format);

// Flush all graphics changes to the bitmap
g.Flush();

// Now save or use the bitmap
image.Image = bmp;

参考资料


我之前一直无法通过这种方法(或其他任何方法)获得成功的结果,直到我在字体构造中添加了“GraphicsUnit.Pixel”,我可能错过了什么? - mkb
@mkb 你需要展示你自己的代码来看看可能出了什么问题。这是我们系统中正在使用的生产代码。谢谢。 - iCollect.it Ltd
我需要添加 var solidBrush = new SolidBrush (Color.White); g.FillRectangle (solidBrush, rectf); 来创建一个背景为白色、文本为黑色的位图。否则它将完全是黑色的。 - Paulo Guimarães
@PauloGuimarães 这个例子假设在现有的位图图像上进行绘制。听起来你开始没有图像或者是一个黑色的图像。显然,你需要调整颜色以适应你的情况。 - iCollect.it Ltd

15
你需要使用 Graphics 类来在位图上写入内容。
具体来说,可以使用其中一种DrawString方法。
Bitmap a = new Bitmap(@"path\picture.bmp");

using(Graphics g = Graphics.FromImage(a))
{
  g.DrawString(....); // requires font, brush etc
}

pictuteBox1.Image = a;

4
var bmp = new Bitmap(@"path\picture.bmp");
using( Graphics g = Graphics.FromImage( bmp ) )
{
    g.DrawString( ... );
}

picturebox1.Image = bmp;

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接