C#生成code128条形码的方法
程序员文章站
2023-12-17 17:12:16
本文实例讲述了物流条形码的c#实现方法,分享一下供大家参考。具体实现方法如下:
主要功能代码如下:
using system;
using system.co...
本文实例讲述了物流条形码的c#实现方法,分享一下供大家参考。具体实现方法如下:
主要功能代码如下:
using system; using system.collections.generic; using system.data; using system.drawing; namespace code { class barcode { public class code128 { private datatable m_code128 = new datatable(); private uint m_height = 40; /// <summary> /// 高度 /// </summary> public uint height { get { return m_height; } set { m_height = value; } } private font m_valuefont = null; /// <summary> /// 是否显示可见号码 如果为null不显示号码 /// </summary> public font valuefont { get { return m_valuefont; } set { m_valuefont = value; } } private byte m_magnify = 0; /// <summary> /// 放大倍数 /// </summary> public byte magnify { get { return m_magnify; } set { m_magnify = value; } } /// <summary> /// 条码类别 /// </summary> public enum encode { code128a, code128b, code128c, ean128 } public code128() { m_code128.columns.add("id"); m_code128.columns.add("code128a"); m_code128.columns.add("code128b"); m_code128.columns.add("code128c"); m_code128.columns.add("bandcode"); m_code128.casesensitive = true; #region 数据表 m_code128.rows.add("0", " ", " ", "00", "212222"); m_code128.rows.add("1", "!", "!", "01", "222122"); m_code128.rows.add("2", "\"", "\"", "02", "222221"); m_code128.rows.add("3", "#", "#", "03", "121223"); m_code128.rows.add("4", "$", "$", "04", "121322"); m_code128.rows.add("5", "%", "%", "05", "131222"); m_code128.rows.add("6", "&", "&", "06", "122213"); m_code128.rows.add("7", "'", "'", "07", "122312"); m_code128.rows.add("8", "(", "(", "08", "132212"); m_code128.rows.add("9", ")", ")", "09", "221213"); m_code128.rows.add("10", "*", "*", "10", "221312"); m_code128.rows.add("11", "+", "+", "11", "231212"); m_code128.rows.add("12", ",", ",", "12", "112232"); m_code128.rows.add("13", "-", "-", "13", "122132"); m_code128.rows.add("14", ".", ".", "14", "122231"); m_code128.rows.add("15", "/", "/", "15", "113222"); m_code128.rows.add("16", "0", "0", "16", "123122"); m_code128.rows.add("17", "1", "1", "17", "123221"); m_code128.rows.add("18", "2", "2", "18", "223211"); m_code128.rows.add("19", "3", "3", "19", "221132"); m_code128.rows.add("20", "4", "4", "20", "221231"); m_code128.rows.add("21", "5", "5", "21", "213212"); m_code128.rows.add("22", "6", "6", "22", "223112"); m_code128.rows.add("23", "7", "7", "23", "312131"); m_code128.rows.add("24", "8", "8", "24", "311222"); m_code128.rows.add("25", "9", "9", "25", "321122"); m_code128.rows.add("26", ":", ":", "26", "321221"); m_code128.rows.add("27", ";", ";", "27", "312212"); m_code128.rows.add("28", "<", "<", "28", "322112"); m_code128.rows.add("29", "=", "=", "29", "322211"); m_code128.rows.add("30", ">", ">", "30", "212123"); m_code128.rows.add("31", "?", "?", "31", "212321"); m_code128.rows.add("32", "@", "@", "32", "232121"); m_code128.rows.add("33", "a", "a", "33", "111323"); m_code128.rows.add("34", "b", "b", "34", "131123"); m_code128.rows.add("35", "c", "c", "35", "131321"); m_code128.rows.add("36", "d", "d", "36", "112313"); m_code128.rows.add("37", "e", "e", "37", "132113"); m_code128.rows.add("38", "f", "f", "38", "132311"); m_code128.rows.add("39", "g", "g", "39", "211313"); m_code128.rows.add("40", "h", "h", "40", "231113"); m_code128.rows.add("41", "i", "i", "41", "231311"); m_code128.rows.add("42", "j", "j", "42", "112133"); m_code128.rows.add("43", "k", "k", "43", "112331"); m_code128.rows.add("44", "l", "l", "44", "132131"); m_code128.rows.add("45", "m", "m", "45", "113123"); m_code128.rows.add("46", "n", "n", "46", "113321"); m_code128.rows.add("47", "o", "o", "47", "133121"); m_code128.rows.add("48", "p", "p", "48", "313121"); m_code128.rows.add("49", "q", "q", "49", "211331"); m_code128.rows.add("50", "r", "r", "50", "231131"); m_code128.rows.add("51", "s", "s", "51", "213113"); m_code128.rows.add("52", "t", "t", "52", "213311"); m_code128.rows.add("53", "u", "u", "53", "213131"); m_code128.rows.add("54", "v", "v", "54", "311123"); m_code128.rows.add("55", "w", "w", "55", "311321"); m_code128.rows.add("56", "x", "x", "56", "331121"); m_code128.rows.add("57", "y", "y", "57", "312113"); m_code128.rows.add("58", "z", "z", "58", "312311"); m_code128.rows.add("59", "[", "[", "59", "332111"); m_code128.rows.add("60", "\\", "\\", "60", "314111"); m_code128.rows.add("61", "]", "]", "61", "221411"); m_code128.rows.add("62", "^", "^", "62", "431111"); m_code128.rows.add("63", "_", "_", "63", "111224"); m_code128.rows.add("64", "nul", "`", "64", "111422"); m_code128.rows.add("65", "soh", "a", "65", "121124"); m_code128.rows.add("66", "stx", "b", "66", "121421"); m_code128.rows.add("67", "etx", "c", "67", "141122"); m_code128.rows.add("68", "eot", "d", "68", "141221"); m_code128.rows.add("69", "enq", "e", "69", "112214"); m_code128.rows.add("70", "ack", "f", "70", "112412"); m_code128.rows.add("71", "bel", "g", "71", "122114"); m_code128.rows.add("72", "bs", "h", "72", "122411"); m_code128.rows.add("73", "ht", "i", "73", "142112"); m_code128.rows.add("74", "lf", "j", "74", "142211"); m_code128.rows.add("75", "vt", "k", "75", "241211"); m_code128.rows.add("76", "ff", "i", "76", "221114"); m_code128.rows.add("77", "cr", "m", "77", "413111"); m_code128.rows.add("78", "so", "n", "78", "241112"); m_code128.rows.add("79", "si", "o", "79", "134111"); m_code128.rows.add("80", "dle", "p", "80", "111242"); m_code128.rows.add("81", "dc1", "q", "81", "121142"); m_code128.rows.add("82", "dc2", "r", "82", "121241"); m_code128.rows.add("83", "dc3", "s", "83", "114212"); m_code128.rows.add("84", "dc4", "t", "84", "124112"); m_code128.rows.add("85", "nak", "u", "85", "124211"); m_code128.rows.add("86", "syn", "v", "86", "411212"); m_code128.rows.add("87", "etb", "w", "87", "421112"); m_code128.rows.add("88", "can", "x", "88", "421211"); m_code128.rows.add("89", "em", "y", "89", "212141"); m_code128.rows.add("90", "sub", "z", "90", "214121"); m_code128.rows.add("91", "esc", "{", "91", "412121"); m_code128.rows.add("92", "fs", "|", "92", "111143"); m_code128.rows.add("93", "gs", "}", "93", "111341"); m_code128.rows.add("94", "rs", "~", "94", "131141"); m_code128.rows.add("95", "us", "del", "95", "114113"); m_code128.rows.add("96", "fnc3", "fnc3", "96", "114311"); m_code128.rows.add("97", "fnc2", "fnc2", "97", "411113"); m_code128.rows.add("98", "shift", "shift", "98", "411311"); m_code128.rows.add("99", "codec", "codec", "99", "113141"); m_code128.rows.add("100", "codeb", "fnc4", "codeb", "114131"); m_code128.rows.add("101", "fnc4", "codea", "codea", "311141"); m_code128.rows.add("102", "fnc1", "fnc1", "fnc1", "411131"); m_code128.rows.add("103", "starta", "starta", "starta", "211412"); m_code128.rows.add("104", "startb", "startb", "startb", "211214"); m_code128.rows.add("105", "startc", "startc", "startc", "211232"); m_code128.rows.add("106", "stop", "stop", "stop", "2331112"); #endregion } /// <summary> /// 获取128图形 /// </summary> /// <param name="p_text">文字</param> /// <param name="p_code">编码</param> /// <returns>图形</returns> public bitmap getcodeimage(string p_text, encode p_code) { string _viewtext = p_text; string _text = ""; ilist<int> _textnumb = new list<int>(); int _examine = 0; //首位 switch (p_code) { case encode.code128c: _examine = 105; if (!((p_text.length & 1) == 0)) throw new exception("128c长度必须是偶数"); while (p_text.length != 0) { int _temp = 0; try { int _codenumb128 = int32.parse(p_text.substring(0, 2)); } catch { throw new exception("128c必须是数字!"); } _text += getvalue(p_code, p_text.substring(0, 2), ref _temp); _textnumb.add(_temp); p_text = p_text.remove(0, 2); } break; case encode.ean128: _examine = 105; if (!((p_text.length & 1) == 0)) throw new exception("ean128长度必须是偶数"); _textnumb.add(102); _text += "411131"; while (p_text.length != 0) { int _temp = 0; try { int _codenumb128 = int32.parse(p_text.substring(0, 2)); } catch { throw new exception("128c必须是数字!"); } _text += getvalue(encode.code128c, p_text.substring(0, 2), ref _temp); _textnumb.add(_temp); p_text = p_text.remove(0, 2); } break; default: if (p_code == encode.code128a) { _examine = 103; } else { _examine = 104; } while (p_text.length != 0) { int _temp = 0; string _valuecode = getvalue(p_code, p_text.substring(0, 1), ref _temp); if (_valuecode.length == 0) throw new exception("无效的字符集!" + p_text.substring(0, 1).tostring()); _text += _valuecode; _textnumb.add(_temp); p_text = p_text.remove(0, 1); } break; } if (_textnumb.count == 0) throw new exception("错误的编码,无数据"); _text = _text.insert(0, getvalue(_examine)); //获取开始位 for (int i = 0; i != _textnumb.count; i++) { _examine += _textnumb[i] * (i + 1); } _examine = _examine % 103; //获得严效位 _text += getvalue(_examine); //获取严效位 _text += "2331112"; //结束位 bitmap _codeimage = getimage(_text); getviewtext(_codeimage, _viewtext); return _codeimage; } /// <summary> /// 获取目标对应的数据 /// </summary> /// <param name="p_code">编码</param> /// <param name="p_value">数值 a b 30</param> /// <param name="p_setid">返回编号</param> /// <returns>编码</returns> private string getvalue(encode p_code, string p_value, ref int p_setid) { if (m_code128 == null) return ""; datarow[] _row = m_code128.select(p_code.tostring() + "='" + p_value + "'"); if (_row.length != 1) throw new exception("错误的编码" + p_value.tostring()); p_setid = int32.parse(_row[0]["id"].tostring()); return _row[0]["bandcode"].tostring(); } /// <summary> /// 根据编号获得条纹 /// </summary> /// <param name="p_codeid"></param> /// <returns></returns> private string getvalue(int p_codeid) { datarow[] _row = m_code128.select("id='" + p_codeid.tostring() + "'"); if (_row.length != 1) throw new exception("验效位的编码错误" + p_codeid.tostring()); return _row[0]["bandcode"].tostring(); } /// <summary> /// 获得条码图形 /// </summary> /// <param name="p_text">文字</param> /// <returns>图形</returns> private bitmap getimage(string p_text) { char[] _value = p_text.tochararray(); int _width = 0; for (int i = 0; i != _value.length; i++) { _width += int32.parse(_value[i].tostring()) * (m_magnify + 1); } bitmap _codeimage = new bitmap(_width, (int)m_height); graphics _garphics = graphics.fromimage(_codeimage); //pen _pen; int _lenex = 0; for (int i = 0; i != _value.length; i++) { int _valuenumb = int32.parse(_value[i].tostring()) * (m_magnify + 1); //获取宽和放大系数 if (!((i & 1) == 0)) { //_pen = new pen(brushes.white, _valuenumb); _garphics.fillrectangle(brushes.white, new rectangle(_lenex, 0, _valuenumb, (int)m_height)); } else { //_pen = new pen(brushes.black, _valuenumb); _garphics.fillrectangle(brushes.black, new rectangle(_lenex, 0, _valuenumb, (int)m_height)); } //_garphics.(_pen, new point(_lenex, 0), new point(_lenex, m_height)); _lenex += _valuenumb; } _garphics.dispose(); return _codeimage; } /// <summary> /// 显示可见条码文字 如果小于40 不显示文字 /// </summary> /// <param name="p_bitmap">图形</param> private void getviewtext(bitmap p_bitmap, string p_viewtext) { if (m_valuefont == null) return; graphics _graphics = graphics.fromimage(p_bitmap); sizef _drawsize = _graphics.measurestring(p_viewtext, m_valuefont); if (_drawsize.height > p_bitmap.height - 10 || _drawsize.width > p_bitmap.width) { _graphics.dispose(); return; } int _stary = p_bitmap.height - (int)_drawsize.height; _graphics.fillrectangle(brushes.white, new rectangle(0, _stary, p_bitmap.width, (int)_drawsize.height)); _graphics.drawstring(p_viewtext, m_valuefont, brushes.black, 0, _stary); } //12345678 //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47 //结果为starc +12 +34 +56 +78 +47 +end internal image getcodeimage(string p) { throw new notimplementedexception(); } } } }
调用代码为:
code128 _code = new code128(); _code.valuefont = new font("宋体", 20); system.drawing.bitmap imgtemp = _code.getcodeimage("t26200-1900-123-1-0900",code128.encode.code128a); imgtemp.save(system.appdomain.currentdomain.basedirectory + "\\" + "barcode.gif", system.drawing.imaging.imageformat.gif);
希望本文所述方法对大家的c#程序设计有所帮助。