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

 更新时间:2020年07月13日 11:32:23   作者:江北  
这篇文章主要介绍了C# 根据字符串生成二维码的实例,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下

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#备忘录人生存档的设计模式实例

    C#备忘录人生存档的设计模式实例

    这篇文章主要为大家介绍了C#设计模式中备忘录模式的实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • 详解C#开发Android应用程序的流程

    详解C#开发Android应用程序的流程

    在本篇文章里小编给大家分享了关于C#开发Android应用程序的流程和相关技巧,需要的朋友们跟着学习下。
    2019-03-03
  • C#位运算符的基本用法介绍

    C#位运算符的基本用法介绍

    这篇文章介绍了C#位运算符的基本用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 利用C#实现AOP常见的几种方法详解

    利用C#实现AOP常见的几种方法详解

    AOP面向切面编程(Aspect Oriented Programming),是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。下面这篇文章主要给大家介绍了关于利用C#实现AOP常见的几种方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-09-09
  • Unity UGUI的StandaloneInputModule标准输入模块组件使用示例

    Unity UGUI的StandaloneInputModule标准输入模块组件使用示例

    这篇文章主要为大家介绍了Unity UGUI的StandaloneInputModule标准输入模块组件使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • C# 使用 GDI+ 实现添加中心旋转(任意角度)的文字

    C# 使用 GDI+ 实现添加中心旋转(任意角度)的文字

    这篇文章主要介绍了C# 使用 GDI+ 实现添加中心旋转(任意角度)的文字,需要的朋友可以参考下
    2018-04-04
  • C#关键字async/await用法

    C#关键字async/await用法

    在本篇文章里小编给大家整理的是关于C#关键字async/await用法及相关实例,需要的朋友们学习下。
    2019-12-12
  • 详解C#通过反射获取对象的几种方式比较

    详解C#通过反射获取对象的几种方式比较

    本文主要介绍了C#通过反射获取对象的几种方式比较,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • C# 利用Autofac批量接口注入依赖的问题小结

    C# 利用Autofac批量接口注入依赖的问题小结

    这篇文章主要介绍了C# 利用Autofac批量接口注入依赖的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • C#备忘录模式(Memento Pattern)实例教程

    C#备忘录模式(Memento Pattern)实例教程

    这篇文章主要介绍了C#备忘录模式(Memento Pattern),以一个支持回退操作的例子讲述了C#备忘模式的实现方法,需要的朋友可以参考下
    2014-09-09

最新评论