动态生成一个计数器图片

网上常能看到很多制作精美的计数器,很多效果都看上去很酷,但是这些图片往往是一张完整的图片而不是一系列数字图片的展现。如何用ASP.net实现动态生成这样一张计数器图片呢?

步骤如下:
1 制作数字0~9的小图片,可以制作多种风格的
2 每种风格的图片放在一个文件夹里
3 使用Graphics类将这些图片合成最后输出的图片
4 利用Bitmap保存图片,并以图片流形式输出 

测试工程截图:


代码如下:
这个页面也是没有前台的,所以只需要写入后台C#代码

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Imaging;
None.gif
using  System.Drawing.Drawing2D;
None.gif//made by hekui 20070212
None.gif
public  partial  class  Count : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strCount = string.Empty;
InBlock.gif        
string strStyle = string.Empty;
InBlock.gif
InBlock.gif        
if (Request.QueryString["count"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strCount 
= Request.QueryString["count"].ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
if (Request.QueryString["style"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strStyle 
= Request.QueryString["style"].ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strStyle 
= "number1";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//生成COUNT图片
InBlock.gif
        SetCountIMG(strCount, strStyle);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//生成COUNT图片
InBlock.gif
    private void SetCountIMG( string strCount,string strStyle )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//定义输出位图
InBlock.gif
        Bitmap bmp = new Bitmap(300,100);
InBlock.gif        
//定义绘图对象
InBlock.gif
        Graphics g = Graphics.FromImage(bmp);
InBlock.gif        
//描述矩形区域 
InBlock.gif
        Rectangle newRect = new Rectangle(00300100);
InBlock.gif        
//设置背景色
InBlock.gif
        g.FillRectangle(new SolidBrush(Color.White), newRect);
InBlock.gif        
InBlock.gif        
int intLength = strCount.Length;
InBlock.gif        
int intWidth = 0;
InBlock.gif        
for (int i = 0; i < intLength; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strNumberImg = Page.Server.MapPath("img/" + strStyle + "/" + strCount.Substring(i, 1+ ".jpg");
InBlock.gif            Bitmap newBitmap 
= new Bitmap(strNumberImg);
InBlock.gif            g.DrawImage(newBitmap, intWidth, 
0);
InBlock.gif            
InBlock.gif            
//每次描述的X坐标需要加上图片的宽度
InBlock.gif
            intWidth += newBitmap.Width; 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
InBlock.gif        bmp.Save(ms, ImageFormat.Gif);
InBlock.gif
InBlock.gif        
//输出图片
InBlock.gif
        Response.ClearContent();
InBlock.gif        Response.ContentType 
= "image/gif";
InBlock.gif        Response.BinaryWrite(ms.ToArray());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

示例代码:/Files/heekui/RandCode_2.rar
(合在了一个以前的生成验证码图片的项目里)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值