namespace GenCode128 { using System; using System.Drawing; /// /// Summary description for Code128Rendering. /// public static class Code128Rendering { private const int CQuietWidth = 10; // Code patterns: // in principle these rows should each have 6 elements // however, the last one -- STOP -- has 7. The cost of the // extra integers is trivial, and this lets the code flow // much more elegantly private static readonly int[,] CPatterns = { { 2, 1, 2, 2, 2, 2, 0, 0 }, // 0 { 2, 2, 2, 1, 2, 2, 0, 0 }, // 1 { 2, 2, 2, 2, 2, 1, 0, 0 }, // 2 { 1, 2, 1, 2, 2, 3, 0, 0 }, // 3 { 1, 2, 1, 3, 2, 2, 0, 0 }, // 4 { 1, 3, 1, 2, 2, 2, 0, 0 }, // 5 { 1, 2, 2, 2, 1, 3, 0, 0 }, // 6 { 1, 2, 2, 3, 1, 2, 0, 0 }, // 7 { 1, 3, 2, 2, 1, 2, 0, 0 }, // 8 { 2, 2, 1, 2, 1, 3, 0, 0 }, // 9 { 2, 2, 1, 3, 1, 2, 0, 0 }, // 10 { 2, 3, 1, 2, 1, 2, 0, 0 }, // 11 { 1, 1, 2, 2, 3, 2, 0, 0 }, // 12 { 1, 2, 2, 1, 3, 2, 0, 0 }, // 13 { 1, 2, 2, 2, 3, 1, 0, 0 }, // 14 { 1, 1, 3, 2, 2, 2, 0, 0 }, // 15 { 1, 2, 3, 1, 2, 2, 0, 0 }, // 16 { 1, 2, 3, 2, 2, 1, 0, 0 }, // 17 { 2, 2, 3, 2, 1, 1, 0, 0 }, // 18 { 2, 2, 1, 1, 3, 2, 0, 0 }, // 19 { 2, 2, 1, 2, 3, 1, 0, 0 }, // 20 { 2, 1, 3, 2, 1, 2, 0, 0 }, // 21 { 2, 2, 3, 1, 1, 2, 0, 0 }, // 22 { 3, 1, 2, 1, 3, 1, 0, 0 }, // 23 { 3, 1, 1, 2, 2, 2, 0, 0 }, // 24 { 3, 2, 1, 1, 2, 2, 0, 0 }, // 25 { 3, 2, 1, 2, 2, 1, 0, 0 }, // 26 { 3, 1, 2, 2, 1, 2, 0, 0 }, // 27 { 3, 2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1, 1, 4, 1, 0, 0 }, // 94 { 1, 1, 4, 1, 1, 3, 0, 0 }, // 95 { 1, 1, 4, 3, 1, 1, 0, 0 }, // 96 { 4, 1, 1, 1, 1, 3, 0, 0 }, // 97 { 4, 1, 1, 3, 1, 1, 0, 0 }, // 98 { 1, 1, 3, 1, 4, 1, 0, 0 }, // 99 { 1, 1, 4, 1, 3, 1, 0, 0 }, // 100 { 3, 1, 1, 1, 4, 1, 0, 0 }, // 101 { 4, 1, 1, 1, 3, 1, 0, 0 }, // 102 { 2, 1, 1, 4, 1, 2, 0, 0 }, // 103 { 2, 1, 1, 2, 1, 4, 0, 0 }, // 104 { 2, 1, 1, 2, 3, 2, 0, 0 }, // 105 { 2, 3, 3, 1, 1, 1, 2, 0 } // 106 }; /// /// Make an image of a Code128 barcode for a given string /// /// Message to be encoded /// Base thickness for bar width (1 or 2 works well) /// Add required horizontal margins (use if output is tight) /// An Image of the Code128 barcode representing the message public static Image MakeBarcodeImage(string inputData, int barWeight, bool addQuietZone) { // get the Code128 codes to represent the message var content = new Code128Content(inputData); var codes = content.Codes; var width = (((codes.Length - 3) * 11) + 35) * barWeight; var height = Convert.ToInt32(Math.Ceiling(Convert.ToSingle(width) * .15F)); if (addQuietZone) { width += 2 * CQuietWidth * barWeight; // on both sides } // get surface to draw on Image myImage = new Bitmap(width, height); using (var gr = Graphics.FromImage(myImage)) { // set to white so we don't have to fill the spaces with white gr.FillRectangle(Brushes.White, 0, 0, width, height); // skip quiet zone var cursor = addQuietZone ? CQuietWidth * barWeight : 0; for (var codeIdx = 0; codeIdx < codes.Length; codeIdx++) { var code = codes[codeIdx]; // take the bars two at a time: a black and a white for (var bar = 0; bar < 8; bar += 2) { var barWidth = CPatterns[code, bar] * barWeight; var spcWidth = CPatterns[code, bar + 1] * barWeight; // if width is zero, don't try to draw it if (barWidth > 0) { gr.FillRectangle(Brushes.Black, cursor, 0, barWidth, height); } // note that we never need to draw the space, since we // initialized the graphics to all white // advance cursor beyond this pair cursor += barWidth + spcWidth; } } } return myImage; } } }