Winform used zxing and Graphics drawing two-dimensional code to implement custom layout

Scenes

zxing.dll Download

https://download.csdn.net/download/badao_liumang_qizhi/11623214

effect

 

achieve

According to the above article in the simple two-dimensional code generation, now adjust the layout.

Drag a button, double-click it to enter the event.

Private  void button6_Click ( Object SENDER, EventArgs E) 
        { 
            // dimensional code content object 
            AssetEntity assetEntity = new new AssetEntity () {the Name = " handed " , Gender = " M " , the Url = " 123 " };
             // generated using the above the method of two-dimensional code to obtain two-dimensional code bitmap objects 
            Bitmap bitmap = ZxingHelper.CreateQRCode ( " high-handed " );
             // re-drawn two-dimensional code layout 
            Image img = ZxingHelper.GetPrintPicture (bitmap, assetEntity, 400 , 400);
             // set the picture source pictureBox 
            the this .pictureBox1.Image = IMG; 
        }

 

Here a new tool class ZxingHelper, which calls the method returns CreateQRCode Bitmap format to generate two-dimensional code, and then call its

GetPrintPicture get adjusted layout photos.

Prior to this, first create a new print content storage entity class 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; }
    }
}

 

Then in Tools

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) 
        { 
            // new parameter for returning Bitmap object passed as the width and height 
            Bitmap printPicture = new new Bitmap (picwidth , picheight);
             // height 
            int height = . 5 ;
             // New Font 
            the Font font = new new the Font ( " bold " , 1OF);
             // Graphics: encapsulating a drawing surface GDI +
             //FromImage: Create a new System.Drawing.Graphics from the specified System.Drawing.Image. 
            G = Graphics Graphics.FromImage (printPicture);
             // Brush: definition object for filling the pattern shape (e.g., rectangular, oval, pie-shaped, polygonal, and paths) inside. 
            Brush = Brush new new SolidBrush (Color.Black);
             // set this System.Drawing.Graphics rendering quality. 
            = g.SmoothingMode SmoothingMode.HighQuality;
             // filling Code antialiasing effect 
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; 

            int interval The = 15 ;
             int pointX = . 5 ;
             //Position and size of a new instance of the class initialization System.Drawing.Rectangle specified. 
            = DestRect the Rectangle new new the Rectangle ( 190 , 10 , image.width, image.height);
             // the specified location and the specified size drawn in designated portions of System.Drawing.Image.
            // GraphicsUnit.Pixel: specify a given metric data units.
            // the DrawImage: drawn its original size and the specified objects in the specified location Image 
            g.drawImage (Image, destRect, 0 , 0 , image.width, image.height, GraphicsUnit.Pixel);
             //
             height + = . 8 ;
             / / with the specified location and size of a new instance of the class initialization System.Drawing.RectangleF.
            LayoutRectangle = RectangleF new new RectangleF (pointX, height, 260f, 85F);
             // specified to draw a rectangle and designated with the specified object text string System.Drawing.Brush and System.Drawing.Font 
            g.drawString ( " Name: " + asset.Name, font, Brush, layoutRectangle); 

            height + = interval The; 
            layoutRectangle = new new RectangleF (pointX, height, 230f, 85F); 
            g.drawString ( " gender: " + asset.Gender, font, Brush, layoutRectangle) ; 

            height + = interval The; 
            layoutRectangle= new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("链接:" + asset.Url, font, brush, layoutRectangle);

          

            return printPicture;
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/badaoliumangqizhi/p/11426919.html