生成图片代码

  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();

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值