我有这段代码;
public static Image AddText(this Image image, ImageText text)
{
Graphics surface = Graphics.FromImage(image);
Font font = new Font("Tahoma", 10);
System.Drawing.SolidBrush brush = new SolidBrush(Color.Red);
surface.DrawString(text.Text, font, brush, new PointF { X = 30, Y = 10 });
surface.Dispose();
return image;
}
然而,当文字被绘制到我的图像上时,它是红色的,带有黑色边框或阴影。
如何在没有任何边框或阴影的情况下将图像写入图像?
修改
我通过添加来解决这个问题;
surface.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
如果有人可以解释为什么我需要这个,这将是伟大的。
答案 0 :(得分:4)
当使用默认透明背景在新创建的Bitmap对象上绘制彩色文本时,它周围有不完整的黑色边框。初始化后使用graphics.Clear(Color.White)解决了这个问题。
答案 1 :(得分:3)
你在那里看到的是正确的。见http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image
您可以尝试使用TextRenderer.DrawText(但请注意它位于System.Windows.Forms命名空间中,因此可能不合适):</ p>
TextRenderer.DrawText(args.Graphics, text, drawFont, ClientRectangle, foreColor, backColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter);
对于backColor尝试使用Color.Transparent。
使用图形,画笔,字体等时,请确保将它们包装在使用语句中,以便只在需要时保留它们...
e.g。
using (var surface = Graphics.FromImage(image))
{
var font = ... // Build font
using (var foreBrush = new SolidBrush(Color.Red))
{
... // Do stuff with brush
}
}
答案 2 :(得分:0)
我认为不应该有任何边界。 是边界还是阴影? 只是尝试使用另一种字体和字体大小+ FontStyle.Regular,看看是否有任何区别