C# 根據(jù)字符串生成二維碼的實(shí)例代碼
更新時(shí)間:2020年07月13日 11:32:23 作者:江北
這篇文章主要介紹了C# 根據(jù)字符串生成二維碼的實(shí)例,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
1.先下載NuGet包(ZXing.Net)

2.新建控制器及編寫后臺(tái)代碼
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXing;
using ZXing.QrCode;
namespace WebApplication1.Controllers
{
public class StrController : Controller
{
// GET: Str
public ActionResult Index()
{
return View();
}
/// <summary>
/// 生成二維碼方法
/// </summary>
/// <param name="text">輸入的字符串</param>
/// <param name="width">二維碼寬度</param>
/// <param name="height">二維碼高度</param>
/// <returns></returns>
public string QRcode(string text, string width, string height)
{
string Response = "";
try
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.DisableECI = true;
//設(shè)置內(nèi)容編碼
options.CharacterSet = "UTF-8";
//將傳來的值賦給二維碼的寬度和高度
options.Width = Convert.ToInt32(width);
options.Height = Convert.ToInt32(height);
//設(shè)置二維碼的邊距,單位不是固定像素
options.Margin = 1;
writer.Options = options;
Bitmap map = writer.Write(text);
string di = text + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
//二維碼會(huì)顯示在桌面(你也想顯示在桌面的話,要改一下路徑)
string path = Path.Combine("C:\\Users\\zhulin\\Desktop", di);
map.Save(path, ImageFormat.Png);
map.Dispose();
Response = "二維碼生成成功!";
}
catch (Exception)
{
Response = "二維碼生成失?。?;
}
return Response;
}
}
}
3.前端
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Scripts/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet" />
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
var w = $("#wd").val();
var h = $("#hg").val();
var text = $("#tx").val();
$.ajax({
url: "/Str/QRcode",
data: "text=" + text + "&width=" + w + "&height=" + h,
success: function (e) {
alert(e);
}
});
});
})
</script>
</head>
<body>
<div style="margin-top:20px;margin-left:20px;">
<p>高度:<input type="text" style="width:60px;height:32px;border:1px solid #66b1ff;margin-left:3px;" id="wd" /><span style="margin-left:10px;">寬度:</span><input type="text" style="width:60px;height:32px;border:1px solid #66b1ff;margin-left:3px;" id="hg" /></p>
<input type="text" style="width:200px;height:32px;border:1px solid #66b1ff;" id="tx" placeholder="請輸入字符串..." /><button type="button" class="btn btn-info" id="btn" style="margin-left:5px;margin-top:-1px;height:33px;">提交</button>
</div>
</body>
</html>
4.效果:


以上就是C# 根據(jù)字符串生成二維碼的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于C# 根據(jù)字符串生成二維碼的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
- c# 生成二維碼的示例
- C#如何用ThoughtWorks生成二維碼
- C#實(shí)現(xiàn)掃描槍掃描二維碼并打印(實(shí)例代碼)
- C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
- C#生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)實(shí)例代碼
- C#二維碼圖片識(shí)別代碼
- C#利用ZXing.Net生成條形碼和二維碼
- C#生成帶logo的二維碼
- C#生成二維碼的方法
- .NET C#利用ZXing生成、識(shí)別二維碼/條形碼
- C#利用QrCode.Net生成二維碼(Qr碼)的方法
- C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹
相關(guān)文章
System.Data.OleDb.OleDbException: 未指定的錯(cuò)誤的完美解決方法
本文給大家?guī)砣N有關(guān)System.Data.OleDb.OleDbException: 未指定的錯(cuò)誤的完美解決方法,每種方法都很不錯(cuò),需要的朋友可以參考下2016-09-09
C# .Net實(shí)現(xiàn)灰度圖和HeatMap熱力圖winform(進(jìn)階)
本文主要介紹了C# .NET實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
C#獲得程序的根目錄以及判斷文件是否存在的實(shí)例講解
今天小編大家分享一篇C#獲得程序的根目錄以及判斷文件是否存在的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
C#實(shí)現(xiàn)獲取電腦硬件顯卡核心代號(hào)信息
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)獲取電腦硬件顯卡核心代號(hào)信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01

