• QrCode.Net 生成二维码图片


      QrCode.Net  0.4.0

      最近实现.net生成二维码。如果二维码png图片不存在,则使用第三方库QrCode.Net生成二维码png图片保存到服务器上,然后返回该二维码的生成路径(因为生成png图片的路径,是根据参数归类到**年文件夹 下的**月文件夹 下的 **日文件夹下,所以要根据参数找对应的二维码图片路径)

    1.打开 程序包管理器 控制台,在窗口中 输入  Install-Package QrCode.Net,就可以通过 nuget获取到 QrCode.Net  0.4.0 dll包。

    2.新建类,添加引用。

    using Gma.QrCodeNet.Encoding;
    using Gma.QrCodeNet.Encoding.Windows.Render;

    3.该类实现代码如下。

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using Gma.QrCodeNet.Encoding;
    using Gma.QrCodeNet.Encoding.Windows.Render;
    
    namespace CYP2014.Shd.Common
    {
        public class GenerateImage
        {
            private static readonly CYPLog.TextLogger _generateImage = CYPLog.TextLogManager.Create(typeof (GenerateImage));
    
            /// <summary>
            /// 获取分享到微信二维码图片路径,如果图片不存在,则生成二维码png图片
            /// </summary>
            /// <param name="tradeCode">车辆编码</param>
            /// <param name="wayTag">检测报告导入平台  1  PC端   2 WCF   默认为1</param>
            /// <returns>二维码图片路径</returns>
            public static string GetImage(string tradeCode, int wayTag = 1)
            {
                try
                {
                    if (string.IsNullOrEmpty(tradeCode))
                    {
                        return "";
                    }
    
                    //图片路径  
                    string fileName = tradeCode.Trim() + ".png";
                    string secondFolder = "/WeChatImg";
    
                    string subYearMonth = tradeCode.Substring(3, 5);
                    int yearNum = 0;
                    int dayNum = 0;
                    int.TryParse(subYearMonth.Substring(0, 2), out yearNum);
                    int.TryParse(subYearMonth.Substring(2, 3), out dayNum);
                    int.TryParse("20" + yearNum, out yearNum);
    
                    //声明日期
                    var date = new DateTime();
                    date = date.AddYears(yearNum).AddYears(-1);
                    date = date.AddDays(dayNum).AddDays(-1);
                    secondFolder = string.Format("{0}\{1}\{2}\{3}\", secondFolder, yearNum, date.Month, date.Day);
                    string tagFolder = System.Web.Hosting.HostingEnvironment.MapPath("~" + secondFolder);
                    if (!Directory.Exists(tagFolder))
                    {
                        Directory.CreateDirectory(tagFolder);
                    }
    
                    string newfilename = tagFolder + fileName;
                    if (!File.Exists(newfilename))
                    {
                        //请求 下载图片
                        //var httpWebRequest = WebRequest.Create(url);
                        //var httpWebResponse = httpWebRequest.GetResponse();
                        //var stream = httpWebResponse.GetResponseStream();
                        //var bitmap = (Bitmap) Image.FromStream(stream);
                        ////保存图片文件  
                        //bitmap.Save(newfilename, System.Drawing.Imaging.ImageFormat.Png);
    
                        string url = string.Format(@"{0}Share/Report.aspx?tradecode={1}&waytag={2}", Config.MobileServiceUrl,
                            tradeCode, wayTag);
                        GenQrCode(newfilename, url);
                    }
                    return string.Concat(Config.VirtualDirectory, secondFolder, fileName);
                }
                catch (Exception ex)
                {
                    _generateImage.Info(string.Format(
                        "GenerateImage.GetImage异常。参数:tradeCode=【{0}】,wayTag=【{1}】。异常信息:【{2}】", tradeCode, wayTag,
                        ex.ToString()));
                    return string.Empty;
                }
            }
    
            /// <summary>
            /// 生成二维码,并保存图片到指定路径下
            /// </summary>
            /// <param name="fileName">图片保存路径全名(包括路径和文件名)</param>
            /// <param name="content">要生成二维码的内容</param>
            public static void GenQrCode(string fileName, string content)
            {
                try
                {
                    var qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
                    var qrCode = qrEncoder.Encode(content);
                    var gRender = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two));
    
                    using (var stream = new FileStream(fileName, FileMode.Create))
                    {
                        gRender.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream, new Point(600, 600));
                    }
    
                }
                catch (Exception ex)
                {
                    _generateImage.Info(
                        string.Format(
                            "GenerateImage.GenQrCode异常。参数:content=【{0}】,fileName=【{1}】。异常信息:【{2}】", content, fileName,
                            ex.ToString()));
                }
    
            }
        }
    }
    View Code
  • 相关阅读:
    oracle索引学习
    (转载)Windows下手动完全卸载Oracle
    Funsion Charts 学习(二)
    Funsion Charts 学习(一)
    集合操作
    文件操作
    字典操作
    字符串操作
    列表操作
    python基本运算
  • 原文地址:https://www.cnblogs.com/qqxiongmao/p/4402734.html
Copyright © 2020-2023  润新知