C# 制作39条形码

public void aspnet_barcode()
    {
        HttpContext c = new HttpContext(Request, Response);
        //获取生成条形码的高度
        if (c.Request.QueryString["height"] != null)
        {
            _height = Convert.ToInt32(c.Request.QueryString["height"].ToString());
        }
        //获取生成条形码的编号
        if (c.Request.QueryString["barcode"] != null)
        {
        }
        //调用自定义BarCode方法,打印出条形码
        barcode = BarCode("*"+Session["receipt"].ToString ()+"*", 1);
        //barcode = BarCode(Label1.Text, 1);
        int p_l = barcode.Length;
        int p_h = _height ;
        //输出生成条形码的图片
        c.Response.ContentType = "image/gif";
        Bitmap myBitmap = new Bitmap(p_l, p_h);
        //创建一个画布
        Graphics myGrap = Graphics.FromImage(myBitmap);
        //清空背景色
        myGrap.Clear(Color.White);
        for (int i = 0; i < p_l; i++)
        {
            //创建画笔对象
            Pen myPen = new Pen(Color.White, 1);
            if (barcode.Substring(i, 1) == "|")
            {
                myPen.Color = Color.Black;
            }
            //绘制一条连接由坐标对指定的两点连线
            myGrap.DrawLine(myPen, i, 0, i, _height);
        }
        //myGrap.DrawString(Session["receipt"].ToString(), new Font("Courier New", 10), new SolidBrush(Color.Black), -4, _height);
        //将此图像以指定格式保存到数据流中
        myBitmap.Save(c.Response.OutputStream, ImageFormat.Gif);
        string savePath = Server.MapPath("Receipt/" + Session["receipt"] + ".jpg");

        if (!System.IO.File.Exists(savePath))
        {
            myBitmap.Save(savePath);
            c.Response.End();
          
        }
        
    }
    public static string BarCode(object str, int type_code)
    {
        #region
        string strTmp = str.ToString();
        string code = strTmp;
        strTmp = strTmp.ToLower();
        strTmp = strTmp.Replace("0", "_|_|__||_||_|"); ;
        strTmp = strTmp.Replace("1", "_||_|__|_|_||");
        strTmp = strTmp.Replace("2", "_|_||__|_|_||");
        strTmp = strTmp.Replace("3", "_||_||__|_|_|");
        strTmp = strTmp.Replace("4", "_|_|__||_|_||");
        strTmp = strTmp.Replace("5", "_||_|__||_|_|");
        strTmp = strTmp.Replace("7", "_|_|__|_||_||");
        strTmp = strTmp.Replace("6", "_|_||__||_|_|");
        strTmp = strTmp.Replace("8", "_||_|__|_||_|");
        strTmp = strTmp.Replace("9", "_|_||__|_||_|");
        strTmp = strTmp.Replace ("*","_|__|_||_||_|");
        if (type_code == 1)
        {
            return strTmp + "<BR>" + code;
        }
        else
        {
            return strTmp;
        }
        #endregion
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值