QRCodeHelper 二维码生成

 using System;
using System.Drawing;
using ThoughtWorks.QRCode.Codec;
using System.IO;
using System.Text;
using System.Drawing.Drawing2D;
using System.Web; namespace APP.QrCodes
{
public class QRCodeHelper
{
public enum ImgEnum
{
JPG
} public static string GetImgType(ImgEnum jpg)
{
string ext = string.Empty;
switch (jpg)
{
case ImgEnum.JPG:
ext = ".jpg";
break;
default:
break;
}
return ext;
}
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="codeNumber">要生成二维码的字符串</param>
/// <param name="size">大小尺寸</param>
/// <returns>二维码图片</returns>
public Bitmap Create_ImgCode(string codeNumber, int size)
{
//创建二维码生成类
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置编码模式
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//设置编码测量度
qrCodeEncoder.QRCodeScale = size;
//设置编码版本
qrCodeEncoder.QRCodeVersion = ;
//设置编码错误纠正
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//生成二维码图片
System.Drawing.Bitmap image = qrCodeEncoder.Encode(codeNumber, Encoding.UTF8); return image;
} public static string CreateLogQrCode(string strContent)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = ;
qrCodeEncoder.QRCodeVersion = ;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
System.Drawing.Image image = qrCodeEncoder.Encode(strContent, Encoding.UTF8);//生成二维码图片 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, HttpRuntime.AppDomainAppPath + "QrCodes/logo.png").Save(MStream1, System.Drawing.Imaging.ImageFormat.Png); string reStr = Convert.ToBase64String(MStream1.ToArray());
//image.Dispose();
MStream.Dispose();
MStream1.Dispose();
return reStr;
} public string SaveImg(Bitmap bmp, string path)
{
bmp.Save(path);
return path;
} /// <summary>
/// 保存图片
/// </summary>
/// <param name="bmp">源</param>
/// <param name="path">路径</param>
/// <param name="jpg">格式</param>
/// <returns></returns>
public bool SaveImg(Bitmap bmp, string path,ImgEnum jpg)
{
try
{
bmp.Save(path);
return true;
}
catch
{
return false;
}
} /// <summary>
/// 保存图片
/// </summary>
/// <param name="bmp">源</param>
/// <param name="path">路径</param>
/// <param name="path">文件名</param>
/// <returns></returns>
public bool SaveImg(Bitmap bmp, string path,string fileName)
{
try
{
bmp.Save(path+fileName);
return true;
}
catch
{
return false;
}
} /// <summary>
/// 保存图片
/// </summary>
/// <param name="bmp">源</param>
/// <param name="path">路径</param>
/// <param name="path">文件名</param>
/// <param name="jpg">格式</param>
/// <returns></returns>
public bool SaveImg(Bitmap bmp, string path, string fileName,ImgEnum jpg)
{
try
{
var ext = GetImgType(jpg);
bmp.Save(path + fileName + ext);
return true;
}
catch
{
return false;
}
} /// <summary>
/// Bitmap转化base64编码
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public string GetBase64String(Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = ;
ms.Read(arr, , (int)ms.Length);
ms.Close();
string pic = Convert.ToBase64String(arr);
return pic;
} /// <summary>
/// 得到流
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public Stream GetStream(Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = ;
ms.Read(arr, , (int)ms.Length);
ms.Close();
return ms;
} /// <summary>
/// 调用此函数后使此两种图片合并,类似相册,有个
/// 背景图,中间贴自己的目标图片
/// </summary>
/// <param name="imgBack">粘贴的源图片</param>
/// <param name="destImg">粘贴的目标图片</param>
/// <summary>
public static Image CombinImage(Image imgBack, string destImg)
{
Image img = Image.FromFile(destImg); //照片图片
if (img.Height != || img.Width != )
{
img = KiResizeImage(img, , , );
}
Graphics g = Graphics.FromImage(imgBack); g.DrawImage(imgBack, , , 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 / - img.Width / , imgBack.Width / - img.Width / , 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(, , newW, newH), new Rectangle(, , bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
}
}
05-11 01:15