加载字体并获取C#中的字符

时间:2014-01-22 17:53:12

标签: c# fonts bitmap character

我只需要知道如何加载字体文件并获取数据数组中的字符,然后调用一个特定的字符。

 var families = Fonts.GetFontFamilies(@"C:\WINDOWS\Fonts\Arial.TTF");
 foreach (FontFamily family in families)
 {

 }

1 个答案:

答案 0 :(得分:0)

希望这会给你一个想法(未经测试)。注意使用using或明确处理图形对象:

using System.Drawing; 
using System.Drawing.Imaging;
...

    // Create your bitmap - 100x100 pixels for example
        using (Bitmap b = new Bitmap(100, 100))
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                g.Clear(Color.White); // White background
                using (FontFamily fontFamily = new FontFamily("Arial"))
                {
                    using (Font font = new Font(fontFamily, 24, FontStyle.Regular, GraphicsUnit.Pixel))
                    {
                        using (SolidBrush solidBrush = new SolidBrush(Color.Red)) // Red text
                        {
                            g.DrawString("A", font, solidBrush, new PointF(10, 10)); // Draw an "A" at position 10,10
                        }
                    }
                }
            }
          b.Save(Response.OutputStream, ImageFormat.Jpeg); // return to response, for example
        }
    }