生成一张带有logo的二维码图片

string url = 二维码内容;
            var qrcode = QRCodeTools.EncodeQrcode(url, 500, 500, Encoding.UTF8);

            // 合成活动的LOGO图片
            var hasImage = GlobalTools.GetCommonContent("TG_EVENT_LOGO_IMG", lott.WX_LOTT_TYPE_SEQ, out contentType, out contentLength, out content)//获取后台上传的logo图;
            if (hasImage)
            {
                var logo = Image.FromStream(new MemoryStream(content));
                Graphics graph = Graphics.FromImage(qrcode);
                graph.DrawImage(logo, qrcode.Width / 2.6f, qrcode.Height / 2.6f, 100, 100);
                graph.Dispose();
            }

            Image image;
            using (MemoryStream ms = new MemoryStream())
            {
  #region 压缩二进制图片质量
 EncoderParameters encoderParameters = new EncoderParameters();
                EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L);//修改此参数,改变生成的图片质量
                encoderParameters.Param[0] = encoderParameter;
                ImageCodecInfo[] ImageCodecInfoArray = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegImageCodecInfo = null;
                for (int i = 0; i < ImageCodecInfoArray.Length; i++)
                {
                    if (ImageCodecInfoArray[i].FormatDescription.Equals("JPEG"))
                    {
                        jpegImageCodecInfo = ImageCodecInfoArray[i];
                        break;
                    }
                }
                qrcode.Save(ms, jpegImageCodecInfo, encoderParameters);
  #endregion
                byte[] buffer = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                image = System.Drawing.Image.FromStream(ms);
  imgBase64 = Convert.ToBase64String(buffer);//64位二进制图片 }

 

posted @ 2019-10-10 10:03  过一天日子修一天缘  阅读(653)  评论(0编辑  收藏  举报