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

时间:2021-03-14 23:18:33
string url = 二维码内容;
var qrcode = QRCodeTools.EncodeQrcode(url, , , 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, , );
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[] = encoderParameter;
ImageCodecInfo[] ImageCodecInfoArray = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegImageCodecInfo = null;
for (int i = ; 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(, SeekOrigin.Begin);
ms.Read(buffer, , buffer.Length);
image = System.Drawing.Image.FromStream(ms);
  imgBase64 = Convert.ToBase64String(buffer);//64位二进制图片
}