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
}
}
生成條形碼: