C#生成二维码的方法

时间:2024-01-18 21:28:02

本文实例讲述了C#生成二维码的方法。分享给大家供大家参考。具体实现方法如下:

首先引用ThoughtWorks.QRCode.dll

具体代码如下:

复制代码 代码如下:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using ThoughtWorks.QRCode.Codec;

namespace QRCodeUtil

{

    /// <summary>

    /// 二维码生成

    /// </summary>

    public class QRCodeHelper

    {

        #region 根据链接获取二维码

        /// <summary>

        /// 根据链接获取二维码

        /// </summary>

        /// <param name="link">链接</param>

        /// <returns>返回二维码图片</returns>

        public static Bitmap GetQRCodeBmp(string link)

        {

            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();

            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;

            qrCodeEncoder.QRCodeScale = 4;

            qrCodeEncoder.QRCodeVersion = 0;

            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;

            Bitmap bmp = qrCodeEncoder.Encode(link);

return bmp;

        }

        #endregion

}

}

使用示例如下:

复制代码 代码如下:

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 QRCodeUtil;
namespace SWX

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            Bitmap bmp = QRCodeHelper.GetQRCodeBmp(@"http://www.baidu.com/");

            string str = HttpContext.Current.Request.MapPath("\\aa.bmp");

            bmp.Save(str);

        }

    }

}