宝玉

专注于web开发技术
随笔 - 114, 评论 - 1503 , 引用 - 581

自动生成字体图片

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

  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

评论

# re: 自动生成字体图片

不错不错
以前我都用java/com来做,学习学习asp.net,谢谢!

不过最好还是写成一个class比较好些,尤其作为例子 :)

hoho,毕竟很多初学者虽然会很容易理解,但却养成了不好的习惯。
2004-7-20 15:14 | piggybank

# re: 自动生成字体图片

汗颜!有待改进!
2004-7-20 15:16 | 宝玉

# re: 自动生成字体图片

好真他吗的好
2004-10-15 11:09 | 超子

# re: 自动生成字体图片

有个问题想请教.
我用SQL里的数据填充的DATAGRID,在AutoGenerateColumns="true"
时,一切正常,但我使用自定义的模版AutoGenerateColumns="false"时,
MyDataGrid.SelectedItem.Cells[2].Text;
不能正常或得值.
AutoGenerateColumns="true"时,
MyDataGrid.SelectedItem.Cells[2].Text;
不能正常或得值.
自定义模版SelectedItem.Cells[2].Text不能正常获得值,但自动的那部分却能正常取得值,请教一下这是为啥啊?
我要怎么改?
2004-9-6 23:20 | TINS

# re: 自动生成字体图片

唉,奇怪的问题,平常不喜欢求人的,不过,这个也太奇怪了,很简单的代码,简单到不能出错~~~

//下面是写在Load里的,大家帮帮忙吧
Response.ContentType = "image/Jpeg";
Bitmap bm = new Bitmap ( 600 , 250 ) ;
//创建一个长度为600,宽带为250的Bitmap实例
Graphics g ;
g = Graphics.FromImage ( bm ) ;
//由此Bitmap实例创建Graphic实例
g . Clear ( Color . Snow ) ;
//在绘画图面的指定位置,以指定的字体、指定的颜色绘制指定的字符
g . DrawString ( "aAdmin" , new Font ( "MS Sans Serif" , 8 ) , Brushes . Black , new Point ( 525 , 12 ) ) ;
bm.Save ( Response . OutputStream ,System.Drawing.Imaging.ImageFormat.Gif ) ;
bm.Save(@"C:\Temp\a.jpg",System.Drawing.Imaging.ImageFormat.Jpeg );
//
上面的程序生成的图片上面竟然是乱码~!。。。
2004-12-22 4:51 | deneb

# re: 自动生成字体图片

SDFSDFDS
2005-4-23 6:24 | 幻想

# re: 自动生成字体图片

刘建新
2005-6-20 21:57 | 刘建新

# re: 自动生成字体图片

急!求助!怎么做啊?我不明白,是不是可以把一张图片用文字表现出来?我现在正需要这种图片!老大帮忙啦,我QQ是404936161。谢谢
2005-7-29 21:40 | bravery

# re: 自动生成字体图片

欢迎
2005-9-9 8:49 |

# re: 自动生


唉,奇怪的问题,平常不喜欢求人的,不过,这个也太奇怪了,很简单的代码,简单到不能出错~~~

//下面是写在Load里的,大家帮帮忙吧
Response.ContentType = "image/Jpeg";
Bitmap bm = new Bitmap ( 600 , 250 ) ;
//创建一个长度为600,宽带为250的Bitmap实例
Graphics g ;
g = Graphics.FromImage ( bm ) ;
//由此Bitmap实例创建Graphic实例
g . Clear ( Color . Snow ) ;
//在绘画图面的指定位置,以指定的字体、指定的颜色绘制指定的字符
g . DrawString
2005-9-27 20:59 | 成字体图片

# re: 自动生成字体图片

为什么图片背景是黑色的,能不能换成白色啊?
2005-12-16 4:20 | 不清楚

# re: 自动生成字体图片

2005-12-22 15:39 | 12321

# re: 自动生成字体图片

这东西怎么弄啊

2006-2-9 4:41 | fang

# re: 自动生成字体图片

';.;.
2007-2-22 10:39 |

# -「此秂已葬"

-「此秂已葬"
2007-11-27 1:30 | -「此秂已葬"

# re: 自动生成字体图片

sdfasfaf我我往往我我
2008-3-2 14:21 | 符芳钦

# re: 自动生成字体图片

白衣飘飘的年华,驻守一方或漂泊他乡,都不过是生命里一个多愁的季节。小小的季动,轻轻的乡愁,就这样缓缓滑过青春时光,投下最美的记忆。
20岁,像一根即将飘落的羽毛,一株等待静放的花朵,无论是逃离喧嚣的城市,还是辗转投进霓虹的华彩,都是一份对温暖的独自渴望。
只有当我们独处时,我们才会更清楚地意识到,我们与万物同在。我们都需要更多独处的时间。生活中充满哲理,等待我们去发现。
2008-3-5 21:44 | 222

# re: 自动生成字体图片

SSDA B
2008-5-29 12:49 |

Post Comment

主题  
姓名  
主页
校验码  
内容   
京ICP备 05050892号