.NET 一维、二维码生成DEMO

发布时间:2017-09-10 10:58:44
.NET 一维、二维码生成DEMO

条码技术越来越流行,已经不再局限于物流、超市等专业行业了,很多网站都已经加上了二维码,站群软件,作为代码民工们,站群系统,怎么能不懂得用这门技术呢。

在网上找了一些资料,一维码生成的源码相对较多,也可用,二维码的也不少,但我发现找来找去都是同一个DEMO,而且跑不动,晕死,后来找到了QrCodeNet的源码才搞掂。

关键代码如下:

一维码生成(调用BarcodeLib):

[csharp]
//生成一维码图片
private byte[] GetBarcode(int height, int width, BarcodeLib.TYPE type,
string code, out System.Drawing.Image image)
{
image = null;
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.BackColor = System.Drawing.Color.White;
b.ForeColor = System.Drawing.Color.Black;
b.IncludeLabel = true;
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);
b.LabelFont = font;

b.Height = height;
b.Width = width;

try
{
image = b.Encode(type, code);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
image = null;
}
//SaveImage(image, Guid.NewGuid().ToString("N") + ".png");
byte[] buffer = b.GetImageData(BarcodeLib.SaveTypes.GIF);
return buffer;
}

//调用
private void BuildBarcode()
{
string number = string.IsNullOrEmpty(textBox1.Text.Trim()) ? "Avx-(13614)-vR" : textBox1.Text.Trim();
BarcodeLib.TYPE codeType = comboBox1.SelectedIndex == -1 ? BarcodeLib.TYPE.CODE128 : (BarcodeLib.TYPE)Enum.Parse(typeof(BarcodeLib.TYPE), comboBox1.Text);
System.Drawing.Image image;
int width = 250, height = 100;
byte[] buffer = GetBarcode(height, width,
codeType, number, out image);

pictureBox1.Image = image;
if (image != null)
pictureBox1.SizeMode = image.Height > pictureBox1.Height ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.Normal;
}

//生成一维码图片
private byte[] GetBarcode(int height, int width, BarcodeLib.TYPE type,
string code, out System.Drawing.Image image)
{
image = null;
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.BackColor = System.Drawing.Color.White;
b.ForeColor = System.Drawing.Color.Black;
b.IncludeLabel = true;
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);
b.LabelFont = font;

b.Height = height;
b.Width = width;

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:宜昌SEO http://yichang.4567w.com