C# MVC4 QRCode二维码生成

引用微软二维码类库:ThoughtWorks.QRCode.dll

在控制器中(Controllers)引用命名空间:

using System.Drawing;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;

创建生成二维码方法:

//生成二维码
        public ContentResult Erweima(string id)
        {
            string bmp = "http://www.shenraytech.com/Products/Index/";
            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(bmp + id);
            //生成新的(时间)图片名称
            string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg";
            string filepath = Server.MapPath(@"~\picture") + "\\" + filename;
            //验证文件夹中是否有重命名的图片名,保证唯一性
            System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            //保存图片(以文件流的方式,保存到picture文件夹下)
            image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
            fs.Close();
            image.Dispose();
            //返回图片名称
            return Content(filename);

        }

在视图中(网页):


   
   
   



你可能感兴趣的:(C#基础知识)