private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
System.Drawing.Image image;
int width = 148, height = 55;
string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "BarcodePattern.jpg";
if (File.Exists(fileSavePath))
{
File.Delete(fileSavePath);
}
string value = textBox1.Text;
GetBarcode(height, width, BarcodeLib.TYPE.CODE128, value, out image, fileSavePath);
pictureBox1.Image = Image.FromFile("BarcodePattern.jpg");
}
public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl)
{
try
{
image = null;
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.BackColor = System.Drawing.Color.White;//图片背景颜色
b.ForeColor = System.Drawing.Color.Black;//条码颜色
b.IncludeLabel = true;
b.Alignment = BarcodeLib.AlignmentPositions.LEFT;
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置
b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式
System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置
b.LabelFont = font;
b.Height = height;//图片高度设置(px单位)
b.Width = width;//图片宽度设置(px单位)
image = b.Encode(type, code);//生成图片
image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
image = null;
}
}