条形码打印解决方案,支持多种条码格式、批量打印、标签设计等功能。

BarcodePrinter/ ├── Program.cs ├── MainForm.cs ├── BarcodeGenerator.cs ├── PrintManager.cs ├── LabelDesigner.cs ├── BarcodeTypes.cs └── App.config
using System;
using System.Windows.Forms;
namespace BarcodePrinter
{
internal static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace BarcodePrinter
{
public partial class MainForm : Form
{
private BarcodeGenerator barcodeGenerator;
private PrintManager printManager;
private LabelDesigner labelDesigner;
public MainForm()
{
InitializeComponent();
barcodeGenerator = new BarcodeGenerator();
printManager = new PrintManager();
labelDesigner = new LabelDesigner();
LoadBarcodeTypes();
}
private void LoadBarcodeTypes()
{
cmbBarcodeType.Items.AddRange(BarcodeTypes.GetSupportedTypes());
cmbBarcodeType.SelectedIndex = 0;
}
private void btnGenerate_Click(object sender, EventArgs e)
{
try
{
string data = txtBarcodeData.Text.Trim();
string type = cmbBarcodeType.SelectedItem.ToString();
if (string.IsNullOrEmpty(data))
{
MessageBox.Show("请输入条形码数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Image barcodeImage = barcodeGenerator.GenerateBarcode(data, type,
(int)numWidth.Value, (int)numHeight.Value);
pictureBox.Image = barcodeImage;
lblStatus.Text = $"条形码生成成功 ({type})";
}
catch (Exception ex)
{
MessageBox.Show($"生成失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
if (pictureBox.Image == null)
{
MessageBox.Show("请先生成条形码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printManager.CreatePrintDocument(pictureBox.Image, txtLabel.Text);
if (printDialog.ShowDialog() == DialogResult.OK)
{
printManager.Print(printDialog.Document);
lblStatus.Text = "打印任务已发送";
}
}
catch (Exception ex)
{
MessageBox.Show($"打印失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnBatchPrint_Click(object sender, EventArgs e)
{
BatchPrintForm batchForm = new BatchPrintForm(barcodeGenerator);
batchForm.ShowDialog();
}
private void btnDesignLabel_Click(object sender, EventArgs e)
{
LabelDesignForm designForm = new LabelDesignForm(labelDesigner);
designForm.ShowDialog();
}
private void numCopies_ValueChanged(object sender, EventArgs e)
{
printManager.SetCopies((int)numCopies.Value);
}
#region Windows Form Designer generated code
private System.ComponentModel.IContainer components = null;
private PictureBox pictureBox;
private TextBox txtBarcodeData;
private ComboBox cmbBarcodeType;
private Button btnGenerate;
private Button btnPrint;
private Button btnBatchPrint;
private Button btnDesignLabel;
private Label label1;
private Label label2;
private Label label3;
private Label label4;
private NumericUpDown numWidth;
private NumericUpDown numHeight;
private NumericUpDown numCopies;
private TextBox txtLabel;
private Label lblStatus;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.txtBarcodeData = new System.Windows.Forms.TextBox();
this.cmbBarcodeType = new System.Windows.Forms.ComboBox();
this.btnGenerate = new System.Windows.Forms.Button();
this.btnPrint = new System.Windows.Forms.Button();
this.btnBatchPrint = new System.Windows.Forms.Button();
this.btnDesignLabel = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.numWidth = new System.Windows.Forms.NumericUpDown();
this.numHeight = new System.Windows.Forms.NumericUpDown();
this.numCopies = new System.Windows.Forms.NumericUpDown();
this.txtLabel = new System.Windows.Forms.TextBox();
this.lblStatus = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numWidth)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numHeight)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numCopies)).BeginInit();
this.SuspendLayout();
// pictureBox
this.pictureBox.BackColor = System.Drawing.Color.White;
this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox.Location = new System.Drawing.Point(12, 12);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(400, 200);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
// txtBarcodeData
this.txtBarcodeData.Location = new System.Drawing.Point(12, 230);
this.txtBarcodeData.Name = "txtBarcodeData";
this.txtBarcodeData.Size = new System.Drawing.Size(200, 23);
this.txtBarcodeData.TabIndex = 1;
this.txtBarcodeData.Text = "123456789012";
// cmbBarcodeType
this.cmbBarcodeType.FormattingEnabled = true;
this.cmbBarcodeType.Location = new System.Drawing.Point(218, 230);
this.cmbBarcodeType.Name = "cmbBarcodeType";
this.cmbBarcodeType.Size = new System.Drawing.Size(121, 23);
this.cmbBarcodeType.TabIndex = 2;
// btnGenerate
this.btnGenerate.Location = new System.Drawing.Point(345, 228);
this.btnGenerate.Name = "btnGenerate";
this.btnGenerate.Size = new System.Drawing.Size(75, 23);
this.btnGenerate.TabIndex = 3;
this.btnGenerate.Text = "生成";
this.btnGenerate.UseVisualStyleBackColor = true;
this.btnGenerate.Click += new System.EventHandler(this.btnGenerate_Click);
// btnPrint
this.btnPrint.Location = new System.Drawing.Point(426, 228);
this.btnPrint.Name = "btnPrint";
this.btnPrint.Size = new System.Drawing.Size(75, 23);
this.btnPrint.TabIndex = 4;
this.btnPrint.Text = "打印";
this.btnPrint.UseVisualStyleBackColor = true;
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
// btnBatchPrint
this.btnBatchPrint.Location = new System.Drawing.Point(12, 270);
this.btnBatchPrint.Name = "btnBatchPrint";
this.btnBatchPrint.Size = new System.Drawing.Size(100, 30);
this.btnBatchPrint.TabIndex = 5;
this.btnBatchPrint.Text = "批量打印";
this.btnBatchPrint.UseVisualStyleBackColor = true;
this.btnBatchPrint.Click += new System.EventHandler(this.btnBatchPrint_Click);
// btnDesignLabel
this.btnDesignLabel.Location = new System.Drawing.Point(118, 270);
this.btnDesignLabel.Name = "btnDesignLabel";
this.btnDesignLabel.Size = new System.Drawing.Size(100, 30);
this.btnDesignLabel.TabIndex = 6;
this.btnDesignLabel.Text = "标签设计";
this.btnDesignLabel.UseVisualStyleBackColor = true;
this.btnDesignLabel.Click += new System.EventHandler(this.btnDesignLabel_Click);
// label1
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 310);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 15);
this.label1.TabIndex = 7;
this.label1.Text = "标签文字:";
// txtLabel
this.txtLabel.Location = new System.Drawing.Point(74, 307);
this.txtLabel.Name = "txtLabel";
this.txtLabel.Size = new System.Drawing.Size(144, 23);
this.txtLabel.TabIndex = 8;
this.txtLabel.Text = "产品名称";
// label2
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(224, 310);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(44, 15);
this.label2.TabIndex = 9;
this.label2.Text = "宽度:";
// numWidth
this.numWidth.Location = new System.Drawing.Point(274, 308);
this.numWidth.Maximum = new decimal(new int[] { 500, 0, 0, 0 });
this.numWidth.Minimum = new decimal(new int[] { 50, 0, 0, 0 });
this.numWidth.Name = "numWidth";
this.numWidth.Size = new System.Drawing.Size(65, 23);
this.numWidth.TabIndex = 10;
this.numWidth.Value = new decimal(new int[] { 300, 0, 0, 0 });
// label3
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(345, 310);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(44, 15);
this.label3.TabIndex = 11;
this.label3.Text = "高度:";
// numHeight
this.numHeight.Location = new System.Drawing.Point(395, 308);
this.numHeight.Maximum = new decimal(new int[] { 300, 0, 0, 0 });
this.numHeight.Minimum = new decimal(new int[] { 50, 0, 0, 0 });
this.numHeight.Name = "numHeight";
this.numHeight.Size = new System.Drawing.Size(65, 23);
this.numHeight.TabIndex = 12;
this.numHeight.Value = new decimal(new int[] { 150, 0, 0, 0 });
// label4
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(466, 310);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(44, 15);
this.label4.TabIndex = 13;
this.label4.Text = "份数:";
// numCopies
this.numCopies.Location = new System.Drawing.Point(516, 308);
this.numCopies.Maximum = new decimal(new int[] { 100, 0, 0, 0 });
this.numCopies.Minimum = new decimal(new int[] { 1, 0, 0, 0 });
this.numCopies.Name = "numCopies";
this.numCopies.Size = new System.Drawing.Size(65, 23);
this.numCopies.TabIndex = 14;
this.numCopies.Value = new decimal(new int[] { 1, 0, 0, 0 });
this.numCopies.ValueChanged += new System.EventHandler(this.numCopies_ValueChanged);
// lblStatus
this.lblStatus.AutoSize = true;
this.lblStatus.Location = new System.Drawing.Point(12, 350);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(61, 15);
this.lblStatus.TabIndex = 15;
this.lblStatus.Text = "就绪...";
// MainForm
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 380);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.numCopies);
this.Controls.Add(this.label4);
this.Controls.Add(this.numHeight);
this.Controls.Add(this.label3);
this.Controls.Add(this.numWidth);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtLabel);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnDesignLabel);
this.Controls.Add(this.btnBatchPrint);
this.Controls.Add(this.btnPrint);
this.Controls.Add(this.btnGenerate);
this.Controls.Add(this.cmbBarcodeType);
this.Controls.Add(this.txtBarcodeData);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Name = "MainForm";
this.Text = "条形码打印程序";
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numWidth)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numHeight)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numCopies)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
}
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
using ZXing;
using ZXing.Common;
using ZXing.OneD;
namespace BarcodePrinter
{
public class BarcodeGenerator
{
/// <summary>
/// 生成条形码
/// </summary>
/// <param name="data">条码数据</param>
/// <param name="type">条码类型</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns>条形码图片</returns>
public Image GenerateBarcode(string data, string type, int width, int height)
{
try
{
BarcodeFormat format = BarcodeTypes.GetFormat(type);
EncodingOptions options = new EncodingOptions
{
Width = width,
Height = height,
Margin = 10,
PureBarcode = false
};
BarcodeWriter writer = new BarcodeWriter
{
Format = format,
Options = options
};
return writer.Write(data);
}
catch (Exception ex)
{
throw new Exception($"生成条形码失败: {ex.Message}");
}
}
/// <summary>
/// 生成带标签的条形码
/// </summary>
public Image GenerateBarcodeWithLabel(string data, string type, string label, int width, int height)
{
Image barcode = GenerateBarcode(data, type, width, height);
Bitmap result = new Bitmap(width, height + 40);
using (Graphics g = Graphics.FromImage(result))
{
g.Clear(Color.White);
g.DrawImage(barcode, 0, 0);
// 添加标签文字
using (Font font = new Font("Arial", 10))
using (Brush brush = new SolidBrush(Color.Black))
{
StringFormat sf = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
g.DrawString(label, font, brush, new Rectangle(0, height, width, 40), sf);
}
}
return result;
}
/// <summary>
/// 生成二维码
/// </summary>
public Image GenerateQRCode(string data, int size)
{
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Width = size,
Height = size,
Margin = 1
}
};
return writer.Write(data);
}
}
}
using System.Collections.Generic;
using ZXing;
namespace BarcodePrinter
{
public static class BarcodeTypes
{
private static readonly Dictionary<string, BarcodeFormat> TypeMap = new Dictionary<string, BarcodeFormat>
{
["Code128"] = BarcodeFormat.CODE_128,
["Code39"] = BarcodeFormat.CODE_39,
["EAN13"] = BarcodeFormat.EAN_13,
["EAN8"] = BarcodeFormat.EAN_8,
["UPC-A"] = BarcodeFormat.UPC_A,
["UPC-E"] = BarcodeFormat.UPC_E,
["ITF"] = BarcodeFormat.ITF,
["Codabar"] = BarcodeFormat.CODABAR,
["RSS14"] = BarcodeFormat.RSS_14,
["RSSExpanded"] = BarcodeFormat.RSS_EXPANDED,
["QR Code"] = BarcodeFormat.QR_CODE,
["DataMatrix"] = BarcodeFormat.DATA_MATRIX,
["PDF417"] = BarcodeFormat.PDF_417,
["Aztec"] = BarcodeFormat.AZTEC
};
public static string[] GetSupportedTypes()
{
return new string[]
{
"Code128", "Code39", "EAN13", "EAN8", "UPC-A", "UPC-E",
"ITF", "Codabar", "QR Code", "DataMatrix", "PDF417"
};
}
public static BarcodeFormat GetFormat(string typeName)
{
if (TypeMap.TryGetValue(typeName, out BarcodeFormat format))
{
return format;
}
throw new ArgumentException($"不支持的条码类型: {typeName}");
}
}
}
using System.Drawing;
using System.Drawing.Printing;
namespace BarcodePrinter
{
public class PrintManager
{
private Image barcodeImage;
private string labelText;
private int copies = 1;
public void SetCopies(int copies)
{
this.copies = copies;
}
public PrintDocument CreatePrintDocument(Image image, string label)
{
barcodeImage = image;
labelText = label;
PrintDocument document = new PrintDocument();
document.PrintPage += Document_PrintPage;
document.EndPrint += Document_EndPrint;
return document;
}
public void Print(PrintDocument document)
{
document.Print();
}
private void Document_PrintPage(object sender, PrintPageEventArgs e)
{
// 计算打印位置(居中)
float x = (e.PageBounds.Width - barcodeImage.Width) / 2;
float y = (e.PageBounds.Height - barcodeImage.Height) / 2 - 20;
// 绘制条形码
e.Graphics.DrawImage(barcodeImage, x, y);
// 绘制标签文字
if (!string.IsNullOrEmpty(labelText))
{
using (Font font = new Font("Arial", 12))
using (Brush brush = new SolidBrush(Color.Black))
{
StringFormat sf = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
RectangleF rect = new RectangleF(0, y + barcodeImage.Height + 5,
e.PageBounds.Width, 30);
e.Graphics.DrawString(labelText, font, brush, rect, sf);
}
}
// 设置是否还有更多页
e.HasMorePages = false;
}
private void Document_EndPrint(object sender, PrintEventArgs e)
{
// 清理资源
if (barcodeImage != null)
{
barcodeImage.Dispose();
barcodeImage = null;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
namespace BarcodePrinter
{
public class LabelDesigner
{
public List<LabelElement> Elements { get; private set; }
public Size LabelSize { get; set; }
public LabelDesigner()
{
Elements = new List<LabelElement>();
LabelSize = new Size(400, 300);
}
public void AddBarcode(string data, string type, Point position, Size size)
{
Elements.Add(new LabelElement
{
Type = ElementType.Barcode,
Data = data,
BarcodeType = type,
Position = position,
Size = size
});
}
public void AddText(string text, Point position, Font font, Color color)
{
Elements.Add(new LabelElement
{
Type = ElementType.Text,
Data = text,
Font = font,
Color = color,
Position = position
});
}
public void AddImage(Image image, Point position, Size size)
{
Elements.Add(new LabelElement
{
Type = ElementType.Image,
Image = image,
Position = position,
Size = size
});
}
public Image GenerateLabel()
{
Bitmap label = new Bitmap(LabelSize.Width, LabelSize.Height);
using (Graphics g = Graphics.FromImage(label))
{
g.Clear(Color.White);
foreach (var element in Elements)
{
switch (element.Type)
{
case ElementType.Barcode:
// 这里会调用BarcodeGenerator生成条码
break;
case ElementType.Text:
using (Brush brush = new SolidBrush(element.Color))
{
g.DrawString(element.Data, element.Font, brush, element.Position);
}
break;
case ElementType.Image:
g.DrawImage(element.Image, new Rectangle(element.Position, element.Size));
break;
}
}
}
return label;
}
}
public enum ElementType
{
Barcode,
Text,
Image
}
public class LabelElement
{
public ElementType Type { get; set; }
public string Data { get; set; }
public string BarcodeType { get; set; }
public Font Font { get; set; }
public Color Color { get; set; }
public Image Image { get; set; }
public Point Position { get; set; }
public Size Size { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace BarcodePrinter
{
public partial class BatchPrintForm : Form
{
private BarcodeGenerator generator;
private List<string> barcodeDataList;
public BatchPrintForm(BarcodeGenerator barcodeGenerator)
{
InitializeComponent();
generator = barcodeGenerator;
barcodeDataList = new List<string>();
}
private void btnAddData_Click(object sender, EventArgs e)
{
string data = txtNewData.Text.Trim();
if (!string.IsNullOrEmpty(data))
{
barcodeDataList.Add(data);
listBox.Items.Add(data);
txtNewData.Clear();
lblCount.Text = $"共 {barcodeDataList.Count} 条数据";
}
}
private void btnRemoveData_Click(object sender, EventArgs e)
{
if (listBox.SelectedIndex >= 0)
{
barcodeDataList.RemoveAt(listBox.SelectedIndex);
listBox.Items.RemoveAt(listBox.SelectedIndex);
lblCount.Text = $"共 {barcodeDataList.Count} 条数据";
}
}
private void btnImportFromFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "文本文件|*.txt|CSV文件|*.csv|所有文件|*.*"
};
if (ofd.ShowDialog() == DialogResult.OK)
{
string[] lines = System.IO.File.ReadAllLines(ofd.FileName);
foreach (string line in lines)
{
if (!string.IsNullOrWhiteSpace(line))
{
barcodeDataList.Add(line.Trim());
listBox.Items.Add(line.Trim());
}
}
lblCount.Text = $"共 {barcodeDataList.Count} 条数据";
}
}
private void btnStartBatchPrint_Click(object sender, EventArgs e)
{
if (barcodeDataList.Count == 0)
{
MessageBox.Show("请先添加条码数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
int successCount = 0;
string type = cmbBatchType.SelectedItem.ToString();
foreach (string data in barcodeDataList)
{
try
{
Image barcode = generator.GenerateBarcode(data, type,
(int)numBatchWidth.Value, (int)numBatchHeight.Value);
PrintDocument doc = new PrintDocument();
doc.PrintPage += (sender, args) =>
{
args.Graphics.DrawImage(barcode, 50, 50);
args.HasMorePages = false;
};
doc.PrinterSettings = printDialog.PrinterSettings;
doc.Print();
successCount++;
barcode.Dispose();
}
catch (Exception ex)
{
MessageBox.Show($"打印 {data} 失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
MessageBox.Show($"批量打印完成!成功打印 {successCount}/{barcodeDataList.Count} 个条码",
"完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#region Windows Form Designer generated code
private System.ComponentModel.IContainer components = null;
private TextBox txtNewData;
private Button btnAddData;
private Button btnRemoveData;
private Button btnImportFromFile;
private ListBox listBox;
private Label lblCount;
private ComboBox cmbBatchType;
private NumericUpDown numBatchWidth;
private NumericUpDown numBatchHeight;
private Button btnStartBatchPrint;
private Label label1;
private Label label2;
private Label label3;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.txtNewData = new System.Windows.Forms.TextBox();
this.btnAddData = new System.Windows.Forms.Button();
this.btnRemoveData = new System.Windows.Forms.Button();
this.btnImportFromFile = new System.Windows.Forms.Button();
this.listBox = new System.Windows.Forms.ListBox();
this.lblCount = new System.Windows.Forms.Label();
this.cmbBatchType = new System.Windows.Forms.ComboBox();
this.numBatchWidth = new System.Windows.Forms.NumericUpDown();
this.numBatchHeight = new System.Windows.Forms.NumericUpDown();
this.btnStartBatchPrint = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.numBatchWidth)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numBatchHeight)).BeginInit();
this.SuspendLayout();
// txtNewData
this.txtNewData.Location = new System.Drawing.Point(12, 12);
this.txtNewData.Name = "txtNewData";
this.txtNewData.Size = new System.Drawing.Size(200, 23);
this.txtNewData.TabIndex = 0;
// btnAddData
this.btnAddData.Location = new System.Drawing.Point(218, 10);
this.btnAddData.Name = "btnAddData";
this.btnAddData.Size = new System.Drawing.Size(75, 23);
this.btnAddData.TabIndex = 1;
this.btnAddData.Text = "添加";
this.btnAddData.UseVisualStyleBackColor = true;
this.btnAddData.Click += new System.EventHandler(this.btnAddData_Click);
// btnRemoveData
this.btnRemoveData.Location = new System.Drawing.Point(299, 10);
this.btnRemoveData.Name = "btnRemoveData";
this.btnRemoveData.Size = new System.Drawing.Size(75, 23);
this.btnRemoveData.TabIndex = 2;
this.btnRemoveData.Text = "删除";
this.btnRemoveData.UseVisualStyleBackColor = true;
this.btnRemoveData.Click += new System.EventHandler(this.btnRemoveData_Click);
// btnImportFromFile
this.btnImportFromFile.Location = new System.Drawing.Point(380, 10);
this.btnImportFromFile.Name = "btnImportFromFile";
this.btnImportFromFile.Size = new System.Drawing.Size(100, 23);
this.btnImportFromFile.TabIndex = 3;
this.btnImportFromFile.Text = "导入文件";
this.btnImportFromFile.UseVisualStyleBackColor = true;
this.btnImportFromFile.Click += new System.EventHandler(this.btnImportFromFile_Click);
// listBox
this.listBox.FormattingEnabled = true;
this.listBox.ItemHeight = 15;
this.listBox.Location = new System.Drawing.Point(12, 41);
this.listBox.Name = "listBox";
this.listBox.Size = new System.Drawing.Size(468, 199);
this.listBox.TabIndex = 4;
// lblCount
this.lblCount.AutoSize = true;
this.lblCount.Location = new System.Drawing.Point(12, 246);
this.lblCount.Name = "lblCount";
this.lblCount.Size = new System.Drawing.Size(41, 15);
this.lblCount.TabIndex = 5;
this.lblCount.Text = "共 0 条";
// cmbBatchType
this.cmbBatchType.FormattingEnabled = true;
this.cmbBatchType.Items.AddRange(BarcodeTypes.GetSupportedTypes());
this.cmbBatchType.Location = new System.Drawing.Point(12, 275);
this.cmbBatchType.Name = "cmbBatchType";
this.cmbBatchType.Size = new System.Drawing.Size(121, 23);
this.cmbBatchType.TabIndex = 6;
this.cmbBatchType.SelectedIndex = 0;
// numBatchWidth
this.numBatchWidth.Location = new System.Drawing.Point(139, 277);
this.numBatchWidth.Maximum = new decimal(new int[] { 500, 0, 0, 0 });
this.numBatchWidth.Minimum = new decimal(new int[] { 50, 0, 0, 0 });
this.numBatchWidth.Name = "numBatchWidth";
this.numBatchWidth.Size = new System.Drawing.Size(65, 23);
this.numBatchWidth.TabIndex = 7;
this.numBatchWidth.Value = new decimal(new int[] { 300, 0, 0, 0 });
// numBatchHeight
this.numBatchHeight.Location = new System.Drawing.Point(210, 277);
this.numBatchHeight.Maximum = new decimal(new int[] { 300, 0, 0, 0 });
this.numBatchHeight.Minimum = new decimal(new int[] { 50, 0, 0, 0 });
this.numBatchHeight.Name = "numBatchHeight";
this.numBatchHeight.Size = new System.Drawing.Size(65, 23);
this.numBatchHeight.TabIndex = 8;
this.numBatchHeight.Value = new decimal(new int[] { 150, 0, 0, 0 });
// btnStartBatchPrint
this.btnStartBatchPrint.Location = new System.Drawing.Point(281, 273);
this.btnStartBatchPrint.Name = "btnStartBatchPrint";
this.btnStartBatchPrint.Size = new System.Drawing.Size(100, 30);
this.btnStartBatchPrint.TabIndex = 9;
this.btnStartBatchPrint.Text = "开始批量打印";
this.btnStartBatchPrint.UseVisualStyleBackColor = true;
this.btnStartBatchPrint.Click += new System.EventHandler(this.btnStartBatchPrint_Click);
// label1
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(139, 260);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(44, 15);
this.label1.TabIndex = 10;
this.label1.Text = "宽度:";
// label2
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(210, 260);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(44, 15);
this.label2.TabIndex = 11;
this.label2.Text = "高度:";
// label3
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 260);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 15);
this.label3.TabIndex = 12;
this.label3.Text = "条码类型:";
// BatchPrintForm
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(492, 320);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnStartBatchPrint);
this.Controls.Add(this.numBatchHeight);
this.Controls.Add(this.numBatchWidth);
this.Controls.Add(this.cmbBatchType);
this.Controls.Add(this.lblCount);
this.Controls.Add(this.listBox);
this.Controls.Add(this.btnImportFromFile);
this.Controls.Add(this.btnRemoveData);
this.Controls.Add(this.btnAddData);
this.Controls.Add(this.txtNewData);
this.Name = "BatchPrintForm";
this.Text = "批量打印";
((System.ComponentModel.ISupportInitialize)(this.numBatchWidth)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numBatchHeight)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace BarcodePrinter
{
public partial class LabelDesignForm : Form
{
private LabelDesigner designer;
public LabelDesignForm(LabelDesigner labelDesigner)
{
InitializeComponent();
designer = labelDesigner;
}
private void btnAddBarcode_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtBarcodeData.Text))
{
designer.AddBarcode(txtBarcodeData.Text, cmbBarcodeType.SelectedItem.ToString(),
new Point((int)numX.Value, (int)numY.Value),
new Size((int)numWidth.Value, (int)numHeight.Value));
UpdatePreview();
}
}
private void btnAddText_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtText.Text))
{
designer.AddText(txtText.Text,
new Point((int)numTextX.Value, (int)numTextY.Value),
new Font("Arial", (float)numFontSize.Value),
Color.Black);
UpdatePreview();
}
}
private void UpdatePreview()
{
pictureBoxPreview.Image = designer.GenerateLabel();
}
#region Windows Form Designer generated code
private System.ComponentModel.IContainer components = null;
private PictureBox pictureBoxPreview;
private TextBox txtBarcodeData;
private ComboBox cmbBarcodeType;
private NumericUpDown numX;
private NumericUpDown numY;
private NumericUpDown numWidth;
private NumericUpDown numHeight;
private Button btnAddBarcode;
private TextBox txtText;
private NumericUpDown numTextX;
private NumericUpDown numTextY;
private NumericUpDown numFontSize;
private Button btnAddText;
private Label label1;
private Label label2;
private Label label3;
private Label label4;
private Label label5;
private Label label6;
private Label label7;
private Label label8;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pictureBoxPreview = new System.Windows.Forms.PictureBox();
this.txtBarcodeData = new System.Windows.Forms.TextBox();
this.cmbBarcodeType = new System.Windows.Forms.ComboBox();
this.numX = new System.Windows.Forms.NumericUpDown();
this.numY = new System.Windows.Forms.NumericUpDown();
this.numWidth = new System.Windows.Forms.NumericUpDown();
this.numHeight = new System.Windows.Forms.NumericUpDown();
this.btnAddBarcode = new System.Windows.Forms.Button();
this.txtText = new System.Windows.Forms.TextBox();
this.numTextX = new System.Windows.Forms.NumericUpDown();
this.numTextY = new System.Windows.Forms.NumericUpDown();
this.numFontSize = new System.Windows.Forms.NumericUpDown();
this.btnAddText = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxPreview)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numX)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numY)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numWidth)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numHeight)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numTextX)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numTextY)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numFontSize)).BeginInit();
this.SuspendLayout();
// pictureBoxPreview
this.pictureBoxPreview.BackColor = System.Drawing.Color.White;
this.pictureBoxPreview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBoxPreview.Location = new System.Drawing.Point(12, 12);
this.pictureBoxPreview.Name = "pictureBoxPreview";
this.pictureBoxPreview.Size = new System.Drawing.Size(400, 300);
this.pictureBoxPreview.TabIndex = 0;
this.pictureBoxPreview.TabStop = false;
// txtBarcodeData
this.txtBarcodeData.Location = new System.Drawing.Point(418, 12);
this.txtBarcodeData.Name = "txtBarcodeData";
this.txtBarcodeData.Size = new System.Drawing.Size(150, 23);
this.txtBarcodeData.TabIndex = 1;
this.txtBarcodeData.Text = "123456789012";
// cmbBarcodeType
this.cmbBarcodeType.FormattingEnabled = true;
this.cmbBarcodeType.Items.AddRange(BarcodeTypes.GetSupportedTypes());
this.cmbBarcodeType.Location = new System.Drawing.Point(418, 41);
this.cmbBarcodeType.Name = "cmbBarcodeType";
this.cmbBarcodeType.Size = new System.Drawing.Size(150, 23);
this.cmbBarcodeType.TabIndex = 2;
this.cmbBarcodeType.SelectedIndex = 0;
// numX
this.numX.Location = new System.Drawing.Point(418, 70);
this.numX.Maximum = new decimal(new int[] { 400, 0, 0, 0 });
this.numX.Name = "numX";
this.numX.Size = new System.Drawing.Size(65, 23);
this.numX.TabIndex = 3;
this.numX.Value = new decimal(new int[] { 50, 0, 0, 0 });
// numY
this.numY.Location = new System.Drawing.Point(489, 70);
this.numY.Maximum = new decimal(new int[] { 300, 0, 0, 0 });
this.numY.Name = "numY";
this.numY.Size = new System.Drawing.Size(65, 23);
this.numY.TabIndex = 4;
this.numY.Value = new decimal(new int[] { 50, 0, 0, 0 });
// numWidth
this.numWidth.Location = new System.Drawing.Point(418, 99);
this.numWidth.Maximum = new decimal(new int[] { 400, 0, 0, 0 });
this.numWidth.Name = "numWidth";
this.numWidth.Size = new System.Drawing.Size(65, 23);
this.numWidth.TabIndex = 5;
this.numWidth.Value = new decimal(new int[] { 300, 0, 0, 0 });
// numHeight
this.numHeight.Location = new System.Drawing.Point(489, 99);
this.numHeight.Maximum = new decimal(new int[] { 300, 0, 0, 0 });
this.numHeight.Name = "numHeight";
this.numHeight.Size = new System.Drawing.Size(65, 23);
this.numHeight.TabIndex = 6;
this.numHeight.Value = new decimal(new int[] { 150, 0, 0, 0 });
// btnAddBarcode
this.btnAddBarcode.Location = new System.Drawing.Point(418, 128);
this.btnAddBarcode.Name = "btnAddBarcode";
this.btnAddBarcode.Size = new System.Drawing.Size(100, 30);
this.btnAddBarcode.TabIndex = 7;
this.btnAddBarcode.Text = "添加条码";
this.btnAddBarcode.UseVisualStyleBackColor = true;
this.btnAddBarcode.Click += new System.EventHandler(this.btnAddBarcode_Click);
// txtText
this.txtText.Location = new System.Drawing.Point(418, 174);
this.txtText.Name = "txtText";
this.txtText.Size = new System.Drawing.Size(150, 23);
this.txtText.TabIndex = 8;
this.txtText.Text = "产品名称";
// numTextX
this.numTextX.Location = new System.Drawing.Point(418, 203);
this.numTextX.Maximum = new decimal(new int[] { 400, 0, 0, 0 });
this.numTextX.Name = "numTextX";
this.numTextX.Size = new System.Drawing.Size(65, 23);
this.numTextX.TabIndex = 9;
this.numTextX.Value = new decimal(new int[] { 50, 0, 0, 0 });
// numTextY
this.numTextY.Location = new System.Drawing.Point(489, 203);
this.numTextY.Maximum = new decimal(new int[] { 300, 0, 0, 0 });
this.numTextY.Name = "numTextY";
this.numTextY.Size = new System.Drawing.Size(65, 23);
this.numTextY.TabIndex = 10;
this.numTextY.Value = new decimal(new int[] { 200, 0, 0, 0 });
// numFontSize
this.numFontSize.Location = new System.Drawing.Point(418, 232);
this.numFontSize.Maximum = new decimal(new int[] { 48, 0, 0, 0 });
this.numFontSize.Minimum = new decimal(new int[] { 8, 0, 0, 0 });
this.numFontSize.Name = "numFontSize";
this.numFontSize.Size = new System.Drawing.Size(65, 23);
this.numFontSize.TabIndex = 11;
this.numFontSize.Value = new decimal(new int[] { 12, 0, 0, 0 });
// btnAddText
this.btnAddText.Location = new System.Drawing.Point(418, 261);
this.btnAddText.Name = "btnAddText";
this.btnAddText.Size = new System.Drawing.Size(100, 30);
this.btnAddText.TabIndex = 12;
this.btnAddText.Text = "添加文字";
this.btnAddText.UseVisualStyleBackColor = true;
this.btnAddText.Click += new System.EventHandler(this.btnAddText_Click);
// label1
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(415, 96);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(44, 15);
this.label1.TabIndex = 13;
this.label1.Text = "宽度:";
// label2
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(486, 96);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(44, 15);
this.label2.TabIndex = 14;
this.label2.Text = "高度:";
// label3
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(415, 67);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(44, 15);
this.label3.TabIndex = 15;
this.label3.Text = "X坐标:";
// label4
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(486, 67);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(44, 15);
this.label4.TabIndex = 16;
this.label4.Text = "Y坐标:";
// label5
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(415, 200);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(44, 15);
this.label5.TabIndex = 17;
this.label5.Text = "X坐标:";
// label6
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(486, 200);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(44, 15);
this.label6.TabIndex = 18;
this.label6.Text = "Y坐标:";
// label7
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(415, 229);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(56, 15);
this.label7.TabIndex = 19;
this.label7.Text = "字体大小:";
// label8
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(415, 38);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(56, 15);
this.label8.TabIndex = 20;
this.label8.Text = "条码类型:";
// LabelDesignForm
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(580, 324);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnAddText);
this.Controls.Add(this.numFontSize);
this.Controls.Add(this.numTextY);
this.Controls.Add(this.numTextX);
this.Controls.Add(this.txtText);
this.Controls.Add(this.btnAddBarcode);
this.Controls.Add(this.numHeight);
this.Controls.Add(this.numWidth);
this.Controls.Add(this.numY);
this.Controls.Add(this.numX);
this.Controls.Add(this.cmbBarcodeType);
this.Controls.Add(this.txtBarcodeData);
this.Controls.Add(this.pictureBoxPreview);
this.Name = "LabelDesignForm";
this.Text = "标签设计";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxPreview)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numX)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numY)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numWidth)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numHeight)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numTextX)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numTextY)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numFontSize)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- 默认打印机名称,为空则使用系统默认 -->
<add key="DefaultPrinter" value="" />
<!-- 默认条码类型 -->
<add key="DefaultBarcodeType" value="Code128" />
<!-- 默认条码尺寸 -->
<add key="DefaultWidth" value="300" />
<add key="DefaultHeight" value="150" />
<!-- 默认打印份数 -->
<add key="DefaultCopies" value="1" />
<!-- 标签边距 -->
<add key="LabelMargin" value="10" />
<!-- 是否显示条码文字 -->
<add key="ShowBarcodeText" value="true" />
<!-- 日志文件路径 -->
<add key="LogFilePath" value="logs/barcode.log" />
</appSettings>
</configuration>
在项目文件中添加以下 NuGet 包引用:
<PackageReference Include="ZXing.Net" Version="0.16.9" />
以上就是基于C#实现条形码打印程序的详细内容,更多关于C#条形码打印的资料请关注本站其它相关文章!
您可能感兴趣的文章: