public void initBarcode(string barcode, string discription, string itemcode) { this._BarcodeInfo.Barcode = barcode; this._BarcodeInfo.Description = this.DiscriptionSummary; Code128Content content = new Code128Content(this._BarcodeInfo.Barcode); int[] codes = content.Codes; this._BarcodeRenterInfo.Patterns = Code128Rendering.CreatePatterns(codes); foreach (int x in this._BarcodeRenterInfo.Patterns) { Console.WriteLine(x.ToString()); } this._BarcodeRenterInfo.BarColor = Color.Black; this._BarcodeRenterInfo.ShowBarcodeText = true; this._BarcodeRenterInfo.ShowDescription = true; try { this._BarcodeRenterInfo.TextFont = new Font(SystemFonts.DefaultFont.FontFamily, (float)BarcodePrinter.Properties.Settings.Default.FontSize, FontStyle.Regular); } catch (Exception) { } this._BarcodeRenterInfo.SetMarginAll(4); this._BarcodeRenterInfo.QuitZone = new System.Drawing.Printing.Margins(2, 2, 2, 2); }
/// <summary> /// Make an image of a Code128 barcode for a given string /// </summary> /// <param name="InputData">Message to be encoded</param> /// <param name="BarWeight">Base thickness for bar width (1 or 2 works well)</param> /// <param name="AddQuietZone">Add required horiz margins (use if output is tight)</param> /// <returns>An Image of the Code128 barcode representing the message</returns> public static Image MakeBarcodeImage(string InputData, int BarWeight, bool AddQuietZone) { // get the Code128 codes to represent the message Code128Content content = new Code128Content(InputData); int[] codes = content.Codes; int width, height; width = ((codes.Length - 3) * 11 + 35) * BarWeight; height = Convert.ToInt32(System.Math.Ceiling(Convert.ToSingle(width) * .15F)); if (AddQuietZone) { width += 20; // on both sides } //draw the codetext bottom height += 30; // get surface to draw on Image myimg = new System.Drawing.Bitmap(width, height); using (Graphics gr = Graphics.FromImage(myimg)) { // set to white so we don't have to fill the spaces with white gr.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height); // skip quiet zone int cursor = AddQuietZone ? 10 : 0; for (int codeidx = 0; codeidx < codes.Length; codeidx++) { int code = codes[codeidx]; // take the bars two at a time: a black and a white for (int bar = 0; bar < 8; bar += 2) { int barwidth = cPatterns[code, bar] * BarWeight; int spcwidth = cPatterns[code, bar + 1] * BarWeight; // if width is zero, don't try to draw it if (barwidth > 0) { gr.FillRectangle(System.Drawing.Brushes.Black, cursor, 5, barwidth, height - 20); } // 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); } } gr.DrawString(InputData, new Font("Arial", 9), Brushes.Black, new PointF(10, height - 15)); } return(myimg); }
public void OneCharStringTest() { var content = new Code128Content("0"); var result = content.Codes; Assert.AreEqual(4, result.Length, "Wrong number of code values in result"); Assert.AreEqual(104, result[0], "Start code wrong"); Assert.AreEqual(16, result[1], "Code value #1 wrong"); Assert.AreEqual(17, result[2], "Checksum wrong"); Assert.AreEqual(106, result[3], "Stop character wrong"); }
/// <summary> /// 绘制条码 /// </summary> /// <param name="barCodeValue"></param> /// <param name="barWeight"></param> /// <param name="barHeight"></param> /// <param name="x"></param> /// <param name="y"></param> public void DrawBarcode(string barCodeValue, float barHeight, float x, float y, bool viewText, bool cancel) { bool AddQuietZone = true; float barWeight = 1F / PelsValue; barHeight = PelsToMM(barHeight); x = PelsToMM(x); y = PelsToMM(y); Code128Content content = new Code128Content(barCodeValue); int[] codes = content.Codes; float width = ((codes.Length - 3) * 11 + 35) * barWeight; //height = Convert.ToInt32(System.Math.Ceiling(Convert.ToSingle(width) * .15F)); if (AddQuietZone) { width += 2 * cQuietWidth * barWeight; } float cursor = x;//cQuietWidth * barWeight+x; for (int codeidx = 0; codeidx < codes.Length; codeidx++) { int code = codes[codeidx]; for (int bar = 0; bar < 8; bar += 2) { float barwidth = cPatterns[code, bar] * barWeight; float spcwidth = cPatterns[code, bar + 1] * barWeight; if (barwidth > 0) { _MainGraphics.FillRectangle(cancel ? whiteBrush : blackBrush, cursor, y, barwidth, barHeight); } cursor += (barwidth + spcwidth); } } if (viewText) { Font drawFont = new Font("宋体", 11); PointF drawPoint = new PointF(x, y + barHeight + 2); _MainGraphics.DrawString(barCodeValue, drawFont, cancel ? whiteBrush : blackBrush, drawPoint); } }
public static System.Drawing.Image MakeShippingBarcode(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 System.Drawing.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); }
public void FullStringTest() { Code128Content content = new Code128Content("BarCode 1"); int[] result = content.Codes; Assert.AreEqual(12, result.Length, "Wrong number of code values in result"); Assert.AreEqual(104, result[0], "Start code wrong"); Assert.AreEqual(34, result[1], "Code value #1 wrong"); Assert.AreEqual(65, result[2], "Code value #2 wrong"); Assert.AreEqual(82, result[3], "Code value #3 wrong"); Assert.AreEqual(35, result[4], "Code value #4 wrong"); Assert.AreEqual(79, result[5], "Code value #5 wrong"); Assert.AreEqual(68, result[6], "Code value #6 wrong"); Assert.AreEqual(69, result[7], "Code value #7 wrong"); Assert.AreEqual(0, result[8], "Code value #8 wrong"); Assert.AreEqual(17, result[9], "Code value #9 wrong"); Assert.AreEqual(33, result[10], "Checksum wrong"); Assert.AreEqual(106, result[11], "Stop character wrong"); content = new Code128Content("\x11S12345"); result = content.Codes; Assert.AreEqual(10, result.Length, "Wrong number of code values in result"); }