C#(Asp.Net)生成链接二维码

生成链接二维码,扫描二维码即可进入对应的链接网站。
1.下载ThoughtWorks.QRCode.dll动态库
忘记怎么上传资源了,这个库网上比较多,可以自行下载。

2. aspx界面

<form id="form1" runat="server">
        <div>
            <asp:Image ID="id1" runat="server" Width="300px" Height="300px" />
        </div>
    </form>

2.后台代码
.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using ThoughtWorks.QRCode;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using System.Data.SqlTypes;
using System.IO;

namespace 生成动态二维码
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            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();
        }
    }
}

效果图展示:
在这里插入图片描述
如果有疑问可在评论区留言。

©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页