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

时间:2013-06-11 13:21:57

标签: c# .net graphics fonts drawstring

我正在尝试使用C#的Graphics类生成字体映射。

字符应完全位于矩形的中间,以便以后使用它们。其次,我想使用最大的字体大小,所有字符都适合他们的盒子。

Example of such a font-map

这是我尝试这样做的。然而,当我运行它时,字符不在它们的矩形的中间,看起来它们相当附着在它的左上角,考虑到当你通过不同的地图时它们非常跳跃。 / p>

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

                    }
                }
            }

如何让角色完美地融入矩形的中间?

编辑:显然,一种字体的所有字符都必须使用相同的字体大小。

1 个答案:

答案 0 :(得分:2)

这就是我想出的......

        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)
                {
                }
            }
        }