2

I am trying to draw the character "t" to a bitmap at location (0,0), then save the bitmap. However, whenever I do this, the character is rendered at x = 8 (for size 42). The offset can be different per font size, it is 6 when the size is 36. This seems to be a bug related to GDI. How can I query this value that is creating this offset? I tried "GetCharABCWidthsFloat", but the left and right bearings will be zero. I tried "GetTextMetrics", but that does not help. I noticed this article mentions it, but does not mention how to fix it http://support.microsoft.com/kb/307208

As a testing measure, I tried to render t at (-8, 0) and it ended up rendering at (0, 0). Does anyone know how I can get this offset value? I am not interested in hacking something together, but a real viable solution.

GetMeasureString, GetLineSpacing does not help.

Example Code:

static class Program
{
    static void Main(string[] args)
    {
        FontStyle style = FontStyle.Regular;
        FontFamily fontFamily = new FontFamily("Times New Roman");
        Font font = new Font("Times New Roman", 48, style);

        Bitmap bitmap = new Bitmap(64, 64);
        Graphics graphic = Graphics.FromImage(bitmap);

        SolidBrush blackBrush = new SolidBrush(Color.Black);
        graphic.FillRectangle(blackBrush, 0, 0, 64, 64);
        graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

        SizeF size = graphic.MeasureString("t", font);
        float lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

        graphic.DrawString("t", font, Brushes.White, new Point(0, 0));
        bitmap.Save("t.png");
        graphic.Dispose();
    }
}

1 Answer 1

4

Try StringFormat.GenericTypographic:

graphic.DrawString("t", font, Brushes.White, new Point(0, 0), StringFormat.GenericTypographic);
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick! Any idea why it does not do that by default?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.