C#
  1  using System;
  2  using System.Drawing;
  3  using System.Diagnostics;
  4  
  5  namespace GenCode128
  6  {
  7  	/// <summary>
  8  	/// Summary description for Code128Rendering.
  9  	/// </summary>
 10  	public static class Code128Rendering
 11  	{
 12  
 13        #region Code patterns
 14  
 15        // in principle these rows should each have 6 elements
 16        // however, the last one -- STOP -- has 7. The cost of the
 17        // extra integers is trivial, and this lets the code flow
 18        // much more elegantly
 19        private static readonly int[,] cPatterns = 
 20                       {
 21                          {2,1,2,2,2,2,0,0},  // 0
 22                          {2,2,2,1,2,2,0,0},  // 1
 23                          {2,2,2,2,2,1,0,0},  // 2
 24                          {1,2,1,2,2,3,0,0},  // 3
 25                          {1,2,1,3,2,2,0,0},  // 4
 26                          {1,3,1,2,2,2,0,0},  // 5
 27                          {1,2,2,2,1,3,0,0},  // 6
 28                          {1,2,2,3,1,2,0,0},  // 7
 29                          {1,3,2,2,1,2,0,0},  // 8
 30                          {2,2,1,2,1,3,0,0},  // 9
 31                          {2,2,1,3,1,2,0,0},  // 10
 32                          {2,3,1,2,1,2,0,0},  // 11
 33                          {1,1,2,2,3,2,0,0},  // 12
 34                          {1,2,2,1,3,2,0,0},  // 13
 35                          {1,2,2,2,3,1,0,0},  // 14
 36                          {1,1,3,2,2,2,0,0},  // 15
 37                          {1,2,3,1,2,2,0,0},  // 16
 38                          {1,2,3,2,2,1,0,0},  // 17
 39                          {2,2,3,2,1,1,0,0},  // 18
 40                          {2,2,1,1,3,2,0,0},  // 19
 41                          {2,2,1,2,3,1,0,0},  // 20
 42                          {2,1,3,2,1,2,0,0},  // 21
 43                          {2,2,3,1,1,2,0,0},  // 22
 44                          {3,1,2,1,3,1,0,0},  // 23
 45                          {3,1,1,2,2,2,0,0},  // 24
 46                          {3,2,1,1,2,2,0,0},  // 25
 47                          {3,2,1,2,2,1,0,0},  // 26
 48                          {3,1,2,2,1,2,0,0},  // 27
 49                          {3,2,2,1,1,2,0,0},  // 28
 50                          {3,2,2,2,1,1,0,0},  // 29
 51                          {2,1,2,1,2,3,0,0},  // 30
 52                          {2,1,2,3,2,1,0,0},  // 31
 53                          {2,3,2,1,2,1,0,0},  // 32
 54                          {1,1,1,3,2,3,0,0},  // 33
 55                          {1,3,1,1,2,3,0,0},  // 34
 56                          {1,3,1,3,2,1,0,0},  // 35
 57                          {1,1,2,3,1,3,0,0},  // 36
 58                          {1,3,2,1,1,3,0,0},  // 37
 59                          {1,3,2,3,1,1,0,0},  // 38
 60                          {2,1,1,3,1,3,0,0},  // 39
 61                          {2,3,1,1,1,3,0,0},  // 40
 62                          {2,3,1,3,1,1,0,0},  // 41
 63                          {1,1,2,1,3,3,0,0},  // 42
 64                          {1,1,2,3,3,1,0,0},  // 43
 65                          {1,3,2,1,3,1,0,0},  // 44
 66                          {1,1,3,1,2,3,0,0},  // 45
 67                          {1,1,3,3,2,1,0,0},  // 46
 68                          {1,3,3,1,2,1,0,0},  // 47
 69                          {3,1,3,1,2,1,0,0},  // 48
 70                          {2,1,1,3,3,1,0,0},  // 49
 71                          {2,3,1,1,3,1,0,0},  // 50
 72                          {2,1,3,1,1,3,0,0},  // 51
 73                          {2,1,3,3,1,1,0,0},  // 52
 74                          {2,1,3,1,3,1,0,0},  // 53
 75                          {3,1,1,1,2,3,0,0},  // 54
 76                          {3,1,1,3,2,1,0,0},  // 55
 77                          {3,3,1,1,2,1,0,0},  // 56
 78                          {3,1,2,1,1,3,0,0},  // 57
 79                          {3,1,2,3,1,1,0,0},  // 58
 80                          {3,3,2,1,1,1,0,0},  // 59
 81                          {3,1,4,1,1,1,0,0},  // 60
 82                          {2,2,1,4,1,1,0,0},  // 61
 83                          {4,3,1,1,1,1,0,0},  // 62
 84                          {1,1,1,2,2,4,0,0},  // 63
 85                          {1,1,1,4,2,2,0,0},  // 64
 86                          {1,2,1,1,2,4,0,0},  // 65
 87                          {1,2,1,4,2,1,0,0},  // 66
 88                          {1,4,1,1,2,2,0,0},  // 67
 89                          {1,4,1,2,2,1,0,0},  // 68
 90                          {1,1,2,2,1,4,0,0},  // 69
 91                          {1,1,2,4,1,2,0,0},  // 70
 92                          {1,2,2,1,1,4,0,0},  // 71
 93                          {1,2,2,4,1,1,0,0},  // 72
 94                          {1,4,2,1,1,2,0,0},  // 73
 95                          {1,4,2,2,1,1,0,0},  // 74
 96                          {2,4,1,2,1,1,0,0},  // 75
 97                          {2,2,1,1,1,4,0,0},  // 76
 98                          {4,1,3,1,1,1,0,0},  // 77
 99                          {2,4,1,1,1,2,0,0},  // 78
100                          {1,3,4,1,1,1,0,0},  // 79
101                          {1,1,1,2,4,2,0,0},  // 80
102                          {1,2,1,1,4,2,0,0},  // 81
103                          {1,2,1,2,4,1,0,0},  // 82
104                          {1,1,4,2,1,2,0,0},  // 83
105                          {1,2,4,1,1,2,0,0},  // 84
106                          {1,2,4,2,1,1,0,0},  // 85
107                          {4,1,1,2,1,2,0,0},  // 86
108                          {4,2,1,1,1,2,0,0},  // 87
109                          {4,2,1,2,1,1,0,0},  // 88
110                          {2,1,2,1,4,1,0,0},  // 89
111                          {2,1,4,1,2,1,0,0},  // 90
112                          {4,1,2,1,2,1,0,0},  // 91
113                          {1,1,1,1,4,3,0,0},  // 92
114                          {1,1,1,3,4,1,0,0},  // 93
115                          {1,3,1,1,4,1,0,0},  // 94
116                          {1,1,4,1,1,3,0,0},  // 95
117                          {1,1,4,3,1,1,0,0},  // 96
118                          {4,1,1,1,1,3,0,0},  // 97
119                          {4,1,1,3,1,1,0,0},  // 98
120                          {1,1,3,1,4,1,0,0},  // 99
121                          {1,1,4,1,3,1,0,0},  // 100
122                          {3,1,1,1,4,1,0,0},  // 101
123                          {4,1,1,1,3,1,0,0},  // 102
124                          {2,1,1,4,1,2,0,0},  // 103
125                          {2,1,1,2,1,4,0,0},  // 104
126                          {2,1,1,2,3,2,0,0},  // 105
127                          {2,3,3,1,1,1,2,0}   // 106
128                       };
129  
130        #endregion Code patterns
131  
132        private const int cQuietWidth = 10;
133  
134        /// <summary>
135        /// Make an image of a Code128 barcode for a given string
136        /// </summary>
137        /// <param name="InputData">Message to be encoded</param>
138        /// <param name="BarWeight">Base thickness for bar width (1 or 2 works well)</param>
139        /// <param name="AddQuietZone">Add required horiz margins (use if output is tight)</param>
140        /// <returns>An Image of the Code128 barcode representing the message</returns>
141        public static Image MakeBarcodeImage( string InputData, int BarWeight, bool AddQuietZone )
142        {
143           // get the Code128 codes to represent the message
144           Code128Content content = new Code128Content( InputData );
145           int[] codes = content.Codes;
146  
147           int width, height;
148           width = ( (codes.Length-3) * 11 + 35) * BarWeight;
149           height = Convert.ToInt32( System.Math.Ceiling( Convert.ToSingle(width) * .15F) );
150  
151           if (AddQuietZone)
152           {
153              width += 2 * cQuietWidth * BarWeight;  // on both sides
154           }
155  
156           // get surface to draw on
157           Image myimg = new System.Drawing.Bitmap(width, height);
158           using (Graphics gr = Graphics.FromImage(myimg))
159           {
160  
161              // set to white so we don't have to fill the spaces with white
162              gr.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);
163  
164              // skip quiet zone
165              int cursor = AddQuietZone ? cQuietWidth * BarWeight : 0;
166  
167              for (int codeidx = 0; codeidx < codes.Length; codeidx++)
168              {
169                 int code = codes[codeidx];
170  
171                 // take the bars two at a time: a black and a white
172                 for (int bar = 0; bar < 8; bar += 2)
173                 {
174                    int barwidth = cPatterns[code, bar] * BarWeight;
175                    int spcwidth = cPatterns[code, bar + 1] * BarWeight;
176  
177                    // if width is zero, don't try to draw it
178                    if (barwidth > 0)
179                    {
180                       gr.FillRectangle(System.Drawing.Brushes.Black, cursor, 0, barwidth, height);
181                    }
182  
183                    // note that we never need to draw the space, since we 
184                    // initialized the graphics to all white
185  
186                    // advance cursor beyond this pair
187                    cursor += (barwidth + spcwidth);
188                 }
189              }
190           }
191  
192           return myimg;
193  
194        }
195  
196     }
197  }