C# 通过 BarcodeLib 生成条形码

本文介绍如何使用C#及BarcodeLib库生成不同类型的条形码,并提供了完整的代码示例,包括设置条形码样式、尺寸及显示方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    }
}

生成条形码:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值