当前位置:主页 > 软件编程 > .NET代码 >

C# 如何生成 DataMatrix 格式的二维码

时间:2022-05-23 09:18:29 | 栏目:.NET代码 | 点击:

该文主要是利用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、实践部分:

创建如下界面:按钮按下,生产条码。

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

事实上:生成条码的方法有很多种,库也有很多,大家可以多去琢磨琢磨,不能局限一种,就我所知所用过的就有五个库。

网上也有很多对条码底层的开源研究,可自行。

您可能感兴趣的文章:

相关文章