现在的位置: 首页 > 综合 > 正文

[原]ASP.NET 下生成的条形码。

2012年11月04日 ⁄ 综合 ⁄ 共 3538字 ⁄ 字号 评论关闭
由于公司经常要用到条形码打印,但网上搜了好久,总没有几个好用的,有好用的也要很多大洋。没办法本人比较穷所以只能自己写个比较简单的。由于公司很多项目都是不同语言写的,又都基于b/s系统,经常要用到这个条形码,所以我写成的一个ashx文件,并用传入参数的方法来调用

http://www.dubox.cn/test/TuImage.ashx?code=125451ABDCDER4144444&height=60 这里是效果共有两个参数,自己看吧
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 = "";
    
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;
    }


}

抱歉!评论已关闭.