QrCodeNet下载地址:http://qrcodenet.codeplex.com/
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render; namespace QRCodeDemo
{
public class QRCodeHelper
{
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="content">内容</param>
/// <param name="moduleSize">二维码的大小</param>
/// <returns>输出流</returns>
public static MemoryStream GetQRCode(string content, int moduleSize = )
{
//ErrorCorrectionLevel 误差校正水平
//QuietZoneModules 空白区域 var encoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = encoder.Encode(content);
GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two) , Brushes.Black, Brushes.White); MemoryStream memoryStream = new MemoryStream();
render.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, memoryStream); return memoryStream; //生成图片的代码
//DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
//Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
//Graphics g = Graphics.FromImage(map);
//render.Draw(g, qrCode.Matrix);
//map.Save(fileName, ImageFormat.Jpeg);//fileName为存放的图片路径
} /// <summary>
/// 生成带Logo二维码
/// </summary>
/// <param name="content">内容</param>
/// <param name="iconPath">logo路径</param>
/// <param name="moduleSize">二维码的大小</param>
/// <returns>输出流</returns>
public static MemoryStream GetQRCode(string content, string iconPath, int moduleSize = )
{
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = qrEncoder.Encode(content); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
Graphics g = Graphics.FromImage(map);
render.Draw(g, qrCode.Matrix); //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例
//PS:追加的图片过大超过二维码的容错率会导致信息丢失,无法被识别
Image img = Image.FromFile(iconPath); Point imgPoint = new Point((map.Width - img.Width) / , (map.Height - img.Height) / );
g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); MemoryStream memoryStream = new MemoryStream();
map.Save(memoryStream, ImageFormat.Jpeg); return memoryStream; //生成图片的代码: map.Save(fileName, ImageFormat.Jpeg);//fileName为存放的图片路径
}
}
}
Controller:
public ActionResult CreateQrCode()
{
string str = "http://cn.bing.com/search?q=C%23";
using (var memoryStream = QRCodeHelper.GetQRCode(str))
{
Response.ContentType = "image/jpeg";
Response.OutputStream.Write(memoryStream.GetBuffer(), , (int)memoryStream.Length);
Response.End();
}
return null;
}
View:
<img id="qrcode" src="@Url.Action("CreateQrCode")" />
效果如下: