0

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

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

这种字体映射的示例

这是我这样做的尝试。但是,当我运行它时,角色不在矩形的中间,而且看起来它们似乎更贴近矩形的左上角,考虑到当你穿过不同的地图时它们会非常跳跃。

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

                    }
                }
            }

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

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

4

1 回答 1

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)
                {
                }
            }
        }
于 2013-06-11T18:15:08.567 回答