私は、ビットマップにテキストを描画するには、次のコードを使用しています:GDI +を使用してテキストを描画するときに、正確なフォントサイズを定義することはできますか?
using (Font font = new Font("Arial", 10.0f, FontStyle.Bold, GraphicsUnit.Point))
{
//draw the text
graphics.DrawString("Some text", font, Brushes.White, rect, stringFormat);
}
それは素敵な作品。私はテキストを少し大きめにしたい
:ここでは、テキストレンダリングされます。私は、フォントサイズとして11を設定した場合は、ここで私が得るものです:それは私が欲しいもののためには大きすぎる
。 私は10.25,10.5などを試しましたが、10と同じ結果が得られます。GraphicsUnit
からPixel
に設定しようとしますが、カスタムフォントサイズを設定することはできません。ここで
は私の質問です:
GDI +(C#)を使用してテキストを描画するときは、レンダリングされたテキストの "微調整" サイズにpossiblityはありますか?
EDIT: より完全なコード・スニペット(要求される):
using (Bitmap bitmap = new Bitmap(width, height))
using (Graphics graphics = Graphics.FromImage(bitmap))
using (Font font = new Font("Arial", 10.0f, FontStyle.Bold, GraphicsUnit.Point))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle rect = new Rectangle(0, 0, width, height);
//method 1
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
graphics.DrawString("Some text", font, Brushes.White, rect, stringFormat);
//method 2
TextFormatFlags flags = TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
TextRenderer.DrawText(graphics, "Some text", font, rect, Color.White, flags);
bitmap.Save(stream, ImageFormat.Png);
}
'TextRenderingHint.ClearTypeGridFit' – leppie
を試し、私はgraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFitを試みたが、それは私があなたの' StringFormat'が誤っている疑いがある – tigrou
同じ結果を与えます。 IIRC、私は成功して 'FitBlackBox'を使用しました。実際には、非常に特定の高さ設定に基づいて、フォント幅を正確に整数に調整しました。 – leppie