/// <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 += 2 * cQuietWidth * BarWeight; // on both sides } // 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 ? cQuietWidth * BarWeight : 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, 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(myimg); }
public static void MakeBarcodeImage(string InputData, Graphics gr, Rectangle area) { int BarWeight = 1; //bool AddQuietZone = false; // 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 = area.Height;// Convert.ToInt32(System.Math.Ceiling(Convert.ToSingle(width) * .15F)); //if (AddQuietZone) { // width += 2 * cQuietWidth * BarWeight; // on both sides } int scale = Math.Max(1, area.Width / width); // set to white so we don't have to fill the spaces with white gr.FillRectangle(System.Drawing.Brushes.White, area); // skip quiet zone //int cursor = area.Left + (AddQuietZone ? cQuietWidth * BarWeight : 0); int cursor = area.Left + (area.Width - width * scale) / 2; 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 = (int)(cPatterns[code, bar] * BarWeight * scale); int spcwidth = (int)(cPatterns[code, bar + 1] * BarWeight * scale); // if width is zero, don't try to draw it if (barwidth > 0) { gr.FillRectangle(System.Drawing.Brushes.Black, cursor, area.Top, 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); } } }
/// <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 horizontal 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 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); }
/// <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 horizontal margins (use if output is tight)</param> /// <returns>An Image of the Code128 barcode representing the message</returns> public static Image <Rgba32> 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 <Rgba32> myImage = new Image <Rgba32>(width, height); ((Image)myImage).Mutate(ctx => { // set to white so we don't have to fill the spaces with white ctx.Fill(Rgba32.White, new CoreRectangle(0, 0, width, height)); // skip quiet zone var cursor = addQuietZone ? CQuietWidth * barWeight : 0; foreach (var code in codes) { // 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) { ctx.Fill(Rgba32.Black, new CoreRectangle(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); }
/// <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 void MakeBarcodeImage() { format = new StringFormat(); format.Alignment = StringAlignment.Center; font = new Font("arial", (int)(_BarcodeTextSize.Height * 0.75)); blackBrush = new SolidBrush(System.Drawing.Color.Black); // get the Code128 codes to represent the message Code128Content content = new Code128Content(this._Barcode); int[] codes = content.Codes; _BarWeight = _BarSize.Width / ((codes.Length - 3) * 11 + 35); // 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); gr.DrawString(DiscriptionSummary, font, blackBrush, new Rectangle(_DiscriptionPoint, _DiscriptionSize), format); for (int codeidx = 0; codeidx < codes.Length; codeidx++) { int code = codes[codeidx]; Console.WriteLine(code.ToString()); // 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) { _BarSize.Width = barwidth; temp = new Rectangle(_BarcodeImagePoint, _BarSize); gr.FillRectangle(System.Drawing.Brushes.Black, temp); } // note that we never need to draw the space, since we // initialized the graphics to all white // advance cursor beyond this pair _BarcodeImagePoint.X += (barwidth + spcwidth); } } temp = new Rectangle(_BarcodeTextPoint, _BarcodeTextSize); gr.DrawString(this._Barcode, font, blackBrush, temp, format); } _BarcodeImage = myimg; }
public void FullStringTest() { var content = new Code128Content("BarCode 1"); var 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"); }
/// <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 MakeBarcodeImageHoriz(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; BarWeight = 1; // cálculos originales para obtener las dimensiones de la imagen que contiene el código width = ((codes.Length - 3) * 11 + 35) * BarWeight; //height = Convert.ToInt32( System.Math.Ceiling( Convert.ToSingle(width) * .15F) ); // dimensiones estáticas, que son las que necesito //width = 130; height = 30; if (AddQuietZone) { width += 2 * cQuietWidth * BarWeight; // on both sides } // get surface to draw on Image myimg = new System.Drawing.Bitmap(width + 130, 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 + 200, height); // skip quiet zone int cursor = AddQuietZone ? cQuietWidth * BarWeight : 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, 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); } } // Agregada por Tavo: Con esta línea "dibujo" los números del código de barras gr.DrawString(InputData, new Font("Arial", 9, FontStyle.Bold), Brushes.Black, new PointF(width + 20, height / 4)); } return(myimg); }
/// <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, float BarWeight, int BarHeight, bool AddQuietZone ) { // get the Code128 codes to represent the message Code128Content content = new Code128Content( InputData ); int[] codes = content.Codes; float width, height; width = (((codes.Length - 3) * 11 + 35) * BarWeight); //height = Convert.ToInt32( System.Math.Ceiling( Convert.ToSingle(width) * .15F) ); height = BarHeight; if (AddQuietZone) { width += 2 * cQuietWidth * BarWeight; // on both sides } // get surface to draw on Image myimg = new System.Drawing.Bitmap((int)width, (int)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 float cursor = AddQuietZone ? cQuietWidth * BarWeight : 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) { float barwidth = cPatterns[code, bar] * BarWeight; float spcwidth = cPatterns[code, bar + 1] * BarWeight / 1.03F; // if width is zero, don't try to draw it if (barwidth > 0) { gr.FillRectangle(System.Drawing.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 myimg; }
/// <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 horizontal 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 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; }