//绘制条形码 public string DrawCode128(Graphics g, string code, int x, int y) { if (code.Length == 0) { return("Invalid code for Code128 barcode"); } List <int> encoded = new List <int>(); Code128Modes currentMode = Code128Modes.CodeUnset; for (int i = 0; i < code.Length; i++) { if (IsNumber(code[i]) && i + 1 < code.Length && IsNumber(code[i + 1])) { if (currentMode != Code128Modes.CodeC) { if (currentMode == Code128Modes.CodeUnset) { encoded.Add((int)Code128StartModes.CodeC); } else { encoded.Add((int)Code128ChangeModes.CodeC); } currentMode = Code128Modes.CodeC; } encoded.Add(Int32.Parse(code.Substring(i, 2))); i++; } else { if (currentMode != Code128Modes.CodeB) { if (currentMode == Code128Modes.CodeUnset) { encoded.Add((int)Code128StartModes.CodeB); } else { encoded.Add((int)Code128ChangeModes.CodeB); } currentMode = Code128Modes.CodeB; } encoded.Add(EncodeCodeB(code[i])); } } encoded.Add(CheckDigitCode128(encoded)); string barbit = ""; for (int i = 0; i < encoded.Count; i++) { barbit += Code128Encoding[encoded[i]]; } barbit += Code128Stop; barbit += "11"; // end code int barwidth = (int)Math.Floor((double)(width - 2) / (barbit.Length + 20)); // add 20 for padding if (barwidth <= 0) { barwidth = 1; } int padding = barwidth * 10; if (centered) { x = (int)x - (((barwidth * barbit.Length) + (padding * 2)) / 2); } int start = x + padding; for (int i = 1; i <= barbit.Length; i++) { string bit = barbit.Substring(i - 1, 1); if (bit == "0") { g.FillRectangle(Brushes.White, start, y, barwidth, height); } else // bit == "1" { g.FillRectangle(Brushes.Black, start, y, barwidth, height); } start += barwidth; } //显示条码下面的数字code //if (humanReadable) //{ // Font font = new Font(fontName, fontSize, FontStyle.Bold); // SizeF size = g.MeasureString(code, font); // x = x + (int)((barwidth * barbit.Length) + (padding * 2)) / 2; // x -= (int)size.Width / 2; // g.DrawString(code, font, Brushes.Black, x, y + height + 4); //} return(""); }
public string DrawCode128(Graphics g, string code, string nombreproducto, int x, int y) { if (code.Length == 0) { return("Invalid code for Code128 barcode"); } ArrayList encoded = new ArrayList(); // = new int[0]; Code128Modes currentMode = Code128Modes.CodeUnset; for (int i = 0; i < code.Length; i++) { if (IsNumber(code[i]) && i + 1 < code.Length && IsNumber(code[i + 1])) { if (currentMode != Code128Modes.CodeC) { if (currentMode == Code128Modes.CodeUnset) { encoded.Add((int)Code128StartModes.CodeC); } else { encoded.Add((int)Code128ChangeModes.CodeC); } currentMode = Code128Modes.CodeC; } encoded.Add(Int32.Parse(code.Substring(i, 2))); i++; } else { if (currentMode != Code128Modes.CodeB) { if (currentMode == Code128Modes.CodeUnset) { encoded.Add((int)Code128StartModes.CodeB); } else { encoded.Add((int)Code128ChangeModes.CodeB); } currentMode = Code128Modes.CodeB; } encoded.Add(EncodeCodeB(code[i])); } } encoded.Add(CheckDigitCode128((int[])encoded.ToArray(typeof(int)))); string barbit = ""; for (int i = 0; i < encoded.Count; i++) { barbit += Code128Encoding[Int32.Parse(encoded[i].ToString())]; } barbit += Code128Stop; barbit += "11"; // end code int barwidth = (int)Math.Floor((double)(width - 2) / (barbit.Length + 10)); // add 10 for padding if (barwidth <= 0) { barwidth = 1; } int padding = barwidth * 10; if (centered) { x = (int)x + (width / 2) - (((barwidth * barbit.Length) + (padding * 2)) / 2); } int start = x + padding; for (int i = 1; i <= barbit.Length; i++) { string bit = barbit.Substring(i - 1, 1); if (bit == "0") { g.FillRectangle(Brushes.White, start, y, barwidth, height - 20); } else // bit == "1" { g.FillRectangle(Brushes.Black, start, y, barwidth, height - 20); } start += barwidth; } if (humanReadable) { Font font = new Font(fontName, fontSize, FontStyle.Bold); Font font1 = new Font(fontName, fontSizeDescripcion, FontStyle.Bold); SizeF size = g.MeasureString(code, font); SizeF size2 = g.MeasureString(nombreproducto, font1); x = x + (int)((barwidth * barbit.Length) + (padding * 2)) / 2; x -= (int)size.Width / 2; //g.DrawString(code, font, Brushes.Black, x, y + height + 4); g.DrawString(code, font, Brushes.Black, x, y + height - 20); //g.DrawString(nombreproducto, font1, Brushes.Black, -2, y + height + 30); g.DrawString(nombreproducto, font1, Brushes.Black, -2, y + height - 5); } return(""); }