C#生成二維碼的方法
本文實例講述了C#生成二維碼的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
首先引用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 根據(jù)鏈接獲取二維碼
/// <summary>
/// 根據(jù)鏈接獲取二維碼
/// </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);
}
}
}
PS:本站還提供了一個功能非常強大的二維碼生成工具,感興趣的朋友可以參考一下:
http://tools.jb51.net/transcoding/jb51qrcode
希望本文所述對大家的C#程序設計有所幫助。
相關(guān)文章
c# winform 解決PictureBox 無法打印全部圖片的問題
這篇文章主要介紹了c# winform 解決PictureBox 無法打印全部圖片的問題,幫助大家更好進行c# winform開發(fā),感興趣的朋友可以了解下2020-12-12C#中使用IFormattable實現(xiàn)自定義格式化字符串輸出示例
這篇文章主要介紹了C#中使用IFormattable實現(xiàn)自定義格式字符串輸出示例,本文直接給出實例代碼,需要的朋友可以參考下2015-06-06