C#在位图上写文本


69

我有以下问题。我想在C#Windows窗体中制作一些图形。我想读取位图到我的程序,然后在该位图上写一些文本。最后,我希望将此图片加载到pictureBox。这是我的问题。我该怎么做?

例如,它如何工作:

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

有可能吗?

Answers:


143
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;

4
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; //添加此行以使文本美观。
rockXrock 2015年

4
@rockXrock:实际上您要设置TextRenderingHint = TextRenderingHint.AntiAliasGridFit。每像素模式单个位是锯齿状的:msdn.microsoft.com/en-us/library/a619zh6z(v=vs.110).aspx
Gone Coding,

所有这些答案中缺少的一件事是“我怎么知道字符串是否适合?” (或“如何使位图完全适合字符串?”)。答案是使用Graphics.MeasureString
BlueRaja-Danny Pflughoeft

36

这是一个非常老的问题,但是今天只需要为一个应用程序构建它,并发现其他答案中显示的设置不会产生清晰的图像(可能是因为在更高的.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您需要显示自己的代码以查看可能出了什么问题。这是我们系统中有效的生产代码。谢谢
Gone Coding

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;

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.