C# 生成 DataMatrix 格式的二維碼

該文主要是利用OnBarcode.dll 生成 DataMatrix 格式的二維碼的一些簡單方法和操做技巧。關於QrBarcode的二維碼比較常見和簡單,網上有不少資源。函數

一、附件爲dll測試

二、利用上述控件生成二維碼的核心代碼:this

    (a)C# 代碼:編碼

   DataMatrix datamatrix = new DataMatrix();
datamatrix.Data = "0123456789";

// Create Data Matrix and encode barcode to Jpeg format
datamatrix.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
datamatrix.drawBarcode("C://csharp-datamatrix.jpg");

   (b) VB.NET 代碼:spa

 

   Dim datamatrix As OnBarcode.Barcode.DataMatrix
datamatrix = New OnBarcode.Barcode.DataMatrix()
datamatrix.Data = "0123456789"

' Create Data Matrix and encode barcode to Jpeg format
datamatrix.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
datamatrix.drawBarcode("C://vbnet-datamatrix.jpg")

(c)其餘函數接口(分別是C#和VB):code

public void drawBarcode(Graphics graphics);

public void drawBarcode(string filename);

public Bitmap drawBarcode();

public void drawBarcode(Stream fileStream);
   Public Sub drawBarcode(ByRef graphics As Graphics)

Public Sub drawBarcode(ByVal filename As String)

Public Function drawBarcode() As Bitmap

Public Sub drawBarcode(ByRef fileStream As Stream)

 三、實踐部分:orm

    建立以下界面:按鈕按下,生產條碼。blog

using System;接口

using System.Collections.Generic;圖片

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

using OnBarcode.Barcode;

using System.Drawing.Imaging;

 

namespace DataMatrix1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            DataMatrix datamatrix = new DataMatrix();

            // Barcode data to encode

            datamatrix.Data = "OnBarcode";

            // Data Matrix data mode

            datamatrix.DataMode = DataMatrixDataMode.ASCII;

            // Data Matrix format mode

            datamatrix.FormatMode = DataMatrixFormatMode.Format_10X10;

            /*

            * Barcode Image Related Settings

            */

            // Unit of meature for all size related setting in the library. 

            datamatrix.UOM = UnitOfMeasure.PIXEL;

            // Bar module size (X), default is 3 pixel;

            datamatrix.X = 3;

            // Barcode image left, right, top, bottom margins. Defaults are 0.

            datamatrix.LeftMargin = 0;

            datamatrix.RightMargin = 0;

            datamatrix.TopMargin = 0;

            datamatrix.BottomMargin = 0;

            // Image resolution in dpi, default is 72 dpi.

            datamatrix.Resolution = 72;

            // Created barcode orientation. 

            // Rotate0 = 0,

            // Rotate90 = 1,

            // Rotate180 = 2,

            // Rotate270 = 3,

            // 4 options are: facing left, facing right, facing bottom, and facing top

            datamatrix.Rotate = Rotate.Rotate0;

            // Geneat data matrix and encode barcode to gif format

            datamatrix.ImageFormat = System.Drawing.Imaging.ImageFormat.Bmp;

            datamatrix.drawBarcode("C:\\datamatrix.jpg");   //以保存特定格式方法生產二維碼

            //You can also call other drawing methods to generate barcodes

            //public void drawBarcode(Graphics graphics);

            //public void drawBarcode(string filename);

            //public Bitmap drawBarcode();

            //public void drawBarcode(Stream stream);       //將該種編碼的格式,寫入文件流之中

            this.pictureBox1.Image = datamatrix.drawBarcode();  //調用其中一個接口,將圖片以bitmap形式顯示出來

        }

    }

}

測試結果:

 

當初只是隨便分享一下,沒想到你們使用條碼的這麼多,評論也有不少,謝謝你們支持。

這裏附上幾個條碼經常使用的dll。

可在這裏下載:https://i.cnblogs.com/Files.aspx

事實上:生成條碼的方法有不少種,庫也有不少,你們能夠多去琢磨琢磨,不能侷限一種,就我所知所用過的就有五個庫。

網上也有不少對條碼底層的開源研究,可自行。

 

分享共進步,謝謝閱讀!

相關文章
相關標籤/搜索