Why is it not possible to save any font as image?

2019-05-04 16:56发布

问题:

Im a little bit confused because I can display every string with every font on my windows form but as an image it is not always possible. Maybe there is just something wrong with my code. But let me show you what I was trying.

At first I have this:

    Label l = new Label();

    l.Text = "Ì CSharp Î";

    this.Font = new Font("Code 128", 80);

    l.Size = new System.Drawing.Size(300, 200);

    this.Controls.Add(l);
    this.Size = new Size(300, 200);

Well this is very fine. Now I would like to try to save the same string with the same font as image. I found this code and I thought thats how to do this

        private static Image DrawText(string text, Font font, Color textColor, Color backColor)
        {
            //first, create a dummy bitmap just to get a graphics object
            Image img = new Bitmap(1, 1);
            Graphics drawing = Graphics.FromImage(img);

            //measure the string to see how big the image needs to be
            SizeF textSize = drawing.MeasureString(text, font);

            //free up the dummy image and old graphics object
            img.Dispose();
            drawing.Dispose();

            //create a new image of the right size
            img = new Bitmap((int)textSize.Width, (int)textSize.Height);

            drawing = Graphics.FromImage(img);

            //paint the background
            drawing.Clear(backColor);

            //create a brush for the text
            Brush textBrush = new SolidBrush(textColor);

            drawing.DrawString(text, font, textBrush, 0, 0);
            drawing.Save();
            textBrush.Dispose();
            drawing.Dispose();

            return img;
        }

        var i = DrawText("Ì CSharp Î", new Font("Code 128", 40), Color.Black, Color.White);

If I save the image I get this:

I dont get it. Im using the same string and the same font as I used it on my windows form. Why is that so? And how to avoid this problem?

PS: The font that Im using was downloaded here but I tested it with other fonts too and its not always working.

回答1:

Well, this is rather odd, but you are not using the same code that Label uses to draw text. The Label control uses TextRenderer.DrawText() by default, a function that pinvokes a GDI function (DrawTextEx). Your Graphic.DrawString() call invokes a GDI+ function which uses a fundamentally different text rendering engine. It has some layout problems, that's why TextRenderer got added to .NET 2.0

I'm not aware of these two functions mapping fonts differently. But who knows, this is not exactly a standard font. Use TextRenderer instead. The Label's DrawToBitmap() method is a fall-back solution.



回答2:

It seams that the fonts that you downloaded are not working. try this diffrent version of fonts from the same author that build the one you have installed. First delete the old font "Code 128" from c:\windows\fonts and then drag n drop the new in the same folder.



回答3:

http://msdn.microsoft.com/en-us/library/164w6x6z.aspx

states that

If the familyName parameter specifies a font that is not installed on the machine running the application or is not supported, Microsoft Sans Serif will be substituted

I think you need to satisfy yourself the font you get from new Font("Code 128", 40) is of the family you specified. Are you running this code on the same system? Is the font installed or stored localy with the program? Is the font actually available in both cases?

I would try this to test:

Label l = new Label();
l.Text = "Ì CSharp Î";
this.Font = new Font("Code 128", 80);
l.Size = new System.Drawing.Size(300, 200);
this.Controls.Add(l);
this.Size = new Size(300, 200);
var i = DrawText(l.Text, this.Font, Color.Black, Color.White);

if the result still differ, well mmm ... will then need to think about it more!