0

特定のフォント (Courier New) を使用して、テキスト ファイルを画像に変換しようとしています。私が抱えている問題は、フォントの幅が固定されているのに、テキストが画像上でそのようにレンダリングされていないことです。ここに私が現在使用しているコードがあります

var fontName = textToImageSection.GetString("FontName", "Courier New");
var fontSize = textToImageSection.GetInt("FontSize", 12);
textFont = new Font(fontName, fontSize);

var sf = new StringFormat(StringFormatFlags.MeasureTrailingSpaces);
sf.Trimming = StringTrimming.Character;
var text = File.ReadAllText(textFile.Path);
var image = new Bitmap(1, 1);
var textSize = new Size();
using (var g = Graphics.FromImage(image))
    textSize = g.MeasureString(text, textFont, int.MaxValue, sf).ToSize();
image = new Bitmap(image, textSize);
using (var g = Graphics.FromImage(image))
{
    g.Clear(Color.White);
    //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
    g.DrawString(text, textFont, Brushes.Black, borderLeft, borderTop, sf);
}

image.SaveAsTiff(path);

私はさまざまな値を試してきましたが、TextRenderingHintあまり運がなく、StringFormat.

これが結果の画像です

ここに画像の説明を入力

これは、Courier New Font で表示された Notepad++ のテキストです。

ここに画像の説明を入力

4

0 に答える 0