显示字体文件中的字形

时间:2012-11-11 12:45:58

标签: c#-4.0 fonts glyph

处理一个Windows应用程序(.Net 4.0),它将接受字体文件名作为输入,并将返回字体中每个字形的图像。尝试了所有可能的Google内容,但没有获得所有字形图像。如果获得的图像也有一些错误的输出。请告诉我如何做到这一点...目前有以下工作...

private void generateBitmaps(string strFontpath)
        {
            ////Generate font object
            GlyphTypeface font = new GlyphTypeface(new Uri(strFontpath));
            int fontSize = 22;
            int intGlyphcount = 0;
            //Collect geometry of all glyphs
            var Glyphs = from glyphIndex in font.CharacterToGlyphMap.Values
                         select font.GetGlyphOutline(glyphIndex, fontSize, 1d);
            // now create the visual we'll draw them to
            DrawingVisual viz = new DrawingVisual();
            System.Drawing.Size cellSize = new System.Drawing.Size(fontSize, Convert.ToInt32(fontSize * font.Height));
            int bitWidth = (int)Math.Ceiling(Convert.ToDecimal(cellSize.Width*10));
            int bitHeight = (int)Math.Ceiling(Convert.ToDecimal(cellSize.Height * 10));

            //using (DrawingContext dc = viz.RenderOpen())
            {
                foreach (var g in Glyphs)
                {
                    if (intGlyphcount > font.GlyphCount)
                        break;
                    //if (g.IsEmpty())
                    //    continue; // don't draw the blank ones
                    DrawingContext dc = viz.RenderOpen();                   
                    dc.PushTransform(new TranslateTransform());
                    System.Windows.Media.Pen glyphPen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 1);
                    dc.DrawGeometry(System.Windows.Media.Brushes.Red, glyphPen, g);
                    //GeometryDrawing glyphDrawing = new GeometryDrawing(System.Windows.Media.Brushes.White, glyphPen, g);
                    //DrawingImage geometryImage = new DrawingImage(glyphDrawing);
                    //dc.DrawImage(geometryImage, new Rect(0, 0, 150, 150));
                    dc.Close();
                    //geometryImage.Freeze();
                    //dc.Pop(); // get rid of the transform
                    RenderTargetBitmap bmp = new RenderTargetBitmap(
                                                            200, 200,
                                                            96, 96,
                                                            PixelFormats.Pbgra32);
                    bmp.Render(viz);
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bmp));
                    using (FileStream file = new FileStream(@"GlyphList\Glyph" + intGlyphcount++ + ".png", FileMode.Create))
                        encoder.Save(file);

                    //dc.Pop();
                }

            }
            MessageBox.Show("Done");
        }

1 个答案:

答案 0 :(得分:0)

如果您在字体中使用字形作为图像使用外部dll 这个dll可以在网址中找到 http://www.gnostice.com/XtremeFontEngine_dot_NET.asp