c#在位图上写文字

nir*_*mus 67 c# drawing

我有以下问题.我想用c#windows形式制作一些图形.我想读取位图到我的程序,然后在这个位图上写一些文字.最后我希望这张图片加载到pictureBox.这是我的问题.我该怎么做?

如何,它必须如何工作:

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

有可能做到吗?

dan*_*iax 135

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;
Run Code Online (Sandbox Code Playgroud)

  • g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; //添加此行以使文本更好看. (4认同)
  • 所有这些答案中缺少的一件事是“我如何知道字符串是否合适?” (或者“如何才能使位图完美地适应字符串?”)。答案是使用 [Graphics.MeasureString](https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphics.measurestring?view=netframework-4.8) (3认同)
  • @rockXrock:实际上你想设置`TextRenderingHint = TextRenderingHint.AntiAliasGridFit`.每像素单比特模式是锯齿状的:https://msdn.microsoft.com/en-us/library/a619zh6z(v = vs.110).aspx (2认同)

Gon*_*ing 33

很老的问题,但今天只需要为应用程序构建这个,并发现其他答案中显示的设置不会产生干净的图像(可能在以后的.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;
Run Code Online (Sandbox Code Playgroud)

参考


Ode*_*ded 14

您需要使用Graphics该类才能在位图上进行写入.

具体来说,其中一种DrawString方法.

Bitmap a = new Bitmap(@"path\picture.bmp");

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

pictuteBox1.Image = a;
Run Code Online (Sandbox Code Playgroud)