条形码通用类[转载]

生成验证码图片的ASP.NET示例
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;

public class TuImage : IHttpHandler
{
    
protected int _height = 30;
    
protected string _code = "0002bfft6280824";
    
protected string code = "";

    
/*调用方法
     TuImage tu = new TuImage();

            
            //contex(Context.Request.Url.Host+"default.aspx?height=20&code=jijfadlksajf");
           // Response.Redirect("default.aspx?height=20&code=jijfadlksajf");
            HttpContext context = HttpContext.Current;
            tu.ProcessRequest(context);
     
*/

    
public void ProcessRequest(HttpContext context)
    {
        
if (context.Request.QueryString["height"!= null)
        {
            _height 
= Convert.ToInt32(context.Request.QueryString["height"].ToString());
        }
        
if (context.Request.QueryString["code"!= null)
        {
            _code 
= context.Request.QueryString["code"].ToString();
        }
        code 
= getCodeText(_code);
        
int p_w = code.Length;
        
int p_h = _height + 20;
        context.Response.ContentType 
= "image/gif";
        Bitmap myBitmap 
= new Bitmap(p_w, p_h);

        Graphics myGrap 
= Graphics.FromImage(myBitmap);
        myGrap.Clear(Color.White);

        
for (int i = 0; i < p_w; i++)
        {
            Pen myPen 
= new Pen(Color.White, 1);
            
if (code.Substring(i, 1== "|")
            {
                myPen.Color 
= Color.Black;
            }
            
// myGrap.DrawString(_code.Substring(i, 1), new Font("宋体", 12), new SolidBrush(Color.Black), i*13, 20);
            myGrap.DrawLine(myPen, i, 0, i, _height);
        }

        myGrap.DrawString(_code, 
new Font("Courier New"10), new SolidBrush(Color.Black), -4, _height);
        myBitmap.Save(context.Response.OutputStream, ImageFormat.Gif);
        context.Response.End();
    }

    
public bool IsReusable
    {
        
get { return true; }
    }

    
private string getCodeText(string n)
    {
        
string zf = n.ToLower();
        zf 
= zf.Replace("0""_|_|__||_||_|");
        zf 
= zf.Replace("1""_||_|__|_|_||");
        zf 
= zf.Replace("2""_|_||__|_|_||");
        zf 
= zf.Replace("3""_||_||__|_|_|");
        zf 
= zf.Replace("4""_|_|__||_|_||");
        zf 
= zf.Replace("5""_||_|__||_|_|");
        zf 
= zf.Replace("7""_|_|__|_||_||");
        zf 
= zf.Replace("6""_|_||__||_|_|");
        zf 
= zf.Replace("8""_||_|__|_||_|");
        zf 
= zf.Replace("9""_|_||__|_||_|");
        zf 
= zf.Replace("a""_||_|_|__|_||");
        zf 
= zf.Replace("b""_|_||_|__|_||");
        zf 
= zf.Replace("c""_||_||_|__|_|");
        zf 
= zf.Replace("d""_|_|_||__|_||");
        zf 
= zf.Replace("e""_||_|_||__|_|");
        zf 
= zf.Replace("f""_|_||_||__|_|");
        zf 
= zf.Replace("g""_|_|_|__||_||");
        zf 
= zf.Replace("h""_||_|_|__||_|");
        zf 
= zf.Replace("i""_|_||_|__||_|");
        zf 
= zf.Replace("j""_|_|_||__||_|");
        zf 
= zf.Replace("k""_||_|_|_|__||");
        zf 
= zf.Replace("l""_|_||_|_|__||");
        zf 
= zf.Replace("m""_||_||_|_|__|");
        zf 
= zf.Replace("n""_|_|_||_|__||");
        zf 
= zf.Replace("o""_||_|_||_|__|");
        zf 
= zf.Replace("p""_|_||_||_|__|");
        zf 
= zf.Replace("r""_||_|_|_||__|");
        zf 
= zf.Replace("q""_|_|_|_||__||");
        zf 
= zf.Replace("s""_|_||_|_||__|");
        zf 
= zf.Replace("t""_|_|_||_||__|");
        zf 
= zf.Replace("u""_||__|_|_|_||");
        zf 
= zf.Replace("v""_|__||_|_|_||");
        zf 
= zf.Replace("w""_||__||_|_|_|");
        zf 
= zf.Replace("x""_|__|_||_|_||");
        zf 
= zf.Replace("y""_||__|_||_|_|");
        zf 
= zf.Replace("z""_|__||_||_|_|");
        zf 
= zf.Replace("-""_|__|_|_||_||");
        zf 
= zf.Replace("*""_|__|_||_||_|");
        zf 
= zf.Replace("/""_|__|__|_|__|");
        zf 
= zf.Replace("%""_|_|__|__|__|");
        zf 
= zf.Replace("+""_|__|_|__|__|");
        zf 
= zf.Replace(".""_||__|_|_||_|");
        
return zf;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值