ComplianceServer/oldcode/Common/QRCodeHelper.cs

194 lines
8.0 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using ThoughtWorks.QRCode.Codec;
namespace WX.CRM.Common
{
public class QRCodeHelper
{
public static QRCodeEncoder.ENCODE_MODE qrCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
/// <summary>
/// 大小(值越大生成的二维码图片像素越高)
/// </summary>
//public static int qrCodeScale = 4;
public static int qrCodeScale = 8;
/// <summary>
/// 版本(注意设置为0主要是防止编码的字符串太长时发生错误)
/// </summary>
public static int qrCodeVersion = 0;
/// <summary>
/// 错误效验、错误更正(有4个等级)
/// </summary>
public static QRCodeEncoder.ERROR_CORRECTION qrCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
/// <summary>
/// 二维码内容编码方式
/// </summary>
//public static System.Text.Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
public static System.Text.Encoding encode = System.Text.Encoding.UTF8;
public static byte[] Create(string qrCodeBody)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = qrCodeEncodeMode;
qrCodeEncoder.QRCodeScale = qrCodeScale;
qrCodeEncoder.QRCodeVersion = qrCodeVersion;
qrCodeEncoder.QRCodeErrorCorrect = qrCodeErrorCorrect;
Image image = qrCodeEncoder.Encode(qrCodeBody, encode);
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
return buffer;
}
}
public static Image CreateImg(string qrCodeBody)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = qrCodeEncodeMode;
qrCodeEncoder.QRCodeScale = qrCodeScale;
qrCodeEncoder.QRCodeVersion = qrCodeVersion;
qrCodeEncoder.QRCodeErrorCorrect = qrCodeErrorCorrect;
Image image = qrCodeEncoder.Encode(qrCodeBody, encode);
return image;
}
public static byte[] GetQrCode(string name, string price, string productName, string savePath, string url, string payType)
{
var dicPath = Path.Combine(Path.Combine(System.Web.HttpContext.Current.Request.PhysicalApplicationPath, "UploadFile"), "PayQRCode");
var dituPath = Path.Combine(dicPath, "ditu.png");
var wxPath = Path.Combine(dicPath, "wx.png");
var aliPath = Path.Combine(dicPath, "ali.png");
using (Image imgbig = new Bitmap(dituPath))
{
using (Graphics g = Graphics.FromImage(imgbig))
{
var name_font = new Font("微软雅黑", 30, FontStyle.Bold);
g.DrawString(name, name_font, Brushes.Black, 40, 80);
var productName_font = new Font("微软雅黑", 40, FontStyle.Bold);
//var productName_format = new StringFormat{ Alignment = StringAlignment.Center };
SizeF productName_size = g.MeasureString(productName, productName_font);
var productName_x = (imgbig.Width - productName_size.Width) / 2;
//var productName_y = (imgbig.Width - productName_size.Height) / 2;
var productName_y = 165;
g.DrawString(productName, productName_font, Brushes.Orange, productName_x, productName_y);
var price_font = new Font("微软雅黑", 40, FontStyle.Bold);
SizeF price_size = g.MeasureString(price, price_font);
var price_x = (imgbig.Width - price_size.Width) / 2;
//var productName_y = (imgbig.Width - productName_size.Height) / 2;
var price_y = 235;
g.DrawString(price, price_font, Brushes.Black, price_x, price_y);
//二维码
var ssImg = string.Empty;
if (payType == "ali")
{
ssImg = aliPath;
}
else
{
ssImg = wxPath;
}
var img = CombinImage(CreateImg(url), ssImg);
//计算图片大小
float w = imgbig.Width / 4;
float pw = w / (img.Width * (float)0.45);
float ph = w / (img.Height * (float)0.45);
if (pw > ph)
{
pw = ph;
}
int mw = (int)(pw * img.Width);
int mh = (int)(pw * img.Height);
//计算图片在二维上的x,y坐标
int x = (imgbig.Width - mw) / 2;
int y = (imgbig.Height - mh) / 2 + 60;
g.DrawImage(img, x, y, mw, mh);
}
imgbig.Save(savePath);
using (MemoryStream ms = new MemoryStream())
{
imgbig.Save(ms, ImageFormat.Jpeg);
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
return buffer;
}
}
}
/// <summary>    
        /// 调用此函数后使此两种图片合并,类似相册,有个    
        /// 背景图,中间贴自己的目标图片    
        /// </summary>    
        /// <param name="imgBack">粘贴的源图片</param>    
        /// <param name="destImg">粘贴的目标图片</param>    
        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;
}
/// <summary>    
        /// Resize图片    
        /// </summary>    
        /// <param name="bmp">原始Bitmap</param>    
        /// <param name="newW">新的宽度</param>    
        /// <param name="newH">新的高度</param>    
        /// <param name="Mode">保留着,暂时未用</param>    
        /// <returns>处理以后的图片</returns>    
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;
}
}
}
}