C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)

简介: 原文:http://blog.csdn.net/kongwei521/article/details/17588825   首先效果: 一、下载BarcodeLib.dll 下载地址 :http://download.

原文: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;
            }

        }
/// <summary>
 /// Class containing the description of the QR code and wrapping encoding and rendering.
 /// </summary>
 internal class CodeDescriptor
 {
 public ErrorCorrectionLevel Ecl;
 public string Content;
 public QuietZoneModules QuietZones;
 public int ModuleSize;
 public BitMatrix Matrix;
 public string ContentType;

 /// <summary>
 /// Parse QueryString that define the QR code properties
 /// </summary>
 /// <param name="request">HttpRequest containing HTTP GET data</param>
 /// <returns>A QR code descriptor object</returns>
 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;
 }

 /// <summary>
 /// Encode the content with desired parameters and save the generated Matrix
 /// </summary>
 /// <returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>
 public bool TryEncode()
 {
 var encoder = new QrEncoder(Ecl);
 QrCode qr;
 if (!encoder.TryEncode(Content, out qr))
 return false;

 Matrix = qr.Matrix;
 return true;
 }

 /// <summary>
 /// Render the Matrix as a PNG image
 /// </summary>
 /// <param name="ms">MemoryStream to store the image bytes into</param>
 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

相关文章
|
1月前
|
开发框架 小程序 .NET
C#动态生成带参数的小程序二维码
C#动态生成带参数的小程序二维码
|
程序员 C# 图形学
C# 生成二维码,彩色二维码,带有Logo的二维码及普通条形码
每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默。我眼中的程序员大多都不爱说话,默默承受着编程的巨大压力,除了技术上的交流外,他们不愿意也不擅长和别人交流,更不乐意任何人走进他们的内心!    废话少说,咱直接进入正题:    目前来说,比较流行的二维码生成方式有两种:一种是:QrCode.Net和谷歌的Zxing.Net,我个人比较倾向于使用ZXing.Net,今天本篇博客主要讲解Zxing.Net的使用。
1887 0
|
C#
C# 实现生成带二维码的专属微信公众号推广海报
原文:C# 实现生成带二维码的专属微信公众号推广海报 很多微信公众号中需要生成推广海报的功能,粉丝获得专属海报后可以分享到朋友圈或发给朋友,为公众号代言邀请好友即可获取奖励的。海报自带渠道二维码,粉丝长按二维码即可关注微信公众号,从而达到吸粉的目的。
2321 0
|
C#
C#彩色艺术化二维码样式设计(仅说思路)
原文:C#彩色艺术化二维码样式设计(仅说思路) 仅讲思路,想要源码的请绕道。   一、样式 1、先看各种二维码的样式吧: (1)最简单的样式——黑白样式,如下图: 图1  最平常见到的二维码样式(如果你用智能手机,且已安装二维码扫描软件,扫描之后你会发现它就是一个网址:http://www.lgms.net) (2)以下为在基本样式的基础上进行的改进: 图2  圆形点状样式(为了保持可识别性,三个定位方块图没有圆形点状化)。
1436 0
|
Shell C# 图形学
C#生成带logo的二维码
原文:C#生成带logo的二维码 带logo的二维码生成分为两步骤:首先根据输入的内容生成二维码图片,然后读取本地的logo图片,通过图片处理生成带logo的二维码。 生成的二维码效果如下: 下面直接贴出二维码生成类   QRCodeHelper.
1174 0
C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码
本文介绍在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码。网上文章大多只是简单介绍内置参数的设置,根据我的使用目的,增加了自定义目标二维码图片尺寸和白边边框。
1433 0
|
C#
C# QRCode 二维码
/*********************************************************************** * C# QRCode 二维码 * 说明: * 本文记录如何在C#中调用QRCode库生成二维码并在PictureBox中显示。
967 0