C#生成二维码(可带logo)

首先需要添加引用QrCodeNet.dll

然后引入Gma.QrCodeNet.Encoding命名空间。

public static Bitmap createQRCImg(string s, int ew = 10, int eh = 10, Color[] colors = null, ErrorCorrectionLevel ell = ErrorCorrectionLevel.H)
    {
        Random rd = new Random();
        QrEncoder qrEncoder = new QrEncoder(ell);
        QrCode qrCode = new QrCode();
        qrEncoder.TryEncode(s, out qrCode);

        if (!qrCode.isContainMatrix)
        {
            return null;
        }

        //生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败  
        int width = qrCode.Matrix.Width;
        int height = qrCode.Matrix.Height;
        //二维矩阵转为一维像素数组,也就是一直横着排了  
        Bitmap result = new Bitmap(ew * width, eh * height);//新建一个bmp图片
        Graphics g = System.Drawing.Graphics.FromImage(result);//新建一个画板
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
        g.Clear(System.Drawing.Color.White); //清空画布并以透明背景色填充
        if (colors == null)
        {
            colors = new Color[] { Color.Black };
        }

        //Pen pB = new Pen(Color.Black);
        Pen pW = new Pen(Color.White);
        //System.Drawing.SolidBrush bB = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
        System.Drawing.SolidBrush bW = new System.Drawing.SolidBrush(System.Drawing.Color.White);
        //g.DrawRectangle(pB, 0, 0, ew * width, eh * height);
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                if (qrCode.Matrix[x, y])
                {
                    Color c = getRandomColor(colors);
                    g.DrawRectangle(new Pen(c), x * ew, y * eh, ew, eh);
                    g.FillRectangle(new SolidBrush(c), x * ew, y * eh, ew, eh);
                }
                else
                {
                    g.DrawRectangle(pW, x * ew, y * eh, ew, eh);
                    g.FillRectangle(bW, x * ew, y * eh, ew, eh);
                }
            }
        }
        g.Dispose();
        pW.Dispose();
        bW.Dispose();
        return result;
    }
    static int ii = 0;

    private static Color getRandomColor(Color[] colors)
    {
        Random rd = new Random();
        ii++;
        if (ii >= colors.Length)
        {
            ii = 0;
        }
        return colors[ii];

        // return colors[rd.Next(0, colors.Length)];
        //HttpContext.Current.Response.Write();
    }
View Code

调用

private void rqcode()
    {
        string url = "http://www.baidu.com";
        System.Drawing.Bitmap qrcode = MyQRCodeClass.createQRCImg(url, ew: 4, eh: 4);

        //在二维码的Bitmap对象上绘制logo图片
        Bitmap logoImg = Bitmap.FromFile(Server.MapPath("/logo.png")) as Bitmap;
        Graphics g = Graphics.FromImage(qrcode);
        Rectangle logoRec = new Rectangle(); // 设置logo图片的大小和绘制位置
        logoRec.Width = qrcode.Width / 6;
        logoRec.Height = qrcode.Height / 6;
        logoRec.X = qrcode.Width / 2 - logoRec.Width / 2; // 中心点
        logoRec.Y = qrcode.Height / 2 - logoRec.Height / 2;
        g.DrawImage(logoImg, logoRec);

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        qrcode.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.ContentType = "image/Gif";
        System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

posted @ 2020-08-05 23:03  黑色老猫。  阅读(461)  评论(0编辑  收藏  举报