C# 通過 BarcodeLib 生成條形碼

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

    }
}

生成條形碼:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章