C# 利用BarcodeLib.dll生成条形码

首先效果:

1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391&fid=1692834292 如果不存在了则自行搜索下载。

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

然后项目中添加引用

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private void button6_Click(object sender, EventArgs e)  
  2.     {  
  3.         System.Drawing.Image image;  
  4.         int width = 148, height = 55;  
  5.         string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "BarcodePattern.jpg";  
  6.         if (File.Exists(fileSavePath))  
  7.             File.Delete(fileSavePath);  
  8.         GetBarcode(height, width, BarcodeLib.TYPE.CODE128, "20131025-136", out image, fileSavePath);  
  9.   
  10.         pictureBox1.Image  = Image.FromFile("BarcodePattern.jpg");  
  11.     }  
  12.     public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl)  
  13.     {  
  14.         try  
  15.         {  
  16.             image = null;  
  17.             BarcodeLib.Barcode b = new BarcodeLib.Barcode();  
  18.             b.BackColor = System.Drawing.Color.White;//图片背景颜色  
  19.             b.ForeColor = System.Drawing.Color.Black;//条码颜色  
  20.             b.IncludeLabel = true;  
  21.             b.Alignment = BarcodeLib.AlignmentPositions.LEFT;  
  22.             b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;  
  23.             b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式  
  24.             System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置  
  25.             b.LabelFont = font;  
  26.             b.Height = height;//图片高度设置(px单位)  
  27.             b.Width = width;//图片宽度设置(px单位)  
  28.   
  29.             image = b.Encode(type, code);//生成图片  
  30.             image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);  
  31.                
  32.         }  
  33.         catch (Exception ex)  
  34.         {  
  35.   
  36.             image = null;  
  37.         }  
  38.     }  

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

 

 

 

利用 zxing.dll生成条形码和二维码  下载地址http://zxingnet.codeplex.com/

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

下载zxing.dll 项目参照引用

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. {  
  2.                 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();  
  3.                 ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);  
  4.                 Bitmap img = bm.ToBitmap();  
  5.                 pictureBox1.Image = img;  
  6.   
  7.                 //自动保存图片到当前目录  
  8.                 string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";  
  9.                 img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);  
  10.                 lbshow.Text = "图片已保存到:" + filename;  
  11.             }  
  12.             catch (Exception ee)  
  13.             { MessageBox.Show(ee.Message); }  



 

 

利用 QrCodeNet.dll生成条形码和二维码  下载地址http://qrcodenet.codeplex.com/

下载QrCodeNet.dll 项目参照引用

 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1.   private void button2_Click(object sender, EventArgs e)  
  2.         {  
  3.             var codeParams = CodeDescriptor.Init(ErrorCorrectionLevel.H, textBox1.Text.Trim(), QuietZoneModules.Two, 5);  
  4.   
  5.             codeParams.TryEncode();  
  6.   
  7.             // Render the QR code as an image  
  8.             using (var ms = new MemoryStream())  
  9.             {  
  10.                 codeParams.Render(ms);  
  11.   
  12.                 Image image = Image.FromStream(ms);  
  13.                 pictureBox1.Image = image;  
  14.                 if (image != null)  
  15.                     pictureBox1.SizeMode = image.Height > pictureBox1.Height ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.Normal;  
  16.             }  
  17.   
  18.         }  
  19.  /// <summary>  
  20.     /// Class containing the description of the QR code and wrapping encoding and rendering.  
  21.     /// </summary>  
  22.     internal class CodeDescriptor  
  23.     {  
  24.         public ErrorCorrectionLevel Ecl;  
  25.         public string Content;  
  26.         public QuietZoneModules QuietZones;  
  27.         public int ModuleSize;  
  28.         public BitMatrix Matrix;  
  29.         public string ContentType;  
  30.   
  31.         /// <summary>  
  32.         /// Parse QueryString that define the QR code properties  
  33.         /// </summary>  
  34.         /// <param name="request">HttpRequest containing HTTP GET data</param>  
  35.         /// <returns>A QR code descriptor object</returns>  
  36.         public static CodeDescriptor Init(ErrorCorrectionLevel level, string content, QuietZoneModules qzModules, int moduleSize)  
  37.         {  
  38.             var cp = new CodeDescriptor();  
  39.   
  40.             //// Error correction level  
  41.             cp.Ecl = level;  
  42.             //// Code content to encode  
  43.             cp.Content = content;  
  44.             //// Size of the quiet zone  
  45.             cp.QuietZones = qzModules;  
  46.             //// Module size  
  47.             cp.ModuleSize = moduleSize;  
  48.             return cp;  
  49.         }  
  50.   
  51.         /// <summary>  
  52.         /// Encode the content with desired parameters and save the generated Matrix  
  53.         /// </summary>  
  54.         /// <returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>  
  55.         public bool TryEncode()  
  56.         {  
  57.             var encoder = new QrEncoder(Ecl);  
  58.             QrCode qr;  
  59.             if (!encoder.TryEncode(Content, out qr))  
  60.                 return false;  
  61.   
  62.             Matrix = qr.Matrix;  
  63.             return true;  
  64.         }  
  65.   
  66.         /// <summary>  
  67.         /// Render the Matrix as a PNG image  
  68.         /// </summary>  
  69.         /// <param name="ms">MemoryStream to store the image bytes into</param>  
  70.         internal void Render(MemoryStream ms)  
  71.         {  
  72.             var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));  
  73.             render.WriteToStream(Matrix, System.Drawing.Imaging.ImageFormat.Png, ms);  
  74.             ContentType = "image/png";  
  75.         }  
  76.     }  


效果:

参考地址:

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

posted @ 2014-09-19 17:38  r163  阅读(3279)  评论(1编辑  收藏  举报