C# 生成二维码
Posted 322829
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 生成二维码相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="content">二维码内容</param>
/// /// <param name="fileName">文件名称</param>
/// <returns></returns>
private string GenerateQRCode(string content, string fileName = "")
if (fileName == "")
fileName = content;
string PicPath = PathExt.ConvertAbsolutePath("/UploadFile/Barcode/" + fileName + ".png");
//创建路径
string rootPath = PathExt.ConvertAbsolutePath("/UploadFile/Barcode"); //rootPath
if (!System.IO.Directory.Exists(rootPath))
//不存在就添加路径(文件夹)
System.IO.Directory.CreateDirectory(rootPath);
try
QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode;
encoder.TryEncode(content, out qrCode);
GraphicsRenderer gRenderer = new GraphicsRenderer(
new FixedModuleSize(12, QuietZoneModules.Two),
Brushes.Black, Brushes.White);
MemoryStream ms = new MemoryStream();
gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
FileStream fs = new FileStream(PicPath, FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs);
w.Write(ms.ToArray());
fs.Close();
ms.Close();
return "/UploadFile/Barcode/" + fileName + ".png";
catch (Exception)
return "";
以上是关于C# 生成二维码的主要内容,如果未能解决你的问题,请参考以下文章