为什么无法保存任何字体的形象呢? (但是,在我的窗口形式显示出来) [英] Why is it not possible to save any font as image? (But to display it on my windows form)

查看:198
本文介绍了为什么无法保存任何字体的形象呢? (但是,在我的窗口形式显示出来)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个有点困惑,因为我可以显示每个字符串,每个字体在我的Windows形式,但作为一个形象并不总是可能的。也许有只是一些问题我的code。但让我告诉你什么,我想。

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.

起初,我有这样的:

    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);

嗯,这是非常精细的。现在,我想尽量节省相同的字符串具有相同的字体图像。我发现这个code,我想这就是如何做到这一点

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:

我不明白这一点。使用im相同的字符串和相同的字体,因为我用它在我的Windows窗体。为什么会这样?而如何避免这个问题?

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:即时通讯使用的字体被下载 但我与其他字体测试它也和它并不总是工作。

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

推荐答案

那么,这是相当奇怪的,但你没有使用相同的code的标签用来绘制文本。 Label控件使用TextRenderer.DrawText()默认情况下,一个函数,pinvokes GDI函数(DrawTextEx)。您Graphic.DrawString()调用调用GDI +功能,它采用了完全不同的文本渲染引擎。它有一些布局的问题,这就是为什么TextRenderer得到了加入.NET 2.0

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

我不知道这两个函数映射字体不同。但谁知道,这不完全是一个标准的字体。使用TextRenderer代替。标签的DrawToBitmap()方法是一个后备的解决方案。

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.

这篇关于为什么无法保存任何字体的形象呢? (但是,在我的窗口形式显示出来)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆