C # How to generate code128 barcodes

Source: Internet
Author: User

This article describes the Logistics barcode C # Implementation method, share for your reference. The implementation method is as follows:

The main function code is as follows:

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>///high///</summary> public uint Height {get {return m_height;} set {M_heigh t = value;      }} private Font m_valuefont = null; <summary>//Whether the visible number is displayed if NULL does not display the number///</summary> public Font Valuefont {get {return m_v Aluefont;      } set {M_valuefont = value;}}      Private byte m_magnify = 0; <summary>///magnification///</summary> public byte magnify {get {return m_magnify;} set {M_m Agnify = value;        }}///<summary>///Barcode category///</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 Data Sheet 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 ("page", "<", "<", "28", "322112");   M_CODE128.ROWS.ADD ("29", "=", "=", "29", "322211");     M_CODE128.ROWS.ADD ("A", ">", ">", "30", "212123");        M_CODE128.ROWS.ADD ("31", "?", "?", "31", "212321");        M_CODE128.ROWS.ADD ("32", "@", "@", "32", "232121");        M_CODE128.ROWS.ADD ("A", "a", "a", "33", "111323");        M_CODE128.ROWS.ADD ("The", "B", "B", "34", "131123");        M_CODE128.ROWS.ADD ("D", "C", "C", "35", "131321");        M_Code128.Rows.Add ("n", "D", "D", "36", "112313");        M_CODE128.ROWS.ADD ("Panax Notoginseng", "E", "E", "37", "132113");        M_CODE128.ROWS.ADD ("A", "F", "F", "38", "132311");        M_Code128.Rows.Add ("n", "G", "G", "39", "211313");        M_CODE128.ROWS.ADD ("Max", "H", "H", "40", "231113");        M_CODE128.ROWS.ADD ("A", "I", "I", "41", "231311");        M_Code128.Rows.Add ("K", "J", "J", "42", "112133");        M_Code128.Rows.Add ("n", "K", "K", "43", "112331");        M_Code128.Rows.Add ("M", "L", "L", "44", "132131");        M_CODE128.ROWS.ADD ("A", "M", "M", "45", "113123"); M_CODE128.ROWS.ADD ("+", "n", "N", "46", "113321 ");        M_Code128.Rows.Add ("n", "O", "O", "47", "133121");        M_CODE128.ROWS.ADD ("The", "P", "P", "48", "313121");        M_CODE128.ROWS.ADD ("The", "Q", "Q", "49", "211331");        M_Code128.Rows.Add ("n", "R", "R", "50", "231131");        M_CODE128.ROWS.ADD ("Wuyi", "s", "s", "51", "213113");        M_Code128.Rows.Add ("n", "T", "T", "52", "213311");        M_CODE128.ROWS.ADD ("A", "U", "U", "53", "213131");        M_CODE128.ROWS.ADD ("WU", "V", "V", "54", "311123");        M_Code128.Rows.Add ("n", "W", "W", "55", "311321");        M_Code128.Rows.Add ("n", "X", "X", "56", "331121");        M_CODE128.ROWS.ADD ("page", "Y", "Y", "57", "312113");        M_Code128.Rows.Add ("n", "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 ("Up", "NUL", "'", "64", "111422");        M_Code128.Rows.Add ("n", "SOH", "a", "65", "121124");        M_Code128.Rows.Add ("n", "STX", "B", "66", "121421");        M_CODE128.ROWS.ADD ("ETX", "C", "67", "141122");        M_CODE128.ROWS.ADD ("EOT", "D", "68", "141221");        M_CODE128.ROWS.ADD ("A", "ENQ", "E", "69", "112214");        M_Code128.Rows.Add ("n", "ACK", "F", "70", "112412");        M_Code128.Rows.Add ("F", "BEL", "G", "71", "122114");        M_CODE128.ROWS.ADD ("A", "BS", "H", "72", "122411");        M_CODE128.ROWS.ADD ("The", "HT", "I", "73", "142112");        M_Code128.Rows.Add ("K", "LF", "J", "74", "142211");        M_CODE128.ROWS.ADD ("page", "VT", "K", "75", "241211");        M_CODE128.ROWS.ADD ("The", "FF", "I", "76", "221114");        M_Code128.Rows.Add ("n", "CR", "M", "77", "413111");        M_CODE128.ROWS.ADD ("+", "so", "N", "78", "241112");        M_CODE128.ROWS.ADD ("The", "SI", "O", "79", "134111"); M_code128.rowS.add ("n", "DLE", "P", "80", "111242");        M_CODE128.ROWS.ADD ("Bayi", "DC1", "Q", "81", "121142");        M_CODE128.ROWS.ADD ("DC2", "R", "82", "121241");        M_CODE128.ROWS.ADD ("The", "DC3", "s", "83", "114212");        M_Code128.Rows.Add ("n", "DC4", "T", "84", "124112");        M_CODE128.ROWS.ADD ("The", "NAK", "U", "85", "124211");        M_Code128.Rows.Add ("n", "SYN", "V", "86", "411212");        M_CODE128.ROWS.ADD ("ETB", "W", "87", "421112");        M_Code128.Rows.Add ("n", "CAN", "X", "88", "421211");        M_CODE128.ROWS.ADD ("The", "EM", "Y", "89", "212141");        M_Code128.Rows.Add ("n", "SUB", "Z", "90", "214121");        M_CODE128.ROWS.ADD ("A", "ESC", "{", "91", "412121");        M_CODE128.ROWS.ADD ("The", "FS", "|", "92", "111143");        M_CODE128.ROWS.ADD ("" "," GS ","} "," 93 "," 111341 ");        M_CODE128.ROWS.ADD ("94", "RS", "~", "94", "131141");        M_CODE128.ROWS.ADD ("Up", "US", "DEL", "95", "114113"); M_CODE128.ROWS.ADD ("" "," FNC3 "," FNC3 ", "96", "114311");        M_CODE128.ROWS.ADD ("The", "FNC2", "FNC2", "97", "411113");        M_CODE128.ROWS.ADD ("98", "Shift", "shift", "98", "411311");        M_CODE128.ROWS.ADD ("CODEC", "CODEC", "99", "113141");        M_Code128.Rows.Add ("n", "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 ("STARTC", "STARTC", "STARTC", "211232");        M_CODE128.ROWS.ADD ("106", "Stop", "Stop", "Stop", "2331112"); #endregion}///<summary>//Get 128 Graphics///</summary>//<param name= "P_text" > Text </param>//<param name= "P_code" > Encoding </param>//<returns> graphics </returns> Publ IC Bitmap getcodeimage (String p_tExt, Encode p_code) {string _viewtext = P_text;        String _text = "";        ilist<int> _textnumb = new list<int> (); int _examine = 0;            First switch (p_code) {case ENCODE.CODE128C: _examine = 105; if (! (            (P_text.length & 1) = = 0) throw new Exception ("128C length must be even");              while (p_text.length! = 0) {int _temp = 0;              try {int _codenumb128 = Int32.Parse (p_text.substring (0, 2)); } catch {throw new Exception ("128C must be a number!              ");              } _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 length must be even"); _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 must be a number!              ");              } _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 ("Invalid character set!" + 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 ("Wrong code, no data"); _text = _text.insert (0, GetValue (_examine));        Gets the start bit for (int i = 0; I! = _textnumb.count; i++) {_examine + = _textnumb[i] * (i + 1);      } _examine = _examine% 103; _text + = GetValue (_examine) for obtaining a strict efficiency level; Get strict efficiency bit _text + = "2331112";        End bit Bitmap _codeimage = GetImage (_text);        Getviewtext (_codeimage, _viewtext);      return _codeimage; }///<summary>///For Target data///</summary>//<param name= "P_code" > Encoding &LT;/PARAM&G      T <param name= "P_value" > value A B 30</param>//<param name= "P_setid" > Return number </param>///        Lt;returns> encoding </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 ("Wrong Code" + p_value.tostring ()); P_setid = Int32.Parse (_row[0]["ID"].        ToString ()); return _row[0]["Bandcode"].      ToString (); }///<summary>///////</summary>//<param name= "P_codeid" ></param&gt      ; <returns></returns> private string GetValue (int p_codeid) {datarow[] _row = m_code128.se        Lect ("id=" + p_codeid.tostring () + "'");        if (_row.length! = 1) throw new Exception ("Coding error of the validation bit" + p_codeid.tostring ()); return _row[0]["Bandcode"].      ToString ();      }///<summary>//Get barcode Graphics///</summary>//<param name= "P_text" > Text </param> <returns> Graphics </returns> Private Bitmap GetImage (string p_text) {char[] _value = P_tex        T.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); Get the width and magnification factor 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>///Display visible barcode text if less than 40 does not display text///</summary>//<param name= "P_bitmap" > Graphics </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//result is Starc +12 +34 +56 +78 +47 +end Internal Image GetcodeimAge (String p) {throw new NotImplementedException (); }    }  }}

The calling code is:

Code128 _code = new Code128 () _code.valuefont = new Font ("Arial", 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);

I hope the method described in this article will be helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # How to generate code128 barcodes

This address: http://www.paobuke.com/develop/c-develop/pbk23613.html






Related content C # Implementation example log output function Silverlight implementation of the image local amplification Method C # traverse the complete implementation of the folder and its subdirectories C # Parsing JSON strings is always more double quotes cause analysis and solution
C # Implementation of the ListView group Shrink extension Method C # Fahrenheit temperature and Celsius temperature mutual conversion Method C # Method of exporting data to an Excel file C # Implements a class instance for manipulating a wav sound file

C # How to generate code128 barcodes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.