日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#生成二维码和条形码并实现打印的功能

發布時間:2024/1/8 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#生成二维码和条形码并实现打印的功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載:https://www.cnblogs.com/xixim/p/4589078.html

下載地址:http://pan.baidu.com/s/1kTr3Vuf

Step1:使用VS2010新建一個窗體程序項目:

Step2:添加三個類:分別是BarCodeClass.cs、DocementBase.cs、imageDocument.cs。(下一步貼出這些類的代碼);;;;添加下載回來的引用zxing.dll。

》說明:

《1》?? BarCodeClass.cs主要用來實現條形碼和二維碼的生成和解析。

《2》?? DocementBase.cs、imageDocument.cs這兩個類是用來實現對生成的條形碼和二維碼進行打印。

Step3:編寫上一步的三個類的代碼:

》BarCodeClass.cs

????using?System;

using?System.Collections.Generic;

using?System.Linq;

using?System.Text;

using?ZXing.Common;

using?ZXing;

using?System.Windows.Forms;

using?System.Drawing;

using?System.Text.RegularExpressions;

using?ZXing.QrCode;

namespace?BarCode

{

????class?BarCodeClass

??? {

????????///<summary>

?? ?????///生成條形碼

????????///</summary>

????????///<paramname="pictureBox1"></param>

????????///<paramname="Contents"></param>

????????public?void?CreateBarCode(PictureBoxpictureBox1,string?Contents)

??????? {

????????????Regexrg =?new?Regex("^[0-9]{12}$");

? ??????????if(!rg.IsMatch(Contents))

???????????? {

?????????????????MessageBox.Show("本例子采用EAN_13編碼,需要輸入12位數字");

?????????????????return;

???????????? }

?

????????????EncodingOptionsoptions =null;

????????????BarcodeWriterwriter =null;

??????????? options =?newEncodingOptions

??????????? {

??????????????? Width = pictureBox1.Width,

??????????????? Height = pictureBox1.Height

??????????? };

??????????? writer =?newBarcodeWriter();

??????????? writer.Format =?BarcodeFormat.ITF;

??????????? writer.Options = options;

?

????????????Bitmapbitmap = writer.Write(Contents);

??????????? pictureBox1.Image = bitmap;?

??????? }

?

????????///<summary>

????????///生成二維碼

????????///</summary>

????????///<paramname="pictureBox1"></param>

????????///<paramname="Contents"></param>

????????public?void?CreateQuickMark(PictureBoxpictureBox1,string?Contents)

??????? {

????????????if(Contents ==?string.Empty)

?????? ?????{

????????????????MessageBox.Show("輸入內容不能為空!");

????????????????return;

??????????? }

?

????????????EncodingOptionsoptions =null;

????????????BarcodeWriterwriter =null;

?

??????????? options =?newQrCodeEncodingOptions

?????????? {

?????????????? DisableECI =?true,

?????????????? CharacterSet =?"UTF-8",

?????????????? Width = pictureBox1.Width,

?????????????? Height = pictureBox1.Height

?????????? };

??????????? writer =?newBarcodeWriter();

??????????? writer.Format =?BarcodeFormat.QR_CODE;

??????? ????writer.Options = options;

?

????????

????????????Bitmapbitmap = writer.Write(Contents);

??????????? pictureBox1.Image = bitmap;

??????? }

?

????????///<summary>

????????///解碼

????????///</summary>

????????///<paramname="pictureBox1"></param>

????????public?void?Decode(PictureBoxpictureBox1)

??????? {

????????????BarcodeReaderreader =new?BarcodeReader();

????????????Resultresult = reader.Decode((Bitmap)pictureBox1.Image);

??????? }

??? }

}

?

》DocementBase.cs

????using?System;

using?System.Collections.Generic;

using?System.Linq;

using?System.Text;

using?System.Drawing.Printing;

using?System.Drawing;

using?System.Windows.Forms;

?

namespace?BarCode

{

????class?DocementBase?:?PrintDocument

??? {

????????//fields

????????public?Font?Font =?new?Font("Verdana",10,?GraphicsUnit.Point);

?

????????//預覽打印

????????public?DialogResult?showPrintPreviewDialog()

??????? {

????????????PrintPreviewDialogdialog =new?PrintPreviewDialog();

??? ????????dialog.Document =?this;

?

????????????returndialog.ShowDialog();

??????? }

?

????????//先設置后打印

????????public?DialogResult?ShowPageSettingsDialog()

??????? {

????????????PageSetupDialogdialog =new?PageSetupDialog();

??????????? dialog.Document =?this;

?

????????????returndialog.ShowDialog();

??????? }

??? }

}

?

》imageDocument.cs

????using?System;

using?System.Collections.Generic;

using?System.Linq;

using?System.Text;

using?System.Drawing;

using?System.Drawing.Printing;

?

namespace?BarCode

{

????class?imageDocument?:?DocementBase

??? {

????????privateImage?_Image;

?

????????public?Image?Image

??????? {

????????????get

??????????? {

????????????????return_Image;

??????????? }

????????????set

??????????? {

??????????????? _Image =?value;

?

????????????????if(_Image !=?null)

??????????????? {

????????????????????if(_Image.Size.Width > _Image.Size.Height)

???????????????????????DefaultPageSettings.Landscape =?true;

????????????????????else

???????????????????????DefaultPageSettings.Landscape =?false;

????? ??????????}

??????????? }

??????? }

?

????????publicimageDocument()

??????? {

?

??????? }

?

????????publicimageDocument(Image?image)

??????? {

????????????this.Image= image;

??????? }

?

????????protectedoverridevoidOnPrintPage(PrintPageEventArgs?e)

???? ???{

????????????if(Image ==?null)

??????????? {

????????????????thrownewInvalidOperationException();

??????????? }

?

????????????RectanglebestFit = GetBestFitRectangle(e.MarginBounds, Image.Size);

?????????

??????????? e.Graphics.DrawImage(Image, bestFit);

?

??????????? e.Graphics.DrawRectangle(Pens.Black, bestFit);

??????????? e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds);

??????? }

?

???????//?保持高度比:參數為(打印邊界的Rectangularle對象,圖像大小的Size對象)

????????protectedRectangle?GetBestFitRectangle(Rectangle?toContain,SizeobjectSize)

??????? {

????????????//檢查頁面是水平還是豎直的。

????????????boolcontainerLandscape =false;

????????????if(toContain.Width > toContain.Height)

??????????????? containerLandscape =?true;

?

????????????//高度比=圖像的高/圖像的寬

????????????floataspectRatio = (float)objectSize.Height / (float)objectSize.Width;

????????????//得到頁面左上角的坐標

????????????intmidContainerX = toContain.Left + (toContain.Width / 2);

????????????intmidContainerY = toContain.Top + (toContain.Height / 2);

?

????????????intx1 = 0, x2 = 0, y1 = 0, y2 = 0;

????????????if(containerLandscape ==false)

??????????? {

????????????????//豎直圖像

??????????????? x1 = toContain.Left;

??????????????? x2 = toContain.Right;

????????????????//調整之后的height

????????????????intadjustedHeight = (int)((float)toContain.Width * aspectRatio);

?

??????????????? y1 = midContainerY -(adjustedHeight / 2);

??????????????? y2 = y1 + adjustedHeight;

??????????? }

????????????else

??????????? {

??????????????? y1 = toContain.Top;

??????????????? y2 = toContain.Bottom;

????????????????//調整之后的height

????????????????intadjustedWidth = (int)((float)toContain.Height/ aspectRatio);

?

??????????????? x1 = midContainerX -(adjustedWidth / 2);

??????????????? x2 = x1 + adjustedWidth;

??????????? }

????????????returnnewRectangle(x1,y1, x2 - x1, y2 - y1);

??????? }

??? }

}

?

Step4:修改界面。

Step5:依次雙擊【生成條形碼】、【生成二維碼】、【解碼】、【打印】等按鈕,進入Click事件,編寫后臺代碼。這里不再一一講述如何實現。代碼參照下一步:

?

Step6:貼出窗體的全部代碼。

using?System;

using?System.Collections.Generic;

using?System.ComponentModel;

using?System.Data;

using?System.Drawing;

using?System.Linq;

using?System.Text;

using?System.Windows.Forms;

using?System.Text.RegularExpressions;

using?ZXing;

using?ZXing.QrCode.Internal;

using?ZXing.Common;

using?System.IO;

using?ZXing.QrCode;

?

namespace?BarCode

{

????public?partial?class?Main?:?Form

??? {

????????publicMain()

??????? {

??????????? InitializeComponent();?

??????? }

?

???????private?BarCodeClass?bcc =?newBarCodeClass();

???????private?DocementBase?_docement;

?

????????//生成條形碼

????????privatevoid?button1_Click(objectsender,EventArgs?e)

??????? {

?

??????????? bcc.CreateBarCode(pictureBox1,txtMsg.Text);

??????????

??????? }

????????//生成二維碼

????????privatevoid?button2_Click(objectsender,EventArgs?e)

??????? {

??????????? bcc.CreateQuickMark(pictureBox1, txtMsg.Text);

??????? }

?

????????privatevoid?Form1_Load(objectsender,EventArgs?e)

??????? {

??????????? txtMsg.Text = System.DateTime.Now.ToString("yyyyMMddhhmmss").Substring(0,12);

??????? }

????????//解碼

????????privatevoid?button4_Click(objectsender,EventArgs?e)

??????? {

????????????if(pictureBox1.Image ==null)

??????????? {

????????????????MessageBox.Show("請錄入圖像后再進行解碼!");

????????????????return;

??????????? }

????????????BarcodeReaderreader =new?BarcodeReader();?

????????????Resultresult = reader.Decode((Bitmap)pictureBox1.Image);

????????????MessageBox.Show(result.Text);

??????? }

?

????????//打印

????????privatevoid?button3_Click(objectsender,EventArgs?e)

??????? {

??????????

????????????if(pictureBox1.Image ==null)

??????????? {

?????? ?????????MessageBox.Show("You Must Load an Image first!");

????????????????return;

??????????? }

????????????else

??????????? {

??????????????? _docement=new?imageDocument(pictureBox1.Image);

??????????? }

????????? _docement.showPrintPreviewDialog();

?? ?????}

??? }

?

}

?

Step7:剩下的就是演示了:本機演示結果如下:

》運行程序:點擊【生成條形碼】,結果如下:

》點擊【解碼】按鈕,結果如下:

》點擊《打印》按鈕,結果如下:

》點擊【生成二維碼】按鈕,結果如下:

》點擊【解碼】按鈕,結果如下:


》點擊【打印】按鈕,結果如下:

總結

以上是生活随笔為你收集整理的C#生成二维码和条形码并实现打印的功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。