栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
    }
    /// 
    /// 生成二维码方法
    /// 
    /// 输入的字符串
    /// 二维码宽度
    /// 二维码高度
    /// 
    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;
}





  
  Index
  
  
  
  



  
    

高度:宽度:

4.效果:

以上就是C# 根据字符串生成二维码的实例代码的详细内容,更多关于C# 根据字符串生成二维码的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/121944.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号