时间:2021-05-20
1.先下载NuGet包(ZXing.Net)
2.新建控制器及编写后台代码
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; //设置内容编码 options.CharacterSet = "UTF-8"; //将传来的值赋给二维码的宽度和高度 options.Width = Convert.ToInt32(width); options.Height = Convert.ToInt32(height); //设置二维码的边距,单位不是固定像素 options.Margin = 1; writer.Options = options; Bitmap map = writer.Write(text); string di = text + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; //二维码会显示在桌面(你也想显示在桌面的话,要改一下路径) 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# 根据字符串生成二维码的实例代码的详细内容,更多关于C# 根据字符串生成二维码的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例总结了ASP.NET生成二维码的方法。分享给大家供大家参考,具体如下:分享一例c#生成二维码的代码,直接引用ThoughtWorks.QRCode.dl
复制代码代码如下://////生成二维码//////生成二维码路径///生成的内容///二维码宽///二维码高///需生成的Logo图片///privateBi
最近接到这样一个需求,需要在小程序里将十几位随机字符串转换为二维码的形式展示。公众号中(另一终端)调用JSSDK扫一扫功能,去扫描小程序生成的二维码。得到字符串
带logo的二维码生成分为两步骤:首先根据输入的内容生成二维码图片,然后读取本地的logo图片,通过图片处理生成带logo的二维码。生成的二维码效果如下:下面直
本文实例讲述了PHP生成二维码与识别二维码的方法。分享给大家供大家参考,具体如下:二维码的分类线性堆叠式二维码矩阵式二维码二维码的优缺点优点信息容量大编码范围广