第一种情况:将二维码生成张图片保存到本地文件夹里
第一步:下载ThoughtWorks.QRCode.dll
添加引用using System.Text;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
第二步:建个相比入径的文件夹接收图片Upload
第三步:生成二维码保存到文件夹中访问
string nr = "https://www.baidu.com/";
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(nr);
string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg";
string filepath = Server.MapPath(@"~\Upload") + "\\" + filename;
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
this.id1.ImageUrl = "~/Upload/" + filename;
fs.Close();
image.Dispose();
第二种情况:将链接绘制成二维码
第一步:下载ThoughtWorks.QRCode.dll
添加引用using System.Text;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
第二步:绘制二维码,并释放资源
Bitmap bmp = null;
try
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 4;
//int version = Convert.ToInt16(cboVersion.Text);
qrCodeEncoder.QRCodeVersion = 7;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
bmp = qrCodeEncoder.Encode("https://www.baidu.com/");
}
catch (Exception ex)
{
//MessageBox.Show("Invalid version !");
}
Bitmap img= bmp;
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
//System.Drawing.Font font = new System.Drawing.Font("宋体", 16); //字体与大小
//System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
//graphics.DrawString("www.cftea.com", font, brush, 50, 50); //写字,最后两个参数表示位置
//将图片保存到内存流中
System.IO.MemoryStream stream = new System.IO.MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
//在页面上输出
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite(stream.ToArray());
stream.Close();
stream.Dispose();
graphics.Dispose();
img.Dispose();
Response.End();
请发表评论