【发布时间】:2013-07-15 00:05:24
【问题描述】:
我正在寻找一种文本渲染方法,其中从位图初始化图形对象,在其上绘制文本,并且文本看起来像附加图像中的第一行。
有人可以解释一下可以做到这一点的方法吗?我不完全理解为什么以下方法都不能重现它:
测试字体为:Segoe UI,8.25pt
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));
}
【问题讨论】:
-
你试过
TextRenderingHint.ClearTypeGridFit吗?编辑:如果您使用“测试 4”版本但使用TextRenderingHint.SystemDefault会发生什么? -
+1 好问题 我以前也遇到过这个问题,但没有提出问题
-
@ChrisSinclair ClearTypeGridFit 看起来也不像它。 SystemDefault 是图形对象初始化时的默认值,类似于#2。
-
您是否尝试过检查所有传入
e.Graphics对象的属性(例如它的PageUnit、SmoothingMode、TextContrast、InterpolationMode等,看看哪些值与您从Graphics.FromImage(bmp)获得的Graphics对象不同? -
@ChrisSinclair 是的,唯一不同的是PageUnit属性(FromImage初始化为Display,Form传递Pixel)。其他一切都是一样的。这就是我在每个测试用例中设置 PageUnit 属性的原因。