SoFunction
Updated on 2025-03-07

C# barcode generation class sharing

This article shares the specific code of C# barcode generation for your reference. The specific content is as follows

using ; 
using ; 
 
namespace  
{ 
  public class BarCodeToHTML 
  { 
    public static string get39(string s, int width, int height) 
    { 
      Hashtable ht = new Hashtable(); 
 
      #region 39 code 12 bits      ('A', "110101001011"); 
      ('B', "101101001011"); 
      ('C', "110110100101"); 
      ('D', "101011001011"); 
      ('E', "110101100101"); 
      ('F', "101101100101"); 
      ('G', "101010011011"); 
      ('H', "110101001101"); 
      ('I', "101101001101"); 
      ('J', "101011001101"); 
      ('K', "110101010011"); 
      ('L', "101101010011"); 
      ('M', "110110101001"); 
      ('N', "101011010011"); 
      ('O', "110101101001"); 
      ('P', "101101101001"); 
      ('Q', "101010110011"); 
      ('R', "110101011001"); 
      ('S', "101101011001"); 
      ('T', "101011011001"); 
      ('U', "110010101011"); 
      ('V', "100110101011"); 
      ('W', "110011010101"); 
      ('X', "100101101011"); 
      ('Y', "110010110101"); 
      ('Z', "100110110101"); 
      ('0', "101001101101"); 
      ('1', "110100101011"); 
      ('2', "101100101011"); 
      ('3', "110110010101"); 
      ('4', "101001101011"); 
      ('5', "110100110101"); 
      ('6', "101100110101"); 
      ('7', "101001011011"); 
      ('8', "110100101101"); 
      ('9', "101100101101"); 
      ('+', "100101001001"); 
      ('-', "100101011011"); 
      ('*', "100101101101"); 
      ('/', "100100101001"); 
      ('%', "101001001001"); 
      ('$', "100100100101"); 
      ('.', "110010101101"); 
      (' ', "100110101101"); 
      #endregion 
 
      #region 39 digits 9 digits      //('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"); 
      //('*', "010010100"); 
      //('$', "010101000"); 
      //('/', "010100010"); 
      //('+', "010001010"); 
      //('%', "000101010"); 
      #endregion 
 
      s = "*" + () + "*"; 
 
      string result_bin = "";//Binary string 
      try 
      { 
        foreach (char ch in s) 
        { 
          result_bin += ht[ch].ToString(); 
          result_bin += "0";//Space, equal to the width of a unit of line        } 
      } 
      catch { return "There are characters that are not allowed!"; } 
 
      string result_html = ""; //HTML Code      string color = "";    //color      foreach (char c in result_bin) 
      { 
        color = c == '0' ? "#FFFFFF" : "#000000"; 
        result_html += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>"; 
      } 
      result_html += "<div style=\"clear:both\"></div>"; 
 
      int len = ht['*'].ToString().Length; 
      foreach (char c in s) 
      { 
        result_html += "<div style=\"width:" + (width * (len + 1)) + "px;float:left;color:#000000;text-align:center;\">" + c + "</div>"; 
      } 
      result_html += "<div style=\"clear:both\"></div>"; 
 
      return "&lt;div style=\"background:#FFFFF;padding:5px;font-size:" + (width * 10) + "px;font-family:'KaiTi';\">" + result_html + "</div>";    } 
 
    public static string getEAN13(string s, int width, int height) 
    { 
      int checkcode_input = -1;//Input verification code      if (!(s, @"^\d{12}$")) 
      { 
        if (!(s, @"^\d{13}$")) 
        { 
          return "There are characters that are not allowed!"; 
        } 
        else 
        { 
          checkcode_input = (s[12].ToString()); 
          s = (0, 12); 
        } 
      } 
 
      int sum_even = 0;//Sum of even digits      int sum_odd = 0; //Sum of odd numbers 
      for (int i = 0; i &lt; 12; i++) 
      { 
        if (i % 2 == 0) 
        { 
          sum_odd += (s[i].ToString()); 
        } 
        else 
        { 
          sum_even += (s[i].ToString()); 
        } 
      } 
 
      int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//Check code 
      if (checkcode_input &gt; 0 &amp;&amp; checkcode_input != checkcode) 
      { 
        return "The check code entered is wrong!"; 
      } 
 
      s += checkcode;//Become 13 
      // 0000000000101 42 left 01010 right 35 checks 7 1010000000000      // 6 101 6 bits on the left 01010 5 bits on the right 1 bit 101000000000 
      string result_bin = "";//Binary string      result_bin += "000000000101"; 
 
      string type = ean13type(s[0]); 
      for (int i = 1; i &lt; 7; i++) 
      { 
        result_bin += ean13(s[i], type[i - 1]); 
      } 
      result_bin += "01010"; 
      for (int i = 7; i &lt; 13; i++) 
      { 
        result_bin += ean13(s[i], 'C'); 
      } 
      result_bin += "101000000000"; 
 
      string result_html = ""; //HTML Code      string color = "";    //color      int height_bottom = width * 5; 
      foreach (char c in result_bin) 
      { 
        color = c == '0' ? "#FFFFFF" : "#000000"; 
        result_html += "&lt;div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"&gt;&lt;/div&gt;"; 
      } 
      result_html += "&lt;div style=\"clear:both\"&gt;&lt;/div&gt;"; 
 
      result_html += "&lt;div style=\"float:left;color:#000000;width:" + (width * 9) + "px;text-align:center;\"&gt;" + s[0] + "&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"&gt;&lt;/div&gt;"; 
      for (int i = 1; i &lt; 7; i++) 
      { 
        result_html += "&lt;div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\"&gt;" + s[i] + "&lt;/div&gt;"; 
      } 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"&gt;&lt;/div&gt;"; 
      for (int i = 7; i &lt; 13; i++) 
      { 
        result_html += "&lt;div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\"&gt;" + s[i] + "&lt;/div&gt;"; 
      } 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"float:left;color:#000000;width:" + (width * 9) + "px;\"&gt;&lt;/div&gt;"; 
      result_html += "&lt;div style=\"clear:both\"&gt;&lt;/div&gt;"; 
 
      return "&lt;div style=\"background:#FFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:'KaiTi';\">" + result_html + "</div>";    } 
 
    private static string ean13(char c, char type) 
    { 
      switch (type) 
      { 
        case 'A': 
          { 
            switch (c) 
            { 
              case '0': return "0001101"; 
              case '1': return "0011001"; 
              case '2': return "0010011"; 
              case '3': return "0111101";//011101 
              case '4': return "0100011"; 
              case '5': return "0110001"; 
              case '6': return "0101111"; 
              case '7': return "0111011"; 
              case '8': return "0110111"; 
              case '9': return "0001011"; 
              default: return "Error!"; 
            } 
          } 
        case 'B': 
          { 
            switch (c) 
            { 
              case '0': return "0100111"; 
              case '1': return "0110011"; 
              case '2': return "0011011"; 
              case '3': return "0100001"; 
              case '4': return "0011101"; 
              case '5': return "0111001"; 
              case '6': return "0000101";//000101 
              case '7': return "0010001"; 
              case '8': return "0001001"; 
              case '9': return "0010111"; 
              default: return "Error!"; 
            } 
          } 
        case 'C': 
          { 
            switch (c) 
            { 
              case '0': return "1110010"; 
              case '1': return "1100110"; 
              case '2': return "1101100"; 
              case '3': return "1000010"; 
              case '4': return "1011100"; 
              case '5': return "1001110"; 
              case '6': return "1010000"; 
              case '7': return "1000100"; 
              case '8': return "1001000"; 
              case '9': return "1110100"; 
              default: return "Error!"; 
            } 
          } 
        default: return "Error!"; 
      } 
    } 
 
    private static string ean13type(char c) 
    { 
      switch (c) 
      { 
        case '0': return "AAAAAA"; 
        case '1': return "AABABB"; 
        case '2': return "AABBAB"; 
        case '3': return "AABBBA"; 
        case '4': return "ABAABB"; 
        case '5': return "ABBAAB"; 
        case '6': return "ABBBAA";//China        case '7': return "ABABAB"; 
        case '8': return "ABABBA"; 
        case '9': return "ABBABA"; 
        default: return "Error!"; 
      } 
    } 
  } 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.