1 using System;
2 using System.Drawing;
3 using System.Diagnostics;
4
5 namespace GenCode128
6 {
7 public static class Code128Rendering
11 {
12
13 #region Code patterns
14
15
16
17
18
19 private static readonly int[,] cPatterns =
20 {
21 {2,1,2,2,2,2,0,0},
22 {2,2,2,1,2,2,0,0},
23 {2,2,2,2,2,1,0,0},
24 {1,2,1,2,2,3,0,0},
25 {1,2,1,3,2,2,0,0},
26 {1,3,1,2,2,2,0,0},
27 {1,2,2,2,1,3,0,0},
28 {1,2,2,3,1,2,0,0},
29 {1,3,2,2,1,2,0,0},
30 {2,2,1,2,1,3,0,0},
31 {2,2,1,3,1,2,0,0},
32 {2,3,1,2,1,2,0,0},
33 {1,1,2,2,3,2,0,0},
34 {1,2,2,1,3,2,0,0},
35 {1,2,2,2,3,1,0,0},
36 {1,1,3,2,2,2,0,0},
37 {1,2,3,1,2,2,0,0},
38 {1,2,3,2,2,1,0,0},
39 {2,2,3,2,1,1,0,0},
40 {2,2,1,1,3,2,0,0},
41 {2,2,1,2,3,1,0,0},
42 {2,1,3,2,1,2,0,0},
43 {2,2,3,1,1,2,0,0},
44 {3,1,2,1,3,1,0,0},
45 {3,1,1,2,2,2,0,0},
46 {3,2,1,1,2,2,0,0},
47 {3,2,1,2,2,1,0,0},
48 {3,1,2,2,1,2,0,0},
49 {3,2,2,1,1,2,0,0},
50 {3,2,2,2,1,1,0,0},
51 {2,1,2,1,2,3,0,0},
52 {2,1,2,3,2,1,0,0},
53 {2,3,2,1,2,1,0,0},
54 {1,1,1,3,2,3,0,0},
55 {1,3,1,1,2,3,0,0},
56 {1,3,1,3,2,1,0,0},
57 {1,1,2,3,1,3,0,0},
58 {1,3,2,1,1,3,0,0},
59 {1,3,2,3,1,1,0,0},
60 {2,1,1,3,1,3,0,0},
61 {2,3,1,1,1,3,0,0},
62 {2,3,1,3,1,1,0,0},
63 {1,1,2,1,3,3,0,0},
64 {1,1,2,3,3,1,0,0},
65 {1,3,2,1,3,1,0,0},
66 {1,1,3,1,2,3,0,0},
67 {1,1,3,3,2,1,0,0},
68 {1,3,3,1,2,1,0,0},
69 {3,1,3,1,2,1,0,0},
70 {2,1,1,3,3,1,0,0},
71 {2,3,1,1,3,1,0,0},
72 {2,1,3,1,1,3,0,0},
73 {2,1,3,3,1,1,0,0},
74 {2,1,3,1,3,1,0,0},
75 {3,1,1,1,2,3,0,0},
76 {3,1,1,3,2,1,0,0},
77 {3,3,1,1,2,1,0,0},
78 {3,1,2,1,1,3,0,0},
79 {3,1,2,3,1,1,0,0},
80 {3,3,2,1,1,1,0,0},
81 {3,1,4,1,1,1,0,0},
82 {2,2,1,4,1,1,0,0},
83 {4,3,1,1,1,1,0,0},
84 {1,1,1,2,2,4,0,0},
85 {1,1,1,4,2,2,0,0},
86 {1,2,1,1,2,4,0,0},
87 {1,2,1,4,2,1,0,0},
88 {1,4,1,1,2,2,0,0},
89 {1,4,1,2,2,1,0,0},
90 {1,1,2,2,1,4,0,0},
91 {1,1,2,4,1,2,0,0},
92 {1,2,2,1,1,4,0,0},
93 {1,2,2,4,1,1,0,0},
94 {1,4,2,1,1,2,0,0},
95 {1,4,2,2,1,1,0,0},
96 {2,4,1,2,1,1,0,0},
97 {2,2,1,1,1,4,0,0},
98 {4,1,3,1,1,1,0,0},
99 {2,4,1,1,1,2,0,0},
100 {1,3,4,1,1,1,0,0},
101 {1,1,1,2,4,2,0,0},
102 {1,2,1,1,4,2,0,0},
103 {1,2,1,2,4,1,0,0},
104 {1,1,4,2,1,2,0,0},
105 {1,2,4,1,1,2,0,0},
106 {1,2,4,2,1,1,0,0},
107 {4,1,1,2,1,2,0,0},
108 {4,2,1,1,1,2,0,0},
109 {4,2,1,2,1,1,0,0},
110 {2,1,2,1,4,1,0,0},
111 {2,1,4,1,2,1,0,0},
112 {4,1,2,1,2,1,0,0},
113 {1,1,1,1,4,3,0,0},
114 {1,1,1,3,4,1,0,0},
115 {1,3,1,1,4,1,0,0},
116 {1,1,4,1,1,3,0,0},
117 {1,1,4,3,1,1,0,0},
118 {4,1,1,1,1,3,0,0},
119 {4,1,1,3,1,1,0,0},
120 {1,1,3,1,4,1,0,0},
121 {1,1,4,1,3,1,0,0},
122 {3,1,1,1,4,1,0,0},
123 {4,1,1,1,3,1,0,0},
124 {2,1,1,4,1,2,0,0},
125 {2,1,1,2,1,4,0,0},
126 {2,1,1,2,3,2,0,0},
127 {2,3,3,1,1,1,2,0}
128 };
129
130 #endregion Code patterns
131
132 private const int cQuietWidth = 10;
133
134 public static Image MakeBarcodeImage( string InputData, int BarWeight, bool AddQuietZone )
142 {
143
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;
154 }
155
156
157 Image myimg = new System.Drawing.Bitmap(width, height);
158 using (Graphics gr = Graphics.FromImage(myimg))
159 {
160
161
162 gr.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);
163
164
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
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
178 if (barwidth > 0)
179 {
180 gr.FillRectangle(System.Drawing.Brushes.Black, cursor, 0, barwidth, height);
181 }
182
183
184
185
186
187 cursor += (barwidth + spcwidth);
188 }
189 }
190 }
191
192 return myimg;
193
194 }
195
196 }
197 }