C# 不同的文本呈现方法不';我不能生产我想要的
我正在寻找一种文本渲染方法,其中图形对象从位图初始化,在其上绘制文本,文本看起来像附加图像中的第一行 有人能解释一下这样做的方法吗?我不完全理解为什么以下方法都不能复制它: 测试中的字体为:Segoe UI,8.25ptC# 不同的文本呈现方法不';我不能生产我想要的,c#,.net,gdi+,gdi,C#,.net,Gdi+,Gdi,我正在寻找一种文本渲染方法,其中图形对象从位图初始化,在其上绘制文本,文本看起来像附加图像中的第一行 有人能解释一下这样做的方法吗?我不完全理解为什么以下方法都不能复制它: 测试中的字体为:Segoe UI,8.25pt protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); //drawing the string with the Graphics o
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
//drawing the string with the Graphics object the form gives us
e.Graphics.DrawString("1. This is a test using DrawString. " + e.Graphics.TextRenderingHint.ToString(),
base.Font, Brushes.Black, new Point(10, 10));
//width used for all images
const int width = 300;
//drawing the string with Graphics objects initialized from bitmaps
Bitmap bmp = new Bitmap(width, 20);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.PageUnit = GraphicsUnit.Pixel;
gfx.SmoothingMode = SmoothingMode.HighQuality;
gfx.DrawString("2. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(),
base.Font, Brushes.Black, Point.Empty);
}
e.Graphics.DrawImage(bmp, new Point(10, 30));
bmp.Dispose();
bmp = new Bitmap(width, 20);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.PageUnit = GraphicsUnit.Pixel;
gfx.SmoothingMode = SmoothingMode.HighQuality;
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
gfx.DrawString("3. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(),
base.Font, Brushes.Black, Point.Empty);
}
e.Graphics.DrawImage(bmp, new Point(10, 50));
bmp.Dispose();
bmp = new Bitmap(width, 20);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.PageUnit = GraphicsUnit.Pixel;
gfx.SmoothingMode = SmoothingMode.HighQuality;
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
gfx.DrawString("4. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(),
base.Font, Brushes.Black, Point.Empty);
}
e.Graphics.DrawImage(bmp, new Point(10, 70));
bmp.Dispose();
bmp = new Bitmap(width, 20);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.PageUnit = GraphicsUnit.Pixel;
gfx.SmoothingMode = SmoothingMode.HighQuality;
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
gfx.DrawString("5. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(),
base.Font, Brushes.Black, Point.Empty);
}
e.Graphics.DrawImage(bmp, new Point(10, 90));
bmp.Dispose();
bmp = new Bitmap(width, 20);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.PageUnit = GraphicsUnit.Pixel;
gfx.SmoothingMode = SmoothingMode.HighQuality;
using (GraphicsPath path = new GraphicsPath())
{
path.AddString("6. This is a test using GraphicsPath. " + gfx.TextRenderingHint.ToString(),
base.Font.FontFamily, (int)base.Font.Style,
base.Font.Size, Point.Empty, StringFormat.GenericDefault);
gfx.FillPath(Brushes.Black, path);
}
}
e.Graphics.DrawImage(bmp, new Point(10, 110));
bmp.Dispose();
bmp = new Bitmap(width, 20);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.PageUnit = GraphicsUnit.Pixel;
gfx.SmoothingMode = SmoothingMode.HighQuality;
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
using (GraphicsPath path = new GraphicsPath())
{
path.AddString("7. This is a test using GraphicsPath. " + gfx.TextRenderingHint.ToString(),
base.Font.FontFamily, (int)base.Font.Style,
base.Font.Size, Point.Empty, StringFormat.GenericDefault);
gfx.FillPath(Brushes.Black, path);
}
}
e.Graphics.DrawImage(bmp, new Point(10, 130));
}
您的代码中有一个错误,您忘记初始化位图了。它将被彩色像素填充。透明,黑色,alpha为0。当您绘制文本时,Graphics.DrawString()将实现您要求的TextRendering提示,对文本进行抗锯齿处理。但是文本的前景色是黑色的。背景颜色是黑色。因此,抗锯齿像素从黑色混合到黑色。它完全破坏了抗锯齿效果,将字母形状变成了一个斑点。修正:
Bitmap bmp = new Bitmap(width, 20);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.Clear(this.BackColor);
// etc...
}
您是否尝试过
textrendinghint.ClearTypeGridFit
?编辑:如果您使用了“测试4”版本,但使用了textrendinghint.SystemDefault
,会发生什么情况?+1好问题我以前也遇到过这个问题,但没有提出任何问题question@ChrisSinclairClearTypeGridFit看起来也不像。SystemDefault是初始化图形对象时的默认值,它看起来像#2。是否尝试检查传入e.graphics
对象的所有属性(如它的PageUnit
,SmoothingMode
,TextContrast
,InterpolationMode
,等等,以查看它与从Graphics.FromImage(bmp)
获得的Graphics.FromImage(bmp)
?@ChrisSinclair是的,唯一不同的是PageUnit属性。)(从图像初始化到显示,表单通过像素)。其他一切都是一样的。这就是为什么我在每个测试用例中设置PageUnit属性。这确实是问题所在。谢谢。