二维码早就传遍大江南北了,总以为它是个神奇的东西,其实细细研究之后发现也没想象的那么神秘,碰巧最近项目中需要动态生成二维码,解决完实际问题之后,简单总结整理一下。项目中除了动态生成二维码之外,还实现了动态生成自定义图片,二维码可以是其中的元素。
<body>
<form id="form1" runat="server" >
<div>
<img src="GenerateImage.aspx?type=2" />
div>
form>
body>
GenerateImage.aspx.cs文件内容
protected void Page_Load(object sender, EventArgs e)
{
string type = Request.QueryString["type"].ToString();
Bitmap codeImage = Create_QRCode("分享才能获得更多,我尽力而为(5201314)", 6);
MemoryStream ms = Create_ImgCode(codeImage, "分享才能获得更多,我尽力而为", "5201314", type);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
Response.End();
}
private Bitmap Create_QRCode(string codeNumber, int size)
{
//创建二维码生成类
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置编码模式
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//设置编码测量度
qrCodeEncoder.QRCodeScale = size;
//设置编码版本
qrCodeEncoder.QRCodeVersion = 10;
//设置编码错误纠正
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//生成二维码图片
System.Drawing.Bitmap codeImage = qrCodeEncoder.Encode(codeNumber, Encoding.UTF8);
return codeImage;
}
///
/// 生成自定义图片
///
/// 生成的二维码
/// 物体名称
/// 自定义图片内存流
private MemoryStream Create_ImgCode(Bitmap codeImage, string objectName, string objectCode, string type)
{
string path = string.Empty;
if (type == "1")
{
//设置背景图片
path = Server.MapPath("Images/backimg1.png");
}
else if (type == "2")
{
//设置背景图片
path = Server.MapPath("Images/backimg2.png");
}
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
Bitmap bg = new Bitmap(img);
//为画布bg(图片bg)创建一只画笔
Graphics g = Graphics.FromImage(bg);
if (type == "1")
{
//【1】将位图文件codeImage画到画布g上
//【2】codeImage左上角距画布左边界25px、距画布上边界56px
//【3】codeImage的长为原长、宽为原宽
g.DrawImage(codeImage, 25, 56, codeImage.Width, codeImage.Height);
}
else if (type == "2")
{
g.DrawImage(codeImage, 132, 19, 162, 162);
System.Drawing.Brush b = new SolidBrush(Color.Black);
Font font = new Font("宋体", 8, FontStyle.Regular);
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center; // 垂直居中
sf.Alignment = StringAlignment.Near; // 水平左对齐
//string也是画到画布上的,当画的string长度大于112px时会自动换行
SizeF stringSize = g.MeasureString("我的宣言:", font, 112, sf);
int nWidth = (int)stringSize.Width + 1;
int nHeight = (int)stringSize.Height + 1;
RectangleF rf = new Rectangle(new Point(12, 64), new Size(nWidth, nHeight));
g.DrawString("我的宣言:", font, b, rf, sf);
stringSize = g.MeasureString(objectName, font, 112, sf);
int objectWidth = (int)stringSize.Width + 1;
int objectHeight = (int)stringSize.Height + 1;
rf = new Rectangle(new Point(12, 64 + nHeight + 8), new Size(objectWidth, objectHeight));
g.DrawString(objectName, font, b, rf, sf);
SizeF stringSize1 = g.MeasureString("幸运数字:", font, 112, sf);
nWidth = (int)stringSize1.Width + 1;
nHeight = (int)stringSize1.Height + 1;
RectangleF rf1 = new Rectangle(new Point(12, 136), new Size(nWidth, nHeight));
g.DrawString("幸运数字:", font, b, rf1, sf);
stringSize1 = g.MeasureString(objectCode, font, 112, sf);
objectWidth = (int)stringSize1.Width + 1;
objectHeight = (int)stringSize1.Height + 1;
rf1 = new Rectangle(new Point(12, 136 + nHeight + 8), new Size(objectWidth, objectHeight));
g.DrawString(objectCode, font, b, rf1, sf);
}
g.Dispose();
GC.Collect();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bg.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//将画布bg(图片bg)保存到指定路径
path = Server.MapPath("Images");
bg.Save(path + "\\photoName.png", System.Drawing.Imaging.ImageFormat.Png);
codeImage.Dispose();
bg.Dispose();
return ms;
}
ThoughtWorks.QRCode.dll免费下载地址:http://download.csdn.net/detail/xiaouncle/9636525