这篇文章主要讲解了“怎么用C#生成DataMatrix格式的二维码”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用C#生成DataMatrix格式的二维码”吧!
附件为dll
利用上述控件生成二维码的核心代码:
(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)
实践部分:
创建如下界面:按钮按下,生产条码。
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; // 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形式显示出来 } }}
测试结果:
感谢各位的阅读,以上就是“怎么用C#生成DataMatrix格式的二维码”的内容了,经过本文的学习后,相信大家对怎么用C#生成DataMatrix格式的二维码这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!