简体   繁体   English

C#将字体放在矩形中间,最大拟合尺寸

[英]C# Position font in middle of rectangle with the biggest fitting size

I'm trying to generate a font-map using C#'s Graphics class. 我正在尝试使用C#的Graphics类生成字体映射。

The characters are supposed to be perfectly in the middle of their rectangle in order to use them afterwards. 字符应该完美地位于矩形的中间,以便之后使用它们。 Secondly, I want to use the biggest font-size with all characters fitting into their box. 其次,我想使用最大的字体大小,所有字符都适合他们的盒子。

这种字体映射的示例

This is my attempt at doing so. 这是我尝试这样做的。 However, when I run this, the characters are not in the middle of their rectangle and it seems like they're rather attached to the upper-left corner of it, considering they're pretty jump when you go trough the different maps. 然而,当我运行它时,字符不在它们的矩形的中间,看起来它们相当附着在它的左上角,考虑到当你通过不同的地图时它们非常跳跃。

foreach (String FontName in DataHandler.GetFonts())
            {
                foreach (FontStyle Style in Enum.GetValues(typeof(FontStyle)))
                {
                    try
                    {
                        Bitmap map = new Bitmap(585, 559);
                        Graphics g = Graphics.FromImage(map);

                        for (int i = 0; i < charOrder.Length; i++)
                        {
                            string character = charOrder.Substring(i, 1);
                            g.DrawString(character, new Font(FontName, 30 / new Font(FontName, 20).FontFamily.GetEmHeight(Style), Style), new SolidBrush(myColor), new RectangleF(new PointF((i % charactersPerRow) * 40, ((i - (i % charactersPerRow)) / charactersPerRow) * 80), new SizeF(40, 80)));
                        }
                        map.Save(OutputPath + "\\" + Style.ToString() + "_" + FontName + ".png");
                    }
                    catch (Exception)
                    {

                    }
                }
            }

How would I make the characters fit perfectly into in the middle of their rectangle? 如何让角色完美地融入矩形的中间?

EDIT: All characters of one font would have to use the same font-size, obviously. 编辑:显然,一种字体的所有字符都必须使用相同的字体大小。

Here's what I came up with... 这就是我想出来的......

        int charactersPerRow = 14;
        string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-=~!@#$%^&*()_+,./;'[]\\<>?:\"{}|";
        int rows = (int)Math.Ceiling((decimal)chars.Length / (decimal)charactersPerRow);

        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Center;
        sf.LineAlignment = StringAlignment.Center;

        foreach (String FontName in DataHandler.GetFonts())
        {
            foreach (FontStyle Style in Enum.GetValues(typeof(FontStyle)))
            {
                try
                {
                    Bitmap map = new Bitmap(585, 559);
                    using (Graphics g = Graphics.FromImage(map))
                    {
                        // each char must fit into this size:
                        SizeF szF = new SizeF(map.Width / charactersPerRow, map.Height / rows);

                        // fallback font and size
                        int fntSize = 8;
                        Font fnt = new Font(FontName, fntSize, Style);

                        // figure out the largest font size that will fit into "szF" above:
                        bool smaller = true;
                        while (smaller)
                        {
                            Font newFnt = new Font(FontName, fntSize, Style);
                            for (int i = 0; i < chars.Length; i++)
                            {
                                SizeF charSzF = g.MeasureString(chars[i].ToString(), newFnt);
                                if (charSzF.Width > szF.Width || charSzF.Height > szF.Height)
                                {
                                    smaller = false;
                                    break;
                                }
                            }
                            if (smaller)
                            {
                                if (fnt != null)
                                {
                                    fnt.Dispose();
                                }
                                fnt = newFnt;
                                fntSize++;
                            }
                        }

                        // draw each char at the appropriate location:
                        using (SolidBrush brsh = new SolidBrush(myColor))
                        {
                            for (int i = 0; i < chars.Length; i++)
                            {
                                PointF ptF = new PointF(
                                    (float)(i % charactersPerRow) / (float)charactersPerRow * map.Width,
                                    ((float)((int)(i / charactersPerRow)) / (float)rows) * map.Height);
                                g.DrawString(chars[i].ToString(), fnt, brsh, new RectangleF(ptF, szF), sf);
                            }
                        }
                        map.Save(OutputPath + "\\" + Style.ToString() + "_" + FontName + ".png");
                    }
                }
                catch (Exception)
                {
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM