SoFunction
Updated on 2025-03-08

Detailed explanation of the implementation method of BarCode barcode based on C# GDI+


public Bitmap GetCode39(string sourceCode)
         {
             int leftMargin = 5;
             int topMargin = 0;
             int thickLength = 2;
             int narrowLength = 1;
             int barCodeHeight = 35;
             int intSourceLength = ;
string strEncode = "010010100"; //Add the start code "*".
             var font = new ("Segoe UI", 5);

             string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";

             string[] Code39 =
             {
                 /* 0 */ "000110100", 
                 /* 1 */ "100100001", 
                 /* 2 */ "001100001", 
                 /* 3 */ "101100000",
                 /* 4 */ "000110001", 
                 /* 5 */ "100110000", 
                 /* 6 */ "001110000", 
                 /* 7 */ "000100101",
                 /* 8 */ "100100100", 
                 /* 9 */ "001100100", 
                 /* A */ "100001001", 
                 /* B */ "001001001",
                 /* C */ "101001000", 
                 /* D */ "000011001", 
                 /* E */ "100011000", 
                 /* F */ "001011000",
                 /* G */ "000001101", 
                 /* H */ "100001100", 
                 /* I */ "001001100", 
                 /* J */ "000011100",
                 /* K */ "100000011", 
                 /* L */ "001000011", 
                 /* M */ "101000010", 
                 /* N */ "000010011",
                 /* O */ "100010010", 
                 /* P */ "001010010", 
                 /* Q */ "000000111", 
                 /* R */ "100000110",
                 /* S */ "001000110", 
                 /* T */ "000010110", 
                 /* U */ "110000001", 
                 /* V */ "011000001",
                 /* W */ "111000000", 
                 /* X */ "010010001", 
                 /* Y */ "110010000", 
                 /* Z */ "011010000",
                 /* - */ "010000101", 
                 /* . */ "110000100", 
                 /*' '*/ "011000100",
                 /* $ */ "010101000",
                 /* / */ "010100010", 
                 /* + */ "010001010", 
                 /* % */ "000101010", 
                 /* * */ "010010100" 
             };
             sourceCode = ();

             Bitmap objBitmap = new Bitmap(
               ((thickLength * 3 + narrowLength * 7) * (intSourceLength + 2)) + (leftMargin * 2),
               barCodeHeight + (topMargin * 2));
             Graphics objGraphics = (objBitmap);

             (, 0, 0, , );

             for (int i = 0; i < intSourceLength; i++)
             {
//Illegal character verification
                 if ((sourceCode[i]) == -1 || sourceCode[i] == '*')
                 {
                     ("Invalid Bar Code",
                       , , leftMargin, topMargin);
                     return objBitmap;
                 }
//Coding
                 strEncode = ("{0}0{1}", strEncode,
                  Code39[(sourceCode[i])]);
             }

strEncode = ("{0}0010010100", strEncode); //Add the end code "*"

             int intEncodeLength = ;
             int intBarWidth;

for (int i = 0; i < intEncodeLength; i++) //Draw Code39 barcode
             {
                 intBarWidth = strEncode[i] == '1' ? thickLength : narrowLength;
                 (i % 2 == 0 ? : ,
                  leftMargin, topMargin, intBarWidth, barCodeHeight);
                 leftMargin += intBarWidth;
             }

//Draw the clear code
             SizeF sizeF = (sourceCode, font);
             float x=( - ) / 2;
             float y = - ;
             (, x, y, , );
             (sourceCode, font, , x, y);

             return objBitmap;
         }