首页 > 代码库 > 二维码生成方法

二维码生成方法

从网上到处找代码东拼西凑出来的一个二维码生成方法,支持给二维码上面贴图标。

需要通过Nuget引用QRCode.Net

先贴个调用的Demo吧,直接从项目中拷过来的,看意思就好。

技术分享
  public string GetUserQRCode(Web_User user)        {          var url = VirtualPaths.HostAddress + "/Member/MemberSpaceAddFriend?name=" + user.Name;            string userHeadphoto = "";            //如果用户的头像不为空,则给二维码加上用户头像            if (!string.IsNullOrEmpty(user.HeadPhoto))            {                 userHeadphoto= VirtualPaths.ImgDir+user.HeadPhoto;            }            var path = VirtualPaths.ImgDir+ "/Upload/QRCode/";            string qrCode = SaveCodeImg(url, userHeadphoto, path);            user.QRCode = "/Upload/QRCode/" + qrCode;           UpdateUser(user);            return user.QRCode;        }
调用的Demo
    #region 二维码生成基础方法        /// <summary>        /// 生成用户信息的二维码        /// </summary>        /// <param name="url">要生成的字符串</param>        /// <param name="headPhoto">要贴到二维码上面的头像</param>        /// <param name="filePath">文件的保存路径</param>        /// <returns></returns>        public string SaveCodeImg(string url, string headPhoto, string filePath)        {            var fileName = GetFileNameByDate();            var path = Path.Combine(filePath, fileName);            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);            QrCode qrCode = new QrCode();            qrEncoder.TryEncode(url, out qrCode);            var renderer = new GraphicsRenderer(new FixedModuleSize(4, QuietZoneModules.Two));            //var image = new Bitmap();            Image img = new Bitmap(200, 200);            Image qrcode = new Bitmap(200, 200);            using (Graphics g = Graphics.FromImage(img))            {                renderer.Draw(g, qrCode.Matrix);                g.DrawImage(img, new Point { X = 0, Y = 0 });                //using (MemoryStream stream = new MemoryStream())                //{                //    img.Save(stream, ImageFormat.Jpeg);                //}                if (string.IsNullOrEmpty(headPhoto) || !File.Exists(headPhoto))                {                   // img.Save(path+".jpg");                    qrcode = img;                    qrcode.Save(path+".jpg");                }                else                {                    qrcode = CombinImage(img, headPhoto);                    qrcode.Save(path + ".jpg");                }            }            return fileName+".jpg";        }        private string GetFileNameByDate()        {            string productnumber = "";            //按年月日生成前12位            productnumber = DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, 0) + DateTime.Now.Day.ToString().PadLeft(2, 0);            productnumber += DateTime.Now.Hour.ToString().PadLeft(2, 0);            // +DateTime.Now.Minute.ToString().PadLeft(2, ‘0‘) + DateTime.Now.Second.ToString().PadLeft(2, ‘0‘);            //随即数生成4位            var rnd = new Random((int)DateTime.Now.Ticks);            productnumber += rnd.Next(0, 9999).ToString().PadLeft(4, 0);            return productnumber;        }        public static Image CombinImage(Image imgBack, string destImg)        {            Image img = Image.FromFile(destImg);        //照片图片                if (img.Height != 65 || img.Width != 65)            {                img = KiResizeImage(img, 65, 65, 0);            }            Graphics g = Graphics.FromImage(imgBack);            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);      //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);               //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框              //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);              g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);            GC.Collect();            return imgBack;        }        public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)        {            try            {                Image b = new Bitmap(newW, newH);                Graphics g = Graphics.FromImage(b);                // 插值算法的质量                  g.InterpolationMode = InterpolationMode.HighQualityBicubic;                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);                g.Dispose();                return b;            }            catch            {                return null;            }        }        #endregion

 

二维码生成方法