C#绘制条码CODE39和CODE39全码模式

C#绘制条码CODE39和CODE39全码模式http://blog.csdn.net/zgke/article/details/3503845C#绘制Code39和Code39全码模式BarCode:Code39.csusingSystem;usingSyste

C#绘制条码CODE39和CODE39全码模式

 

 

C#绘制Code39和Code39全码模式
BarCode:Code39.cs
using System;
using System.Collections;
using System.Drawing;
using System.Globalization;
namespace WG_30
{

    class Code39
    {

        private readonly Hashtable _mCode39 = new Hashtable();
        private byte _mMagnify;
        /// <summary>
        /// 放大倍数
        /// </summary>
        public byte Magnify { get { return _mMagnify; } set { _mMagnify = value; } }
        private int _mHeight = 40;
        /// <summary>
        /// 图形高
        /// </summary>
        public int Height { get { return _mHeight; } set { _mHeight = value; } }
        private Font _mViewFont;
        /// <summary>
        /// 字体大小
        /// </summary>
        public Font ViewFont { get { return _mViewFont; } set { _mViewFont = value; } }
        public Code39()
        {

            _mCode39.Add(“A”, “1101010010110”);
            _mCode39.Add(“B”, “1011010010110”);
            _mCode39.Add(“C”, “1101101001010”);
            _mCode39.Add(“D”, “1010110010110”);
            _mCode39.Add(“E”, “1101011001010”);
            _mCode39.Add(“F”, “1011011001010”);
            _mCode39.Add(“G”, “1010100110110”);
            _mCode39.Add(“H”, “1101010011010”);
            _mCode39.Add(“I”, “1011010011010”);
            _mCode39.Add(“J”, “1010110011010”);
            _mCode39.Add(“K”, “1101010100110”);
            _mCode39.Add(“L”, “1011010100110”);
            _mCode39.Add(“M”, “1101101010010”);
            _mCode39.Add(“N”, “1010110100110”);
            _mCode39.Add(“O”, “1101011010010”);
            _mCode39.Add(“P”, “1011011010010”);
            _mCode39.Add(“Q”, “1010101100110”);
            _mCode39.Add(“R”, “1101010110010”);
            _mCode39.Add(“S”, “1011010110010”);
            _mCode39.Add(“T”, “1010110110010”);
            _mCode39.Add(“U”, “1100101010110”);
            _mCode39.Add(“V”, “1001101010110”);
            _mCode39.Add(“W”, “1100110101010”);
            _mCode39.Add(“X”, “1001011010110”);
            _mCode39.Add(“Y”, “1100101101010”);
            _mCode39.Add(“Z”, “1001101101010”);
            _mCode39.Add(“0”, “1010011011010”);
            _mCode39.Add(“1”, “1101001010110”);
            _mCode39.Add(“2”, “1011001010110”);
            _mCode39.Add(“3”, “1101100101010”);
            _mCode39.Add(“4”, “1010011010110”);
            _mCode39.Add(“5”, “1101001101010”);
            _mCode39.Add(“6”, “1011001101010”);
            _mCode39.Add(“7”, “1010010110110”);
            _mCode39.Add(“8”, “1101001011010”);
            _mCode39.Add(“9”, “1011001011010”);
            _mCode39.Add(“+”, “1001010010010”);
            _mCode39.Add(“-“, “1001010110110”);
            _mCode39.Add(“*”, “1001011011010”);
            _mCode39.Add(“/”, “1001001010010”);
            _mCode39.Add(“%”, “1010010010010”);
            _mCode39.Add(“$”, “1001001001010”);
            _mCode39.Add(“.”, “1100101011010”);
            _mCode39.Add(” “, “1001101011010”);
        }
        public enum Code39Model
        {

            /// <summary>
            /// 基本类别 1234567890ABC
            /// </summary>
            Code39Normal,
            /// <summary>
            /// 全ASCII方式 +A+B 来表示小写
            /// </summary>
            Code39FullAscIi
        }
        /// <summary>
        /// 获得条码图形
        /// </summary>
        /// <param name=”pText”>文字信息</param>
        /// <param name=”pModel”>类别</param>
        /// <param name=”pStarChar”>是否增加前后*号</param>
        /// <returns>图形</returns>
        public Bitmap GetCodeImage(string pText, Code39Model pModel, bool pStarChar)
        {

            string valueText = “”;
            string codeText = “”;
            char[] valueChar;
            switch (pModel)
            {

                case Code39Model.Code39Normal:
                    valueText = pText.ToUpper();
                    break;
                default:
                    valueChar = pText.ToCharArray();
                    for (int i = 0; i != valueChar.Length; i++)
                    {

                        if ((int)valueChar[i] >= 97 && (int)valueChar[i] <= 122)
                        {

                            valueText += “+” + valueChar[i].ToString(CultureInfo.InvariantCulture).ToUpper();
                        }
                        else
                        {

                            valueText += valueChar[i].ToString(CultureInfo.InvariantCulture);
                        }
                    }
                    break;
            }
            valueChar = valueText.ToCharArray();
            if (pStarChar) codeText += _mCode39[“*”];
            for (int i = 0; i != valueChar.Length; i++)
            {

                if (pStarChar && valueChar[i] == ‘*’) throw new Exception(“带有起始符号不能出现*”);
                object charCode = _mCode39[valueChar[i].ToString(CultureInfo.InvariantCulture)];
                if (charCode == null) throw new Exception(“不可用的字符” + valueChar[i].ToString(CultureInfo.InvariantCulture));
                codeText += charCode.ToString();
            }
            if (pStarChar) codeText += _mCode39[“*”];
            Bitmap codeBmp = GetImage(codeText);
            GetViewImage(codeBmp, pText);
            return codeBmp;
        }
        /// <summary>
        /// 绘制编码图形
        /// </summary>
        /// <param name=”pText”>编码</param>
        /// <returns>图形</returns>
        private Bitmap GetImage(string pText)
        {

            char[] value = pText.ToCharArray();
            //宽 == 需要绘制的数量*放大倍数 + 两个字的宽
            Bitmap codeImage = new Bitmap(value.Length * ((int)_mMagnify + 1), (int)_mHeight);
            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 = _mMagnify + 1;
                garphics.FillRectangle(value[i] != ‘1’ ? Brushes.White : Brushes.Black,
                                       new Rectangle(lenEx, 0, drawWidth, _mHeight));
                lenEx += drawWidth;
            }
            garphics.Dispose();
            return codeImage;
        }
        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name=”pCodeImage”>图形</param>
        /// <param name=”pText”>文字</param>
        private void GetViewImage(Bitmap pCodeImage, string pText)
        {

            if (_mViewFont == null) return;
            Graphics graphics = Graphics.FromImage(pCodeImage);
            SizeF fontSize = graphics.MeasureString(pText, _mViewFont);
            if (fontSize.Width > pCodeImage.Width || fontSize.Height > pCodeImage.Height – 20)
            {

                graphics.Dispose();
                return;
            }
            int starHeight = pCodeImage.Height – (int)fontSize.Height;
            graphics.FillRectangle(Brushes.White, new Rectangle(0, starHeight, pCodeImage.Width, (int)fontSize.Height));
            int starWidth = (pCodeImage.Width – (int)fontSize.Width) / 2;
            graphics.DrawString(pText, _mViewFont, Brushes.Black, starWidth, starHeight);
            graphics.Dispose();
        }
    }
}

 

 

using System;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using System.Drawing.Printing;

private void form1_Load(object sender, EventArgs e)
{

    A5 830*580(in英寸), A4 1170*830(in英寸)
    830,580是更改的重点,”A5″可改,可不改
    //PaperSize ps = new PaperSize(“A5 (210 x 148 mm)”, 827, 574);
    PaperSize ps = new PaperSize(“A4 (210 x 148 mm)”, 830, 1170);
    //横向打印,不可忽略,在Canon打印机上会出现问题,上面的830,1170不可任意更改
    _pdc.DefaultPageSettings.Landscape = true;
    _pdc.DefaultPageSettings.PaperSize = ps;
    printPreviewControl1.Document = _pdc;
    printPreviewControl1.Zoom = 1.3;

    int a = Dspnt.Tables[0].Rows.Count;
    if (a%25 == 0)
        C = a/25;
    else
        C = a/25 + 1;
    //显示共有多少页
    _pdc.DocumentName = C.ToString(CultureInfo.InvariantCulture);
    B = 0;
    //使用自定义
    _pdc.PrintPage += Zdy;
}

//如何使用Code39(Barcode)
private void button1_Click(object sender, EventArgs e)
{

    Code39 c39 = new Code39();
    c39.Height = 120;
    c39.Magnify = 1;
    c39.ViewFont = new Font(“宋体”, 20);
    pictureBox1.Image = c39.GetCodeImage(this.textBox1.Text.Trim().ToUpper(), Code39.Code39Model.Code39Normal, true);
    pictureBox1.Image.Save(@”C:/1.JPG”);
}
//如果自定义绘制图形
private static Bitmap _barcode;
private static void Zdy(object sender, PrintPageEventArgs e)
{

    var c39 = new Code39();
    c39.Height = 70;
    c39.Magnify = 1;
    c39.ViewFont = new Font(“宋体”, 20);
    string txt = dt.Rows[0][“po”].ToString().ToUpper().Trim();
    //此处用到Code39类
    _barcode = c39.GetCodeImage(dt, Code39.Code39Model.Code39Normal, true);
}

今天的文章C#绘制条码CODE39和CODE39全码模式分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/63632.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注