c#4.0 qrcode二维码生成 简单封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;

namespace QRcode
{
    public class QrCodeUtil
    {
        public enum QrCodeType
        {
            Materiel = 0//备品备件

        }

        /*qrcode 使用方法
        1.引用【右击】->管理NuGet程序包->搜索‘qrcode’ ->安装 QrCode.net
        2.引入下列命名空间
            using Gma.QrCodeNet.Encoding;
            using Gma.QrCodeNet.Encoding.Windows.Render;
            using System.Drawing.Imaging;
            using System.IO;
            using System.Drawing;

        */

        //目前只支持英文  content-需生成二位的字符串    fileName-文件绝对位置及文件名(png文件)
        public static void createQrCode(String content, QrCodeType  type, String fileName)
        {
            String filePath = getFilePath(type);

            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = qrEncoder.Encode(content);

            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(20, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            using (FileStream stream = new FileStream(filePath + fileName, FileMode.Create))
            {
                render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
                stream.Close();
            }

            addText(fileName.Split('.')[0], filePath+fileName);
        }

        private static void addText(String name, String fileName)
        {
            Font font = new Font("GB2312", 14, FontStyle.Regular);//设置字体,大小,粗细
            SolidBrush sbrush = new SolidBrush(Color.Black);//设置颜色

            try
            {
                Bitmap im = new Bitmap(fileName);
                Bitmap bmp = new Bitmap(300, 340); //定义图片大小
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.White);
                g.DrawString(name, font, sbrush, new PointF((Int32)(bmp.Width - name.Length * 10.8) / 2, bmp.Height - 40));

                // 合并位图
                g.DrawImage(im, new Rectangle(0, 0, 300, 300));
                im.Dispose();

                bmp.Save(fileName, ImageFormat.Png);
                g.Dispose();
                bmp.Dispose();
            }
            catch
            {

            }

        }

        public static bool qrCodeFileIsExist(QrCodeType type,String fileName)
        {
           return File.Exists(getFilePath(type)+fileName);
        }

        private static String getFilePath(QrCodeType type)
        {
            switch (type)
            {
                case QrCodeType.Materiel:
                    {
                        return getProjectPath() + "\\QrCode\\materiel\\"; 
                    }
                default:
                    break;
            }
            return null;
        }

        private static String getProjectPath()
        {
            return Directory.GetCurrentDirectory();
        }

    }
}

 

posted @ 2019-01-20 14:57  心的记忆  阅读(451)  评论(0编辑  收藏  举报