C# 根据字符串生成二维码的实例代码

所属分类: 软件编程 / C#教程 阅读数: 55
收藏 0 赞 0 分享

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# 根据字符串生成二维码的资料请关注脚本之家其它相关文章!

更多精彩内容其他人还在看

C#基础:Dispose()、Close()、Finalize()的区别详解

本篇文章是对c#中的Dispose()、Close()、Finalize()的区别进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

C#字符串常见操作总结详解

本篇文章是对C#中字符串的常见操作进行了详细的总结介绍,需要的朋友参考下
收藏 0 赞 0 分享

c# 引用类型与值类型的区别详解

本篇文章是对c#中引用类型与值类型的区别进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

c# 实现IComparable、IComparer接口、Comparer类的详解

本篇文章是对c#中实现IComparable、IComparer接口、Comparer类进行了详细的分析详解,需要的朋友参考下
收藏 0 赞 0 分享

深入c# 类和结构的区别总结详解

本篇文章是对c#中类和结构的区别进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解析C#自定义控件的制作与使用实例的详解

本篇文章是对C#中自定义控件的制作与使用实例进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

C#实现路由器断开连接,更改公网ip的实例代码

C#实现路由器断开连接,更改公网ip的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中使用IrisSkin2.dll美化WinForm程序界面的方法

这篇文章主要介绍了c#中使用IrisSkin2.dll美化WinForm程序界面的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

.net C# 实现任意List的笛卡尔乘积算法代码

笛卡尔(Descartes)乘积又叫直积。假设集合A={a,b},集合B={0,1,2},则两个集合的笛卡尔积为{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}。
收藏 0 赞 0 分享

C#中实现任意List的全组合算法代码

这篇文章主要是介绍了.net C# 实现任意List的全组合算法实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多