using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Drawing;
using ThoughtWorks.QRCode.Codec;
using System.Drawing.Drawing2D;
using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal;
using System.IO;
using System.Drawing.Imaging;
namespace Yunlin.Web.shop.templates
{
public partial class QCcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap qrCode = GetQRCodeByZXingNet("HelloWorld",100,100);
string path = "~/" + "images/";
string savePath = HttpContext.Current.Server.MapPath(path);
if (!Directory.Exists(savePath))//如果不存在就创建file文件夹
{
Directory.CreateDirectory(savePath);//创建该文件夹
}
qrCode.Save(savePath + "qr.Jpeg", ImageFormat.Png);
}
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="strMessage">要生成二维码的字符串</param>
/// <param name="width">二维码图片宽度</param>
/// <param name="height">二维码图片高度</param>
/// <returns></returns>
private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height)
{
Bitmap result = null;
try
{
BarcodeWriter barCodeWriter = new BarcodeWriter();
barCodeWriter.Format = BarcodeFormat.QR_CODE;
barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
barCodeWriter.Options.Height = height;
barCodeWriter.Options.Width = width;
barCodeWriter.Options.Margin = 0;
BitMatrix bm = barCodeWriter.Encode(strMessage);
result = barCodeWriter.Write(bm);
}
catch (Exception ex)
{
//异常输出
}
return result;
}
}
}
05-15
1499
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)