C# 利用BarcodeLib.dll生成條形碼(一維,zxing,QrCodeNet/dll二維碼)


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM