我正在尋找一種文本渲染方法,其中圖形對象從位圖初始化,文本被繪製在其上,文本看起來像附加圖像中的第一行。不同的文本渲染方法不生成我想要的一個
有人可以請解釋一種方法,將做到這一點?我不完全理解爲什麼沒有下面的方法可以重現:在測試
字體是:瀨越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。 – test