您的位置:首页 > 编程语言 > C#

C#实现批量生成条形码 ——主要用于准考证号的条形码生成

2014-11-18 11:26 537 查看
功能介绍
1、  批量生成条形码
2、  自由设置图片大小、条形码线高和间距
3、  自由设置标题文本字号大小
4、  自由设置增量(相邻条形码的差量)数制(比如:每考场只生成30名)

源代码
using System;
using System;
using System.Collections.Generic;
usingSystem.ComponentModel;
using System.Data;
usingSystem.Drawing;
using System.Linq;
using System.Text;
usingSystem.Windows.Forms;
usingSystem.Drawing.Imaging;
usingSystem.Collections;
usingSystem.Configuration;
using System.Text.RegularExpressions;
 
namespaceTXMApplication
{
    public partial class Form1 : Form
    {
        //条形码
        public class Code39
        {
            private Hashtable Decode;
            private Hashtable CheckCode;
            private String SPARATOR ="0";
            public int WidthCU=3 ;//粗线和宽间隙宽度
            public int WidthXI=1 ;//细线和窄间隙宽度
            public int AboveMargin;//上边距
            public int DownMargin;//下边距
            public int LeftMargin;//左边距
            public int RightMargin;//右边距
            public int FoundSize;//标题字号
            public int UseTitle;//是否有标题
            public int LineHeight;//线高
            public String PathStr;//存储路径
            private int Height = 0;//
            private int Width = 0;//
            public void Set(int a,int d,int l,int r,int s,int t,int h,String p)
            {
                AboveMargin=a;
                DownMargin=d;
                LeftMargin = l;
                RightMargin = r;
                FoundSize = s;
                UseTitle = t;
                LineHeight = h;
                PathStr = p;
               //Width=LeftMargin+RightMargin+WidthCU+WidthXI
            }
            public Code39()
            {
                Decode = new Hashtable();
                Decode.Add("0","000110100");
                Decode.Add("1","100100001");
                Decode.Add("2","001100001");
                Decode.Add("3","101100000");
                Decode.Add("4","000110001");
                Decode.Add("5","100110000");
                Decode.Add("6","001110000");
                Decode.Add("7","000100101");
                Decode.Add("8","100100100");
                Decode.Add("9","001100100");
                Decode.Add("*","010010100");
                CheckCode = new Hashtable();
                CheckCode.Add("0","0");
                CheckCode.Add("1","1");
                CheckCode.Add("2","2");
                CheckCode.Add("3","3");
                CheckCode.Add("4","4");
                CheckCode.Add("5","5");
                CheckCode.Add("6","6");
                CheckCode.Add("7","7");
                CheckCode.Add("8","8");
                CheckCode.Add("9","9");
                CheckCode.Add("*","39");
            }
            //保存文件
            public Boolean saveFile(StringCode)
            {
                String code39 = Encode39(Code);
                if (code39 != null)
                {
                    Bitmap saved = newBitmap(Width, Height);
                    Graphics g =Graphics.FromImage(saved);
                    g.FillRectangle(newSolidBrush(Color.White), 0, 0, Width, Height);
                    this.DrawBarCode39(code39,Code , g, UseTitle);
                    String filename = PathStr +"/" + Code + ".jpg";
                    saved.Save(filename,ImageFormat.Jpeg);
                    saved.Dispose();
                    return true;
                }
                return false;
            }
            private String Encode39(StringCode)
            {
                int UseStand = 1;
                String originalCode = Code;
                if (null == Code ||Code.Trim().Equals(""))
                {
                    return null;
                }
                Code = Code.ToUpper();
                Regex rule = newRegex("@[^0-9A-Z*]");
                if (rule.IsMatch(Code))
                {
                    MessageBox.Show("非法字符");
                    return null;
                }
                int Check = 0;
                for (int i = 0; i < Code.Length; i++)
                {
                    Check +=int.Parse((String)CheckCode[Code.Substring(i, 1)]);
                }
                Check = Check % 43;
                foreach (DictionaryEntry de inCheckCode)
                {
                    if ((String
4000
)de.Value ==Code.ToString())
                    {
                        Code = Code +(String)de.Key;
                        break;
                    }
                }
                if (Code.Substring(0, 1) !="*")
                    Code = "*" +Code;
                if (Code.Substring(Code.Length- 1, 1) != "*")
                    Code = Code +"*";
                String Code39 = "";
                for (int i = 0; i <Code.Length; i++)
                {
                    Code39 = Code39 +(String)Decode[Code.Substring(i, 1)] + SPARATOR;
                }
                for (int i = 0; i <Code39.Length; i++)
                {
                    if("0".Equals(Code39.Substring(i, 1)))
                        Width += WidthXI;
                    else
                        Width += WidthCU;
                }
                Width = Width +LeftMargin+RightMargin;
                Height =Height+AboveMargin+DownMargin+LineHeight+20;
                return Code39;
            }
            private void DrawBarCode39(StringCode39, String Title, Graphics g, int iftext)
            {
                Pen pWhite = newPen(Color.White, 1);
                Pen pBlack = newPen(Color.Black, 1);
                int position = this.LeftMargin;
                if (UseTitle ==1)//////////////////////////
                {
                    Font TitleFont = newFont("宋?体¬?", this.FoundSize, FontStyle.Regular);
                    SizeF sf = g.MeasureString(Title,TitleFont);
                    g.DrawString(Title,TitleFont, Brushes.Black, (Width - sf.Width) / 2, AboveMargin + LineHeight+5);
                }//////////////////////////////
                for (int i = 0; i <Code39.Length; i++)
                {
                    if("0".Equals(Code39.Substring(i, 1)))
                    {
                        for (int j = 0; j <WidthXI; j++)
                            g.DrawLine(pBlack,position + j, AboveMargin, position + j, AboveMargin + LineHeight);
                        position += WidthXI;
                    }
                    else
                    {
                        for (int j = 0; j <WidthCU; j++)
                            g.DrawLine(pBlack,position + j, AboveMargin, position + j, AboveMargin + LineHeight);
                        position += WidthCU;
                    }
                    i++;
                    if("0".Equals(Code39.Substring(i, 1)))
                        position += WidthXI;
                    else
                        position += WidthCU;
                }
                return;
            }
        }
        //
        public Boolean verify()
        {
            if (PathTBox.Text == "")
            {
                MessageBox.Show("请?填¬?写¡ä存ä?储ä¡é路¡¤径?!ê?", "ERROR");
                return false;
            }
            Regex rule1 = newRegex("@[^0-9]");
            if (MessageBox.Show("将要生成以下信息的条码!\n" + "起始信息:" + BeginTBox.Text + "\n" + "信息增量:" + StepTBox.Text + "\n" + "终止信?息:êo" + EndTBox.Text + "\n" + "存储路径:" + PathTBox.Text + "\n", "确认信息", MessageBoxButtons.OKCancel) == DialogResult.OK)
                return true;
            else
                return false;
        }
 
        public Form1()
        {
            InitializeComponent();
            singleradio.Checked = false;
            multiradio.Checked = true;
            istitleCheck.Checked = true;
        }
 
        private void Form1_Load(object sender,EventArgs e)
        {
 
        }
 
        private void OKButton_Click(objectsender, System.EventArgs e)
        {
            //Code39 c=new Code39();
            int istitle=0;
            int i ,j,step,n;
            int len;
            String beginstr, endstr,nowstr;
            long beginnum,endnum,nownum;
            if (istitleCheck.Checked==true)
                istitle=1;
            if (verify())
            {
                if (singleradio.Checked ==true)
                {
                    Code39 c = new Code39();
                    c.Set(Convert.ToInt32(abovemargin.Value),Convert.ToInt32(downmargin.Value), Convert.ToInt32(leftmargin.Value),Convert.ToInt32(rightmargin.Value), Convert.ToInt32(fontsize.Value), istitle,Convert.ToInt32(lineheight.Value),
PathTBox.Text.ToString());
                   c.saveFile(BeginTBox.Text.ToString());
                }
                else
                {
                    beginstr =BeginTBox.Text.ToString();
                    endstr =EndTBox.Text.ToString();
                    len = beginstr.Length;
                    nowstr = beginstr;
                   beginnum=Convert.ToInt64(beginstr);
                   endnum=Convert.ToInt64(endstr);
                    nownum = beginnum;
                   step=Convert.ToInt32(StepTBox.Text);
                   n=Convert.ToInt32(CodeTBox.Text);
                    while (nownum <= endnum)
                    {
                        if (nownum % 100 >n)
                        {
                            Code39 c = newCode39();
                           c.Set(Convert.ToInt32(abovemargin.Value),Convert.ToInt32(downmargin.Value), Convert.ToInt32(leftmargin.Value),Convert.ToInt32(rightmargin.Value), Convert.ToInt32(fontsize.Value), istitle,Convert.ToInt32(lineheight.Value),
PathTBox.Text.ToString());        
                            nownum = nownum -nownum % 100 + 100 + 1;
                            nowstr =nownum.ToString("d" + len);
                            c.saveFile(nowstr);
                        }
                        else
                        {
                            Code39 c = newCode39();
                           c.Set(Convert.ToInt32(abovemargin.Value),Convert.ToInt32(downmargin.Value), Convert.ToInt32(leftmargin.Value),Convert.ToInt32(rightmargin.Value), Convert.ToInt32(fontsize.Value), istitle,Convert.ToInt32(lineheight.Value),
PathTBox.Text.ToString());
                            nowstr =nownum.ToString("d" + len);
                            c.saveFile(nowstr);
                        }
                        nownum = nownum + step;
                    }
                }
            }
        }
 
        private voidlinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FolderBrowserDialog sfdialog = newFolderBrowserDialog();
            sfdialog.ShowDialog();
            PathTBox.Text =sfdialog.SelectedPath.ToString();
        }
 
        private voidsingleradio_CheckedChanged(object sender, System.EventArgs e)
        {
            if(singleradio.Checked)
            {
                StepTBox.Enabled = false;
                EndTBox.Enabled = false;
                CodeTBox.Enabled = false;
            }
        }
 
        private voidmultiradio_CheckedChanged(object sender, System.EventArgs e)
        {
            if (multiradio.Checked)
            {
                StepTBox.Enabled = true;
                EndTBox.Enabled = true;
                CodeTBox.Enabled = true;
            }
        }
 
    }
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: