using System;
using System.Drawing;
using System.Windows.Forms;
using BarcodeLib;
namespace BarCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void draw_Click(object sender, EventArgs e)
{
const int width = 250;
const int height = 100;
TYPE type;
switch (cmbType.Text)
{
case "Code39":
type = TYPE.CODE39; break;
default :
type = TYPE.CODE128; break;
}
var code = txtCode.Text;
Image image;
GetBarcode(width, height, type, code, out image);
pictureBar.Width = width;
pictureBar.Height = height;
pictureBar.Image = image;
}
#region 生成条形码
/// <summary>
/// 生成条形码
/// </summary>
static byte[] GetBarcode(int width, int height, TYPE type, string code, out Image image)
{
Barcode b = new Barcode
{
BackColor = Color.White, //图片背景颜色
ForeColor = Color.Black, //条码颜色
IncludeLabel = true,
Alignment = AlignmentPositions.CENTER,
LabelPosition = LabelPositions.BOTTOMCENTER,
ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg, //图片格式
LabelFont = new Font("verdana", 10f), //字体设置
Height = height, //图片高度
Width = width //图片宽度
};
image = b.Encode(type, code);//生成图片
byte[] buffer = b.GetImageData(SaveTypes.GIF);//转换byte格式
return buffer;
}
#endregion
}
}
生成条形码: