2012-11-11 62 views
2

在一個將接受字體文件名稱作爲輸入的Windows應用程序(.Net 4.0)上工作,並將返回字體中每個字形的圖像。嘗試了所有可能的谷歌的東西,但沒有得到所有字形的圖像。如果得到的圖片也存在一些錯誤。請建議我做這件事的方式。目前有以下工作了...從字體文件中顯示字形

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"); 
     } 

回答