C# GDI+;大字体渲染和ClearType
我正在尝试使用GDI+将大字体字符渲染为位图,以供以后脱机使用(确切地说,作为在Windows Phone上使用Direct3D绘制文本的位图字体)。我希望使用ClearType呈现字体,但当字体大小大于48点时,我似乎无法做到这一点。以下是我使用的代码:C# GDI+;大字体渲染和ClearType,c#,fonts,gdi+,cleartype,C#,Fonts,Gdi+,Cleartype,我正在尝试使用GDI+将大字体字符渲染为位图,以供以后脱机使用(确切地说,作为在Windows Phone上使用Direct3D绘制文本的位图字体)。我希望使用ClearType呈现字体,但当字体大小大于48点时,我似乎无法做到这一点。以下是我使用的代码: static void Main(string[] args) { var noFontFallbackFormat = (StringFormat)StringFormat.GenericTypographic.Clone();
static void Main(string[] args)
{
var noFontFallbackFormat = (StringFormat)StringFormat.GenericTypographic.Clone();
noFontFallbackFormat.FormatFlags |= StringFormatFlags.NoFontFallback;
DrawCharacter("Segoe UI", 48, noFontFallbackFormat);
DrawCharacter("Segoe UI", 72, noFontFallbackFormat);
}
static void DrawCharacter(string fontName, float fontSize, StringFormat stringFormat)
{
var fontSizeInPixels = fontSize * 96.0f / 72.0f; // Converts points to pixels
using (var font = new Font(fontName, fontSizeInPixels, FontStyle.Regular, GraphicsUnit.Pixel))
{
using (var bitmap = new Bitmap(256, 256, PixelFormat.Format32bppArgb))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
var text = "A";
var characterSize = graphics.MeasureString(text, font, Point.Empty, stringFormat);
int width = (int)Math.Ceiling(characterSize.Width);
int height = (int)Math.Ceiling(characterSize.Height);
graphics.Clear(Color.Black);
graphics.DrawString(text, font, new SolidBrush(Color.White), 0, 0, stringFormat);
graphics.Flush();
using (var characterBitmap = bitmap.Clone(new Rectangle(0, 0, width, height), PixelFormat.Format32bppArgb))
{
characterBitmap.Save(string.Format("{0}_{1}.png", fontName, fontSize));
}
}
}
}
}
使用字体大小48呈现时,如下所示:
但是,字体大小为72的渲染会使ClearType消失:
为什么会这样?我知道TextRenderingHint.ClearTypeGridFit是一个提示,而不是命令,但我能强迫它一直使用ClearType吗?在较大的字体大小下,将字体呈现设置为AntiAliasGridFit可能比ClearType看起来更好。这就是我最终要做的,虽然我仍然想知道为什么ClearType拒绝工作。不同的字体以不同的大小切换。如何知道何时使用抗锯齿GridFit?