网上常能看到很多制作精美的计数器,很多效果都看上去很酷,但是这些图片往往是一张完整的图片而不是一系列数字图片的展现。如何用ASP.net实现动态生成这样一张计数器图片呢?
步骤如下:
1 制作数字0~9的小图片,可以制作多种风格的
2 每种风格的图片放在一个文件夹里
3 使用Graphics类将这些图片合成最后输出的图片
4 利用Bitmap保存图片,并以图片流形式输出
测试工程截图:
代码如下:
这个页面也是没有前台的,所以只需要写入后台C#代码
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
//made by hekui 20070212
public partial class Count : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCount = string.Empty;
string strStyle = string.Empty;
if (Request.QueryString["count"] != null)
{
strCount = Request.QueryString["count"].ToString();
}
if (Request.QueryString["style"] != null)
{
strStyle = Request.QueryString["style"].ToString();
}
else
{
strStyle = "number1";
}
//生成COUNT图片
SetCountIMG(strCount, strStyle);
}
//生成COUNT图片
private void SetCountIMG( string strCount,string strStyle )
{
//定义输出位图
Bitmap bmp = new Bitmap(300,100);
//定义绘图对象
Graphics g = Graphics.FromImage(bmp);
//描述矩形区域
Rectangle newRect = new Rectangle(0, 0, 300, 100);
//设置背景色
g.FillRectangle(new SolidBrush(Color.White), newRect);
int intLength = strCount.Length;
int intWidth = 0;
for (int i = 0; i < intLength; i++)
{
string strNumberImg = Page.Server.MapPath("img/" + strStyle + "/" + strCount.Substring(i, 1) + ".jpg");
Bitmap newBitmap = new Bitmap(strNumberImg);
g.DrawImage(newBitmap, intWidth, 0);
//每次描述的X坐标需要加上图片的宽度
intWidth += newBitmap.Width;
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, ImageFormat.Gif);
//输出图片
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
//made by hekui 20070212
public partial class Count : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCount = string.Empty;
string strStyle = string.Empty;
if (Request.QueryString["count"] != null)
{
strCount = Request.QueryString["count"].ToString();
}
if (Request.QueryString["style"] != null)
{
strStyle = Request.QueryString["style"].ToString();
}
else
{
strStyle = "number1";
}
//生成COUNT图片
SetCountIMG(strCount, strStyle);
}
//生成COUNT图片
private void SetCountIMG( string strCount,string strStyle )
{
//定义输出位图
Bitmap bmp = new Bitmap(300,100);
//定义绘图对象
Graphics g = Graphics.FromImage(bmp);
//描述矩形区域
Rectangle newRect = new Rectangle(0, 0, 300, 100);
//设置背景色
g.FillRectangle(new SolidBrush(Color.White), newRect);
int intLength = strCount.Length;
int intWidth = 0;
for (int i = 0; i < intLength; i++)
{
string strNumberImg = Page.Server.MapPath("img/" + strStyle + "/" + strCount.Substring(i, 1) + ".jpg");
Bitmap newBitmap = new Bitmap(strNumberImg);
g.DrawImage(newBitmap, intWidth, 0);
//每次描述的X坐标需要加上图片的宽度
intWidth += newBitmap.Width;
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, ImageFormat.Gif);
//输出图片
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
}
}
示例代码:/Files/heekui/RandCode_2.rar
(合在了一个以前的生成验证码图片的项目里)