C# 条形码 生成函数 (Code 128 标准
C# 条形码 生成函数 (Code 128 标准参考:GB/T 18347-2001)
| 
 最近在做单据打印,发现客户要求用到条形码,在网上找了,发现只有一些条形码的标准,但打出来发现根本不能扫,还要加某些字体. 以下代码经过实际扫描测试~! 
    /// <summary> 
    /// 生成条形码( 128条码,标准参考:GB/T 18347-2001 ) 
    /// BY JUNSON 20090508 
    /// </summary> 
    public class BarCode 
    { 
        /// <summary> 
        /// 条形码生成函数 
        /// </summary> 
        /// <param name="text">条型码字串</param> 
        /// <returns></returns> 
        public static Bitmap BuildBarCode(string text) 
        { 
            //查检是否合条件TEXT 
            bool ck = CheckErrerCode(text); 
            if (!ck) 
                throw new Exception("条形码字符不合要求,不能是汉字或全角字符"); 
            string barstring = BuildBarString(text); 
            return KiCode128C(barstring, 30); 
        } 
        /// <summary> 
        /// 建立条码字符串 
        /// </summary> 
        /// <param name="tex">条码内容</param> 
        /// <returns></returns> 
        private static string BuildBarString(string tex) 
        { 
            string barstart = "bbsbssbssss";    //码头 
            string barbody = "";                //码身 
            string barcheck = "";               //码检 
            string barend = "bbsssbbbsbsbb";    //码尾 
            int checkNum = 104; 
            //循环添加码身,计算码检 
            for (int i = 1; i <= tex.Length; i++) 
            { 
                int index = (int)tex[i - 1] - 32; 
                checkNum += (index * i); 
                barbody += AddSimpleTag(index);//加入字符值的条码标记 
            } 
            //码检值计算 
            barcheck = AddSimpleTag(int.Parse(Convert.ToDouble(checkNum % 103).ToString("0"))); 
            string barstring = barstart + barbody + barcheck + barend; 
            return barstring; 
        } 
        //增加一个条码标记 
        private static string AddSimpleTag(int CodeIndex) 
        { 
            string res = ""; 
            /// <summary>1-4的条的字符标识 </summary> 
            string[] TagB ={ "", "b", "bb", "bbb", "bbbb" }; 
            /// <summary>1-4的空的字符标识 </summary> 
            string[] TagS ={ "", "s", "ss", "sss", "ssss" }; 
            string[] Code128List = new string[] { 
                "212222","222122","222221","121223","121322","131222","122213","122312","132212","221213", 
                "221312","231212","112232","122132","122231","113222","123122","123221","223211","221132", 
                "221231","213212","223112","312131","311222","321122","321221","312212","322112","322211", 
                "212123","212321","232121","111323","131123","131321","112313","132113","132311","211313", 
                "231113","231311","112133","112331","132131","113123","113321","133121","313121","211331", 
                "231131","213113","213311","213131","311123","311321","331121","312113","312311","332111", 
                "314111","221411","431111","111224","111422","121124","121421","141122","141221","112214", 
                "112412","122114","122411","142112","142211","241211","221114","413111","241112","134111", 
                "111242","121142","121241","114212","124112","124211","411212","421112","421211","212141", 
                "214121","412121","111143","111341","131141","114113","114311","411113","411311","113141", 
                "114131","311141","411131","211412","211214","211232" }; 
            string tag = Code128List[CodeIndex]; 
            for (int i = 0; i < tag.Length; i++) 
            { 
                string temp = ""; 
                int num = int.Parse(tag[i].ToString()); 
                if (i % 2 == 0) 
                { 
                    temp = TagB[num]; 
                } 
                else 
                { 
                    temp = TagS[num]; 
                } 
                res += temp; 
            } 
            return res; 
        } 
        /// <summary> 
        /// 检查条形码文字是否合条件(不能是汉字或全角字符) 
        /// </summary> 
        /// <param name="cktext"></param> 
        /// <returns></returns> 
        private static bool CheckErrerCode(string cktext) 
        { 
            foreach (char c in cktext) 
            { 
                byte[] tmp = System.Text.UnicodeEncoding.Default.GetBytes(c.ToString()); 
                if (tmp.Length > 1) 
                    return false; 
            } 
            return true; 
        } 
        /// <summary>生成条码 </summary> 
        /// <param name="BarString">条码模式字符串</param> //Format32bppArgb 
        /// <param name="Height">生成的条码高度</param> 
        /// <returns>条码图形</returns> 
        private static Bitmap KiCode128C(string BarString, int _Height) 
        { 
            Bitmap b = new Bitmap(BarString.Length, _Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
            //using (Graphics grp = Graphics.FromImage(b)) 
            //{ 
            try 
            { 
                char[] cs = BarString.ToCharArray(); 
                for (int i = 0; i < cs.Length; i++) 
                { 
                    for (int j = 0; j < _Height; j++) 
                    { 
                        if (cs[i] == 'b') 
                        { 
                            b.SetPixel(i, j, Color.Black); 
                        } 
                        else 
                        { 
                            b.SetPixel(i, j, Color.White); 
                        } 
                    } 
               } 
                //grp.DrawString(text, SystemFonts.CaptionFont, Brushes.Black, new PointF(leftEmpty, b.Height - botEmpty)); 
                return b; 
            } 
            catch 
            { 
                return null; 
            } 
            //} 
        } 
    }  
 | 
C# 条形码 生成函数 (Code 128 标准的更多相关文章
- Code 128 规则解析
		
1.CODE 128 标准 1.1 code 128码格式: 格式: 从左起: 空白区域,起始字符(Start),数据区域(data),校验码(check),结束字符(Stop),空白区域. ...
 - 常见条码类型介绍(Code 39、Code 128、EAN-8、EAN-13、EAN-128、ISSN、TIF、TIF-14、UPC(A)、UPC(E))
		
常见条码类型,如下: 1.Code 39 Code 39,又称为"Code 3 of 9",是非零售市场中最常用的格式,用于盘存和跟踪.Code 39码编码规则简单,误码率低.所能 ...
 - 一维码Code 128简介及其解码实现(zxing-cpp)
		
一维码Code 128:1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,相对较为复杂,支持的字元也相对较多,又有不同的编码方式可供交互运用,因此其应用弹性也较大. Cod ...
 - (zxing.net)一维码Code 128的简介、实现与解码
		
一.简介 一维码Code 128:1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,相对较为复杂,支持的字元也相对较多,又有不同的编码方式可供交互运用,因此其应用弹性也较大 ...
 - GitHub上传不了的解决 ssh: connect to host github.com port 22: Bad file number git did not exit cleanly (exit code 128)
		
问题情况 本来一直用的是github的客户端,结果现在上传的时候出问题了,去网站上看,新项目已经创建,但是代码却怎么都上传不上去.于是只好用命令行的方式解决. Tortoisegit上是这样说的: g ...
 - [Jenkins][git]构建时提示Caused by: hudson.plugins.git.GitException: Command "/usr/bin/git reset --hard" returned status code 128:
		
--------------------- 如需转载,转载请注明出处. --------------------- 今日发现所有IOS构建相关的job全部失败,并提示如下错误: ERROR: Erro ...
 - 解决git did not exit cleanly (exit code 128)
		
最近在用git提交代码到部门服务器上的时候,总是有 提示 git did not exit cleanly (exit code 128).网上有2种解决方式: 1.替换路径 1.鼠标右键 -> ...
 - ERROR: gnu-config-native-20150728+gitAUTOINC+b576fa87c1-r0 do_unpack: Function failed: Fetcher failure: Fetch command failed with exit code 128, output: fatal: the '--set-upstream' option is no longer
		
/********************************************************************** * ERROR: gnu-config-native-2 ...
 - git did not exit cleanly (exit code 128)
		
github,pull和push的时候出问题,提示git did not exit cleanly (exit code 128) 使用HTTP格式的url,不要使用SSH格式的url,在官网上赋值下 ...
 
随机推荐
- SGU 101
			
SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...
 - POJ2533——Longest Ordered Subsequence(简单的DP)
			
Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...
 - SQL盲注修订建议
			
一般有多种减轻威胁的技巧: [1] 策略:库或框架 使用不允许此弱点出现的经过审核的库或框架,或提供更容易避免此弱点的构造. [2] 策略:参数化 如果可用,使用自动实施数据和代码之间的分离的结构化机 ...
 - Java API ——StringBuffer类
			
1.StringBuffer类概述 1)我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间.而StringBuffer就可以解决这个问题 2)线程安全的可变字 ...
 - Flex Array内置排序方法的使用
			
在Array类中,提供内置的排序方法.排序是在软件开发的过程中,经常遇到的问题.通过这些内置的方法,可以快速轻便的进行排序操作. Array类提供sort方法对Array实例进行排序.sort方法没有 ...
 - Android 内核初识(1)下载源码需求与教程
			
官方文档: http://source.android.com/source/requirements.html Requirements The Android build is routinel ...
 - Android开发之点击两次Back键退出App
			
Back按键的方法是onKeyDown()方法,重写该方法就可以改变back按键的作用. 实现点击两次Back按键退出app,有两种方法: 方法1. private static boolean is ...
 - 1003. Parity(并查集)
			
1003 看篇国家论文 <从<parity>的解法谈程序优化> 对于区间i,j 如果用sum[i],sum[j]来表示到i的1的个数的奇偶性 那么仔细想下 sum[i-1] 若 ...
 - bzoj2208:[Jsoi2010]连通数
			
http://blog.csdn.net/u013598409/article/details/47037499 里面似乎有生成数据的... //我本来的想法是tarjan缩点之后然后将图遍历一遍就可 ...
 - LRU与MRU算法
			
1.Cache Hit and Cache Miss 当使用者第一次向数据库发出查询数据的请求的时候,数据库会先在缓冲区中查找该数据,如果要访问的数据恰好已经在缓冲区中(我们称之为Cache Hit) ...