Winform中使用zxing和Graphics实现自定义绘制二维码布局
生活随笔
收集整理的這篇文章主要介紹了
Winform中使用zxing和Graphics实现自定义绘制二维码布局
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
場景
zxing.dll下載
https://download.csdn.net/download/badao_liumang_qizhi/11623214
效果
?
實現(xiàn)
根據(jù)上面文章中將簡單的二維碼生成后,現(xiàn)在要調(diào)整其布局。
拖拽一個按鈕,雙擊進(jìn)入其點擊事件。
private void button6_Click(object sender, EventArgs e){//二維碼內(nèi)容對象AssetEntity assetEntity = new AssetEntity() { Name = "霸道",Gender = "男",Url = "123" };//使用上面生成二維碼的方法獲取二維碼的bitmap對象Bitmap bitmap = ZxingHelper.CreateQRCode("霸道");//重新繪制二維碼布局Image img = ZxingHelper.GetPrintPicture(bitmap, assetEntity,400,400);//設(shè)置pictureBox的圖片源this.pictureBox1.Image = img;}這里新建了一個工具類ZxingHelper,調(diào)用其CreateQRCode方法返回生成二維碼的Bitmap格式,然后調(diào)用其
GetPrintPicture獲取調(diào)整布局后的照片。
在此之前,先新建一個存儲打印內(nèi)容的實體類AssetEntity
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace NPOITest {class AssetEntity{private string name;private string gender;private string url;public string Name { get => name; set => name = value; }public string Gender { get => gender; set => gender = value; }public string Url { get => url; set => url = value; }} }然后在工具類中
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Printing; using System.Linq; using System.Text; using System.Threading.Tasks; using ZXing; using ZXing.Common; using ZXing.QrCode;namespace NPOITest {class ZxingHelper{public static Bitmap CreateQRCode(string asset){EncodingOptions options = new QrCodeEncodingOptions{DisableECI = true,//編碼CharacterSet = "UTF-8",//寬度Width = 120,//高度Height = 120};BarcodeWriter writer = new BarcodeWriter();writer.Format = BarcodeFormat.QR_CODE;writer.Options = options;return writer.Write(asset);}public static Image GetPrintPicture(Bitmap image, AssetEntity asset, int picWidth, int picHeight){//新建Bitmap對象 用于返回 使用傳遞的參數(shù)作為寬度和高度Bitmap printPicture = new Bitmap(picWidth, picHeight);//高度int height = 5;//新建字體Font font = new Font("黑體", 10f);//Graphics :封裝一個 GDI+ 繪圖圖面//FromImage :從指定的 System.Drawing.Image 創(chuàng)建新的 System.Drawing.Graphics。Graphics g = Graphics.FromImage(printPicture);//Brush :定義用于填充圖形形狀(如矩形、橢圓、餅形、多邊形和封閉路徑)的內(nèi)部的對象。Brush brush = new SolidBrush(Color.Black);//設(shè)置此 System.Drawing.Graphics 的呈現(xiàn)質(zhì)量。g.SmoothingMode = SmoothingMode.HighQuality;//填加反鋸齒代碼效果g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;int interval = 15;int pointX = 5;//用指定的位置和大小初始化 System.Drawing.Rectangle 類的新實例。Rectangle destRect = new Rectangle(190, 10, image.Width, image.Height);//在指定位置并且按指定大小繪制指定的 System.Drawing.Image 的指定部分。//GraphicsUnit.Pixel: 指定給定的數(shù)據(jù)的度量值的單位。//DrawImage :在指定的位置并且按原始大小繪制指定的Image對象g.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);//height += 8;//用指定的位置和大小初始化 System.Drawing.RectangleF 類的新實例。RectangleF layoutRectangle = new RectangleF(pointX, height, 260f, 85f);//在指定矩形并且用指定的 System.Drawing.Brush 和 System.Drawing.Font 對象繪制指定的文本字符串g.DrawString("姓名:" + asset.Name, font, brush, layoutRectangle);height += interval;layoutRectangle = new RectangleF(pointX, height, 230f, 85f);g.DrawString("性別:" + asset.Gender, font, brush, layoutRectangle);height += interval;layoutRectangle = new RectangleF(pointX, height, 230f, 85f);g.DrawString("鏈接:" + asset.Url, font, brush, layoutRectangle);return printPicture;}} }?
總結(jié)
以上是生活随笔為你收集整理的Winform中使用zxing和Graphics实现自定义绘制二维码布局的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Winform中使用zxing实现二维码
- 下一篇: Winform中使用printDocum