TextRenderer.DrawText以位图VS OnPaintBackground [英] TextRenderer.DrawText in Bitmap vs OnPaintBackground

查看:274
本文介绍了TextRenderer.DrawText以位图VS OnPaintBackground的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用了OnPaintBackground我的文本提供对象的图形使用TextRenderer.DrawText()看起来很完美。如果我创造我自己的位图,并使用图形从我的位图对象得到我的文字看起来很可怕。它看起来是采用了黑色,而不是位图的背景颜色抗锯齿的文本。如果我使用Graphics.DrawString(我能避免这个问题),但这种方法有可怕的字距问题。我该怎么办? ?我怎样才能获得TextRenderer.DrawText()正确使用位图的内容抗混叠

If I use TextRenderer.DrawText() using the Graphics object provided in the OnPaintBackground my text looks perfect. If I create my own Bitmap and use the Graphics object obtained from my Bitmap my text looks terrible. It looks like it is anti-aliasing the text using black, not the bitmap's background color. I can avoid this problem if I use Graphics.DrawString(), but this method has horrible kerning problems. What should I do? How can I get TextRenderer.DrawText() to anti-alias properly using the Bitmap's contents?

看起来很可怕:

Bitmap bmp = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Red);
TextFormatFlags tf = TextFormatFlags.Left;
TextRenderer.DrawText(g, @"C:\Development\Testing\blag", font, clip, Color.White, Color.Transparent, tf);
}



看起来不错,但我想使这个到一个位图,不要到控制表面:

protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.Clear(Color.Red);
TextFormatFlags tf = TextFormatFlags.Left;
TextRenderer.DrawText(e.Graphics, @"C:\Development\Testing\blag", font, clip, Color.White, Color.Transparent, tf);
}



有什么区别?

What is the difference?

推荐答案

答案是不使用 TextRenderer 。 TextRenderer对于GDI(而不是GDI +)实现文本渲染,它具有许多功能的包装,但你发现没有,在内存中的DC很好地互操作。

The answer is not to use TextRenderer. TextRenderer is a wrapper for the GDI (not GDI+) implementation of text rendering, which has lots of features, but doesn't interoperate well with in-memory DCs as you have discovered.

使用 Graphics.DrawString &安培; Graphics.MeasureString ,但记得要传递StringFormat.GenericTypographic得到精确的尺寸和定位。

Use Graphics.DrawString & Graphics.MeasureString, but remember to pass it StringFormat.GenericTypographic to get accurate size and positioning.

原因TextRenderer最初引进的是,GDI +不支持所有复杂的脚本,GDI的发动机的Uniscribe做。随着时间的推移但是GDI +的复杂脚本的支持已经扩大了,这几天没有任何理由留下来使用TextRenderer(这不是两个甚至更快了,其实挺它出现在对面)。

The reason TextRenderer was introduced initially was that GDI+ didn't support all the complex scripts that GDI's Uniscribe engine did. Over time however GDI+ support for complex scripts has been expanded, and these days there aren't any good reasons left to use TextRenderer (it's not even the faster of the two anymore, in fact quite the opposite it appears).

真的,不过,除非你正在运行到严重的,可衡量的绩效问题,只需使用 Graphics.DrawString

Really, though, unless you are running into serious, measurable performance issues just use Graphics.DrawString.

这篇关于TextRenderer.DrawText以位图VS OnPaintBackground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆