一、通过NuGet安装插件包
可以直接通过VS的NuGet管理器下载安装,或者在网站https://www.nuget.org上搜索并下载插件安装包进行安装。

二、生成条形码的核心代码
using System;
using System.Windows.Forms;
using BarcodeLib;
namespace ImageCode {
public class ImageCode {
//生成条形码,参数为生成条形码的字符,返回Image对象的字节数组
public byte[] GetBarcode(string code) {
Barcode b = new Barcode();
b.Width = 200; b.Height = 50;
b.AlternateLabel = "可以自定义"; // 条码图像中显示文本内容,不设置的话默认为条码值,必须配合IncludeLabel=true使用
b.IncludeLabel = true;
b.Encode(TYPE.CODE128, code); // 条码类型可以百度
//如果要返回Image对象,使用以下语句
//return b.EncodedImage;
return b.Encoded_Image_Bytes;
/*
Barcode对象常用属性:
AlternateLabel:条码图像中显示的文本
Raw_Data:待生成条形码图像的原始字符串值
Encoded_Type:编码类型
Alignment:条形码在图片中的位置
RotateFlipType:条形码在图片中旋转或翻转的角度
Width和Height:条码图像的宽度和高度
ForeColor和BackColor:条码图像的前景色和背景色
BarWidth:单个线条的宽度
AspectRatio:长宽比
IncludeLabel:是否在条形码图像中显示文本
LabelFont和LabelPosition:文本的字体和显示位置
EncodedImage:返回条形码的Image对象
Encoded_Image_Bytes:返回条形码Image对象的字节数组
*/
}
//在WinForm窗体的PictureBox控件中显示:
private void Form1_Load(object sender,EventArgs e) {
//需要先将GetBarcode函数的返回值改为Image对象
pictureBox1.Image=GetBarcode("Test string");
}
//在MVC项目中显示,控制器中写入如下代码:
public FileResult ShowQRCode() {
return new FileContentResult(GetBarcode("Test string"),"image/jpg");
}
//在View视图中写入如下HTML代码:
<img src="@Url.Action("ShowQRCode")" />
}
}