C# 如何生成 DataMatrix 格式的二維碼
該文主要是利用OnBarcode.dll 生成DataMatrix 格式的二維碼的一些簡單方法和操作技巧。關于QrBarcode的二維碼比較常見和簡單,網上有很多資源。
1、附件為dll
2、利用上述控件生成二維碼的核心代碼:
(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代碼:
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):
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)
3、實踐部分:
創(chuàng)建如下界面:按鈕按下,生產條碼。
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
事實上:生成條碼的方法有很多種,庫也有很多,大家可以多去琢磨琢磨,不能局限一種,就我所知所用過的就有五個庫。
網上也有很多對條碼底層的開源研究,可自行。
到此這篇關于C# 如何生成 DataMatrix 格式的二維碼的文章就介紹到這了,更多相關C# DataMatrix 格式二維碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!