代码之家  ›  专栏  ›  技术社区  ›  pfinferno

如何返回字体族给定索引的字符

  •  0
  • pfinferno  · 技术社区  · 6 年前

    我有一个字体家族名称和一个特定字符的索引。

    示例:我有字体族“Wingdings 2”和索引33。如果你去的话 http://www.alanwood.net/demos/wingdings-2.html 看第一项,索引33,这个字符是一支圆珠笔。

    我的问题是,如何在C中检索字符本身?我需要在我的应用程序中画这个字符。

    编辑:我知道如何使用图形对象绘制字符,问题是首先检索字符,只知道字体族和给定字体族中字符的索引。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sebastian L    6 年前

    要绘制字符,可以使用以下代码片段:

    public static Image DawTextFromFontFamily(string text, FontFamily family, Color textColor, Color backColor)
    {
         return DrawText(text, new Font(family, 16), textColor, backColor);
    }
    
    public 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;
    }
    

    现在你有了一个图像,任何字符,你需要的字体