博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
阅读量:6551 次
发布时间:2019-06-24

本文共 5470 字,大约阅读时间需要 18 分钟。

原文:http://blog.csdn.net/kongwei521/article/details/17588825

 

首先效果:

一、下载BarcodeLib.dll

下载地址 :http://download.csdn.net/detail/lllooollpp/7961715

 源码:https://github.com/hjgode/barcodeLib

1.BarcodeLib.dll 一维条码库支持以下条码格式

UPC-A

UPC-E

UPC 2 Digit Ext.

UPC 5 Digit Ext.

EAN-13

JAN-13

EAN-8

ITF-14

Codabar

PostNet

Bookland/ISBN

Code 11

Code 39

Code 39 Extended

Code 93

LOGMARS

MSI

Interleaved 2 of 5

Standard 2 of 5

Code 128

Code 128-A

Code 128-B

Code 128-C

Telepen

然后项目中添加引用

 
private void button6_Click(object sender, EventArgs e)        {            System.Drawing.Image image;            int width = 148, height = 55;            string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "BarcodePattern.jpg";            if (File.Exists(fileSavePath))                File.Delete(fileSavePath);            GetBarcode(height, width, BarcodeLib.TYPE.CODE128, "20131025-136", out image, fileSavePath);            pictureBox1.Image  = Image.FromFile("BarcodePattern.jpg");        }        public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl)        {            try            {                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.LEFT;                b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置                b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式                System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置                b.LabelFont = font;                b.Height = height;//图片高度设置(px单位)                b.Width = width;//图片宽度设置(px单位)                image = b.Encode(type, code);//生成图片                image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);                             }            catch (Exception ex)            {                image = null;            }        }

 

简单的写一下。详细的去 http://www.barcodelib.com/net_barcode/main.html 这里看。

 

二、利用 zxing.dll生成条形码和二维码 

下载地址http://zxingnet.codeplex.com/

ZXing (ZebraCrossing)是一个开源的,支持多种格式的条形码图像处理库, 。使用该类库可以方便地实现二维码图像的生成和解析。 

下载zxing.dll 项目参照引用

 
{                MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();                ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);                Bitmap img = bm.ToBitmap();                pictureBox1.Image = img;                //自动保存图片到当前目录                string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";                img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);                lbshow.Text = "图片已保存到:" + filename;            }            catch (Exception ee)            { MessageBox.Show(ee.Message); }

 

 

 

三、 QrCodeNet.dll生成条形码和二维码

  下载地址http://qrcodenet.codeplex.com/

下载QrCodeNet.dll 项目参照引用

 

private void button2_Click(object sender, EventArgs e)        {            var codeParams = CodeDescriptor.Init(ErrorCorrectionLevel.H, textBox1.Text.Trim(), QuietZoneModules.Two, 5);            codeParams.TryEncode();            // Render the QR code as an image            using (var ms = new MemoryStream())            {                codeParams.Render(ms);                Image image = Image.FromStream(ms);                pictureBox1.Image = image;                if (image != null)                    pictureBox1.SizeMode = image.Height > pictureBox1.Height ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.Normal;            }        }///  /// Class containing the description of the QR code and wrapping encoding and rendering. ///  internal class CodeDescriptor { public ErrorCorrectionLevel Ecl; public string Content; public QuietZoneModules QuietZones; public int ModuleSize; public BitMatrix Matrix; public string ContentType; ///  /// Parse QueryString that define the QR code properties ///  /// HttpRequest containing HTTP GET data /// 
A QR code descriptor object
public static CodeDescriptor Init(ErrorCorrectionLevel level, string content, QuietZoneModules qzModules, int moduleSize) { var cp = new CodeDescriptor(); //// Error correction level cp.Ecl = level; //// Code content to encode cp.Content = content; //// Size of the quiet zone cp.QuietZones = qzModules; //// Module size cp.ModuleSize = moduleSize; return cp; } /// /// Encode the content with desired parameters and save the generated Matrix /// ///
True if the encoding succeeded, false if the content is empty or too large to fit in a QR code
public bool TryEncode() { var encoder = new QrEncoder(Ecl); QrCode qr; if (!encoder.TryEncode(Content, out qr)) return false; Matrix = qr.Matrix; return true; } /// /// Render the Matrix as a PNG image /// /// MemoryStream to store the image bytes into internal void Render(MemoryStream ms) { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(Matrix, System.Drawing.Imaging.ImageFormat.Png, ms); ContentType = "image/png"; } }

 

效果:

参考地址:

http://www.cnblogs.com/mzlee/archive/2011/03/19/Lee_Barcode.html

http://blog.163.com/smxp_2006/blog/static/588682542010215163803/

http://q.cnblogs.com/q/15253/

http://www.csharpwin.com/csharpspace/13364r9803.shtml

http://www.2cto.com/kf/201304/203035.html

你可能感兴趣的文章
抵制克苏恩[Lydsy2017年4月月赛]
查看>>
MySql Study Notes
查看>>
6 - laravel 基础 - 视图与模板引擎
查看>>
团队第二次作业
查看>>
linux 查询当前文件夹下的目录数量
查看>>
【python】入门第一篇
查看>>
1682: [Usaco2005 Mar]Out of Hay 干草危机
查看>>
supersr--NSURLConnection iOS2.0苹果原生请求
查看>>
iphone-common-codes-ccteam源代码 CCPlistFileReader.h
查看>>
构造方法
查看>>
SQL效率之索引
查看>>
线性支持向量分类机及其实现
查看>>
Axure产品原型设计工具
查看>>
ASM文件系统
查看>>
ajax学习笔记(原生js的ajax)
查看>>
mysql 函数 事务
查看>>
1312 连续自然数和
查看>>
进程/线程介绍
查看>>
SPSS-Friedman 秩和检验-非参数检验-K个相关样本检验 案例解析
查看>>
java UDP server
查看>>