|
|
@@ -0,0 +1,115 @@ |
|
|
|
using System; |
|
|
|
using System.Web; |
|
|
|
using ThoughtWorks.QRCode.Codec; |
|
|
|
using ThoughtWorks.QRCode.Codec.Data; |
|
|
|
using ThoughtWorks.QRCode.Codec.Util; |
|
|
|
using System.IO; |
|
|
|
using System.Text; |
|
|
|
using System.Drawing; |
|
|
|
using System.Drawing.Drawing2D; |
|
|
|
|
|
|
|
namespace Learun.Util |
|
|
|
{ |
|
|
|
public class QRCodeHelper |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// 生成带图片的二维码 |
|
|
|
/// </summary> |
|
|
|
/// <param name="qrcodeText">二维码内容</param> |
|
|
|
/// <param name="imagePath">二维码上图片地址</param> |
|
|
|
public static string ProcessRequest(string qrcodeText,string imagePath,string savePath) |
|
|
|
{ |
|
|
|
//最终图片保存路径 |
|
|
|
var resultImgPath = string.Empty; |
|
|
|
if (!string.IsNullOrEmpty(qrcodeText)) |
|
|
|
{ |
|
|
|
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); |
|
|
|
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; |
|
|
|
qrCodeEncoder.QRCodeScale = 4; |
|
|
|
qrCodeEncoder.QRCodeVersion = 8; |
|
|
|
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; |
|
|
|
System.Drawing.Image image = qrCodeEncoder.Encode(qrcodeText); |
|
|
|
System.IO.MemoryStream MStream = new System.IO.MemoryStream(); |
|
|
|
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png); |
|
|
|
System.IO.MemoryStream MStream1 = new System.IO.MemoryStream(); |
|
|
|
CombinImage(image, HttpContext.Current.Server.MapPath(imagePath)).Save(MStream1, System.Drawing.Imaging.ImageFormat.Png); |
|
|
|
//保存图片到本地文件夹 |
|
|
|
System.Drawing.Image img = System.Drawing.Image.FromStream(MStream1); |
|
|
|
//string imgPath = System.IO.Path.Combine(Config.GetValue("AnnexesFile"), "带logo图片二维码", DateTime.Now.ToString("yyyyMMddHHmm")); |
|
|
|
string imgPath = System.IO.Path.Combine(Config.GetValue("AnnexesFile"), "带logo图片二维码", savePath); |
|
|
|
if (!System.IO.Directory.Exists(imgPath)) |
|
|
|
{ |
|
|
|
System.IO.Directory.CreateDirectory(imgPath); |
|
|
|
} |
|
|
|
img.Save(System.IO.Path.Combine(imgPath, qrcodeText+".png"), System.Drawing.Imaging.ImageFormat.Png); |
|
|
|
resultImgPath = imgPath; |
|
|
|
|
|
|
|
//HttpContext.Current.Response.ClearContent(); |
|
|
|
//HttpContext.Current.Response.ContentType = "image/png"; |
|
|
|
//HttpContext.Current.Response.BinaryWrite(MStream1.ToArray()); |
|
|
|
//image.Dispose(); |
|
|
|
MStream.Dispose(); |
|
|
|
MStream1.Dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
//HttpContext.Current.Response.Flush(); |
|
|
|
//HttpContext.Current.Response.End(); |
|
|
|
|
|
|
|
return resultImgPath; |
|
|
|
} |
|
|
|
/// <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; |
|
|
|
} |
|
|
|
} |
|
|
|
public bool IsReusable |
|
|
|
{ |
|
|
|
get |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |