当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 如何用.NET技术在线生成网站LOGO

ASP.NET
asp.net实现C#代码加亮显示
如何显示在线人数和所在位置
ASP.net生成文字图片
ASP.NET提供文件下载函数
一个简单的ASP.NET Forms 身份认证
在ASP.NET中实现多文件上传
asp.net 2.0中使用sitemapDATAsource做页面导航
通过ASP.net程序创建域帐户故障
为ASP.NET封装的SQL数据库访问类
在ASP.NET中跟踪和恢复大文件下载
SQL存储过程在.NET数据库中的应用
对“学号”、“身份证”的数字分析
把.NET程序部署到没有安装.NET Framwork的机器上
ASP.NET中同时支持简体和繁体中文
几十个ASP.NET性能优化的常用方法
.NET环境下五种邮件发送解决方案
.NET开发中正则表达式中BUG一例
.NET反射、委托技术与设计模式
.net中Windows窗体间的数据交互
ADO.NET访问Oracle 9i存储过程(上)

ASP.NET 中的 如何用.NET技术在线生成网站LOGO


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 96 ::
收藏到网摘: n/a

也许大家一看标题就知道,又是老生常谈了,在线生成LOGO其实就是在线生成图片,原理听起来很简单:

1. new一个bitmap或类似之物;

2. 用一个graphic在上边画出你想要的东西;

3. 保存,显示出来,大功告成。

今天要说的是生成中的一些细节问题。没有真正做过,你可能永远也不知道有这样的问题。下边提到的问题和代码,希望对各位有所帮助。

本文的示例程序在http://www.ladysolution.cn/logo.aspx

一。 字体位置。

用不同的字体,如果通过计算字体高度来给字体定位是不精确的,不同的字体有不同的em baseline,而且descending 和 ascending 得出来的值几乎很难用来算精确高度,更麻烦的是如果字体是某人造的,那EM更靠不住,最大的问题是文字上方的空白目前我没有找到适合的公式来计算。我用的是比较笨的办法,计算精确的字体高度:

以下为引用的内容:

  private static int[] GetRealFontHeight(Bitmap bmp)

  {

  int width, height;

  int frequency = 2;// higher frequency gets lower performance.

  int[] ret = new int[2];

  Color c;

  bool goOut = false;

  for (height = 1; height < bmp.Height - 1; height += frequency)

  {

  for (width = 1; width < bmp.Width - 1; width += frequency)

  {

  c = bmp.GetPixel(width, height);

  if (c.Name.Length>0 && c.Name != "0")//got it!

  {

  ret[0] = height;

  goOut = true;

  break;

  }

  else

  {

  goOut = false;

  }

  }

  if (goOut)

  break;

  }

  goOut = false;

以下为引用的内容:

  for (height = bmp.Height - 1; height > 1; height -= frequency)

  {

  for (width = bmp.Width - 1; width > 1; width -= frequency)

  {

  c = bmp.GetPixel(width, height);

  if (c.Name.Length > 0 && c.Name != "0")

  {

  ret[1] = height;

  goOut = true;

  break;

  }

  else

  {

  goOut = false;

  }

  }

  if (goOut)

  break;

  }

  return ret;

  }

在画图之前,用这个方法确定精确的字体高度,这样的染色的时候才不至于走样。大家可以通过http://www.ladysolution.cn/logo.aspx 生成图片看一下效果。如果用GDI+自带的计算高度的方法,比如GetHeight()和Height属性,在blend的时候换个字体必定有误差。

二。 画倒影, RotateFlip依然是主力

倒影倒是很简单,把BLEND设好,定位好,画出来就行:

以下为引用的内容:

  Create mirror#region Create mirror

  ……。

  Graphics gBack_mirror = Graphics.FromImage(backImage_mirror);

  pStart = new Point(1, -pre[0]);

  pEnd = new Point(1, pre[1] - pre[0]);

  LinearGradientBrush backColor_mirror = new LinearGradientBrush(pStart, pEnd, Color.White, endColor);

  ……

  gBack_mirror.DrawString(logoText, logoFont, backColor_mirror, new PointF(。0f, -pre[0]));

  ……

  gBack.DrawImage(backImage_mirror, new Point(0, pre[1]+3));

  #endregion

三。 计算好你的下笔点,通过第一点中的方法,可以拿到某字体在某size时的实际高度有多少,这样我们在画笔或写字之前通过调用此方法就可以得出你的着笔点的坐标:

以下为引用的内容:

  private static int[] prerendText(int LogoWidth, int LogoHeight, string logoText, Font logoFont)

  {

  Bitmap bp = new Bitmap(LogoWidth, LogoHeight);

  Graphics g = Graphics.FromImage(bp);

  g.DrawString(logoText, logoFont, SystemBrushes.Info, new PointF(。0f, .0f));

  int[] ret = new int[2];

  ret = GetRealFontHeight(bp);

  g.Dispose();

  bp.Dispose();

  return ret;

}

此方法返回某字体在某size下的最上方坐标和最下方坐标,有了坐标就好办事了:

以下为引用的内容:

  int[] pre = prerendText(LogoWidth, LogoHeight, logoText, logoFont);

  ……

  Point pStart = new Point(1, pre[0]-2);

  Point pEnd = new Point(1, pre[1]+2);

  LinearGradientBrush backColor = new LinearGradientBrush(pStart, pEnd, startColor, endColor);

  return filename;

使用这种方法,画出来的图会比只用GDI+画出来更漂亮些,颜色更准确些。