自动生成字体图片

作公司门户网站,需要很多文字图片,采用程序生成,效果不错!

  string word = "";
  string fontSize;
  string fontFamily = "RockWell";

  Font font;

  float emSizeInGU;
  float ascentInGU;
  float descentInGU;
  float lineSpacingInGU;

  Graphics g;
  Bitmap img;

  private void DrawImage()
  {
   MemoryStream ms = new MemoryStream();
   img.Save(ms,ImageFormat.Gif);
  
   Response.ClearContent();
   Response.ContentType = "image/Gif";
   Response.BinaryWrite(ms.ToArray());
   g.Dispose();
   img.Dispose();
   Response.End();
  }

  private void CreateFontImage()
  {
   SizeF sf = GetWordSize();
   RectangleF rf = new RectangleF(0,-descentInGU,sf.Width,sf.Height - descentInGU);
   img = new Bitmap((int)sf.Width, (int)(sf.Height-2*descentInGU));
   g = Graphics.FromImage(img);
   StringFormat strf = new StringFormat();
   strf.Alignment = StringAlignment.Far;

   g.DrawString(word,font,Brushes.White,rf, strf);
   DrawImage();
  
  }

  private SizeF GetWordSize()
  {
   Bitmap imgTemp;
   int height = Convert.ToInt32(emSizeInGU * 1.5);
   int width = Convert.ToInt32(word.Length * emSizeInGU);
   imgTemp = new Bitmap(width,height);

   Graphics graphics = Graphics.FromImage(imgTemp);

   font = GetFont();
   SizeF sf = graphics.MeasureString(word,font);

   imgTemp.Dispose();
   graphics.Dispose();

   return sf;
  }

  private Font GetFont()
  {
   FontFamily ff = new FontFamily(fontFamily);

   emSizeInGU = float.Parse(fontSize);

   Font f = new Font(ff,emSizeInGU);

   int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
   int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
   int descentInDU = ff.GetCellDescent(FontStyle.Regular);
   int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);
 
   ascentInGU   = ascentInDU*(emSizeInGU/emSizeInDU);
   descentInGU   = descentInDU*(emSizeInGU/emSizeInDU);
   lineSpacingInGU  = lineSpacingInDU*(emSizeInGU/emSizeInDU);

   return f;
  }

  private void Page_Load(object sender, System.EventArgs e)
  {
   fontSize = HttpContext.Current.Request.QueryString["FontSize"];
   if (fontSize == null || fontSize == string.Empty)
    fontSize = "42";
   emSizeInGU = float.Parse(fontSize);

   word = HttpContext.Current.Request.QueryString["Word"];
   if (word == null || word == string.Empty)
    word = "Empty";

   fontFamily = HttpContext.Current.Request.QueryString["FontFamily"];
   if (fontFamily == null || fontFamily == string.Empty)
    fontFamily = "RockWell";

   CreateFontImage();

 

   // 在此处放置用户代码以初始化页面
  }  string word = "";
  string fontSize;
  string fontFamily = "RockWell";

  Font font;

  float emSizeInGU;
  float ascentInGU;
  float descentInGU;
  float lineSpacingInGU;

  Graphics g;
  Bitmap img;

  private void DrawImage()
  {
   MemoryStream ms = new MemoryStream();
   img.Save(ms,ImageFormat.Gif);
  
   Response.ClearContent();
   Response.ContentType = "image/Gif";
   Response.BinaryWrite(ms.ToArray());
   g.Dispose();
   img.Dispose();
   Response.End();
  }

  private void CreateFontImage()
  {
   SizeF sf = GetWordSize();
   RectangleF rf = new RectangleF(0,-descentInGU,sf.Width,sf.Height - descentInGU);
   img = new Bitmap((int)sf.Width, (int)(sf.Height-2*descentInGU));
   g = Graphics.FromImage(img);
   StringFormat strf = new StringFormat();
   strf.Alignment = StringAlignment.Far;

   g.DrawString(word,font,Brushes.White,rf, strf);
   DrawImage();
  
  }

  private SizeF GetWordSize()
  {
   Bitmap imgTemp;
   int height = Convert.ToInt32(emSizeInGU * 1.5);
   int width = Convert.ToInt32(word.Length * emSizeInGU);
   imgTemp = new Bitmap(width,height);

   Graphics graphics = Graphics.FromImage(imgTemp);

   font = GetFont();
   SizeF sf = graphics.MeasureString(word,font);

   imgTemp.Dispose();
   graphics.Dispose();

   return sf;
  }

  private Font GetFont()
  {
   FontFamily ff = new FontFamily(fontFamily);

   emSizeInGU = float.Parse(fontSize);

   Font f = new Font(ff,emSizeInGU);

   int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
   int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
   int descentInDU = ff.GetCellDescent(FontStyle.Regular);
   int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);
 
   ascentInGU   = ascentInDU*(emSizeInGU/emSizeInDU);
   descentInGU   = descentInDU*(emSizeInGU/emSizeInDU);
   lineSpacingInGU  = lineSpacingInDU*(emSizeInGU/emSizeInDU);

   return f;
  }

  private void Page_Load(object sender, System.EventArgs e)
  {
   fontSize = HttpContext.Current.Request.QueryString["FontSize"];
   if (fontSize == null || fontSize == string.Empty)
    fontSize = "42";
   emSizeInGU = float.Parse(fontSize);

   word = HttpContext.Current.Request.QueryString["Word"];
   if (word == null || word == string.Empty)
    word = "Empty";

   fontFamily = HttpContext.Current.Request.QueryString["FontFamily"];
   if (fontFamily == null || fontFamily == string.Empty)
    fontFamily = "RockWell";

   CreateFontImage();

 

   // 在此处放置用户代码以初始化页面
  }

发表于 2004年5月19日 11:22

 

你可能感兴趣的:(收藏经典资料,float,string,null,object)