当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net(C#)生成Code39条形码实例 条码枪可以扫描出

asp.net(C#)生成Code39条形码实例 条码枪可以扫描出

2017年12月12日  | 移动技术网IT编程  | 我要评论

宝马被砸,南美洲地形图,嘘禁止想象完整版

复制代码 代码如下:

using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.data;

namespace mscl
{
    /// <summary>
    /// 生成条码code39
    /// </summary>
    public class code39
    {
      private hashtable m_code39 = new hashtable();

        private byte m_magnify = 0;
        /// <summary>
        /// 放大倍数
        /// </summary>
        public byte magnify { get { return m_magnify; } set { m_magnify = value; } }

        private int m_height = 40;
        /// <summary>
        /// 图形高
        /// </summary>
        public int height { get { return m_height; } set { m_height = value; } }

        private font m_viewfont = null;
        /// <summary>
        /// 字体大小
        /// </summary>
        public font viewfont { get { return m_viewfont; } set { m_viewfont = value; } }

 

        public code39()
        {

            m_code39.add("a", "1101010010110");
            m_code39.add("b", "1011010010110");
            m_code39.add("c", "1101101001010");
            m_code39.add("d", "1010110010110");
            m_code39.add("e", "1101011001010");
            m_code39.add("f", "1011011001010");
            m_code39.add("g", "1010100110110");
            m_code39.add("h", "1101010011010");
            m_code39.add("i", "1011010011010");
            m_code39.add("j", "1010110011010");
            m_code39.add("k", "1101010100110");
            m_code39.add("l", "1011010100110");
            m_code39.add("m", "1101101010010");
            m_code39.add("n", "1010110100110");
            m_code39.add("o", "1101011010010");
            m_code39.add("p", "1011011010010");
            m_code39.add("q", "1010101100110");
            m_code39.add("r", "1101010110010");
            m_code39.add("s", "1011010110010");
            m_code39.add("t", "1010110110010");
            m_code39.add("u", "1100101010110");
            m_code39.add("v", "1001101010110");
            m_code39.add("w", "1100110101010");
            m_code39.add("x", "1001011010110");
            m_code39.add("y", "1100101101010");
            m_code39.add("z", "1001101101010");
            m_code39.add("0", "1010011011010");
            m_code39.add("1", "1101001010110");
            m_code39.add("2", "1011001010110");
            m_code39.add("3", "1101100101010");
            m_code39.add("4", "1010011010110");
            m_code39.add("5", "1101001101010");
            m_code39.add("6", "1011001101010");
            m_code39.add("7", "1010010110110");
            m_code39.add("8", "1101001011010");
            m_code39.add("9", "1011001011010");
            m_code39.add("+", "1001010010010");
            m_code39.add("-", "1001010110110");
            m_code39.add("*", "1001011011010");
            m_code39.add("/", "1001001010010");
            m_code39.add("%", "1010010010010");
            //m_code39.add("contentquot;, "1001001001010");
            m_code39.add(".", "1100101011010");
            m_code39.add(" ", "1001101011010");

        }

        public enum code39model
        {
            /// <summary>
            /// 基本类别 1234567890abc
            /// </summary>
            code39normal,
            /// <summary>
            /// 全ascii方式 +a+b 来表示小写
            /// </summary>
            code39fullascii
        }
        /// <summary>
        /// 获得条码图形
        /// </summary>
        /// <param name="p_text">文字信息</param>
        /// <param name="p_model">类别</param>
        /// <param name="p_starchar">是否增加前后*号</param>
        /// <returns>图形</returns>
        public bitmap getcodeimage(string p_text, code39model p_model, bool p_starchar)
        {
            string _valuetext = "";
            string _codetext = "";
            char[] _valuechar = null;
            switch (p_model)
            {
                case code39model.code39normal:
                    _valuetext = p_text.toupper();
                    break;
                default:
                    _valuechar = p_text.tochararray();
                    for (int i = 0; i != _valuechar.length; i++)
                    {
                        if ((int)_valuechar[i] >= 97 && (int)_valuechar[i] <= 122)
                        {
                            _valuetext += "+" + _valuechar[i].tostring().toupper();

                        }
                        else
                        {
                            _valuetext += _valuechar[i].tostring();
                        }
                    }
                    break;
            }

            _valuechar = _valuetext.tochararray();

            if (p_starchar == true) _codetext += m_code39["*"];

            for (int i = 0; i != _valuechar.length; i++)
            {
                if (p_starchar == true && _valuechar[i] == '*') throw new exception("带有起始符号不能出现*");

                object _charcode = m_code39[_valuechar[i].tostring()];
                if (_charcode == null) throw new exception("不可用的字符" + _valuechar[i].tostring());
                _codetext += _charcode.tostring();
            }

            if (p_starchar == true) _codetext += m_code39["*"];

            bitmap _codebmp = getimage(_codetext);
            getviewimage(_codebmp, p_text);
            return _codebmp;
        }

 

        /// <summary>
        /// 绘制编码图形
        /// </summary>
        /// <param name="p_text">编码</param>
        /// <returns>图形</returns>
        private bitmap getimage(string p_text)
        {
            char[] _value = p_text.tochararray();

            //宽 == 需要绘制的数量*放大倍数 + 两个字的宽  
            bitmap _codeimage = new bitmap(_value.length * ((int)m_magnify + 1), (int)m_height);
            graphics _garphics = graphics.fromimage(_codeimage);

            _garphics.fillrectangle(brushes.white, new rectangle(0, 0, _codeimage.width, _codeimage.height));

            int _lenex = 0;
            for (int i = 0; i != _value.length; i++)
            {
                int _drawwidth = m_magnify + 1;
                if (_value[i] == '1')
                {
                    _garphics.fillrectangle(brushes.black, new rectangle(_lenex, 0, _drawwidth, m_height));

                }
                else
                {
                    _garphics.fillrectangle(brushes.white, new rectangle(_lenex, 0, _drawwidth, m_height));
                }
                _lenex += _drawwidth;
            }

 

            _garphics.dispose();
            return _codeimage;
        }
        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name="p_codeimage">图形</param>
        /// <param name="p_text">文字</param>
        private void getviewimage(bitmap p_codeimage, string p_text)
        {
            if (m_viewfont == null) return;
            graphics _graphics = graphics.fromimage(p_codeimage);
            sizef _fontsize = _graphics.measurestring(p_text, m_viewfont);

            if (_fontsize.width > p_codeimage.width || _fontsize.height > p_codeimage.height - 20)
            {
                _graphics.dispose();
                return;
            }
            int _starheight = p_codeimage.height - (int)_fontsize.height;

            _graphics.fillrectangle(brushes.white, new rectangle(0, _starheight, p_codeimage.width, (int)_fontsize.height));

            int _starwidth = (p_codeimage.width - (int)_fontsize.width) / 2;

            _graphics.drawstring(p_text, m_viewfont, brushes.black, _starwidth, _starheight);

            _graphics.dispose();

        }
    }

}


复制代码 代码如下:

<%@ webhandler language="c#" class="getc39handler" %>

using system;
using system.web;
using system.io;
using system.drawing;
using system.drawing.imaging;

public class getc39handler : ihttphandler {
    //获取code39处理程序
    public void processrequest (httpcontext context) {
        string orderno = context.request.params["orderno"];
        mscl.code39 _code39 = new mscl.code39();
        _code39.height = 60;
        _code39.magnify = 0;
        _code39.viewfont = new font("arial", 12);
        system.drawing.image _codeimage = _code39.getcodeimage(orderno, mscl.code39.code39model.code39normal, true);
        system.io.memorystream _stream = new system.io.memorystream();
        _codeimage.save(_stream, system.drawing.imaging.imageformat.jpeg);
        //_codeimage.save(server.mappath("/1.jpeg"));
        //_codeimage.save(server.mappath("/1.bmp"));
        //_codeimage.save(server.mappath("/1.gif"));

        context.response.contenttype = "image/tiff";
        context.response.clear();
        context.response.bufferoutput = true;
        context.response.binarywrite(_stream.getbuffer());
        context.response.flush();
    }   
    public bool isreusable {
        get {
            return false;
        }
    }
}


复制代码 代码如下:

        //调用显示订单条码
        image1.imageurl = "getc39handler.ashx?orderno=32134411";

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网