项目中需要生成二维码,方法比较多,可以采用JS插件,也可以采用第三方插件后台生成二维码,在后台方法中可以采用QRCode或者GmaQrCode,现在介绍一种C#在后台生成二维码的方法:
/// <summary>
/// 获取二维码
/// </summary>
/// <param name="codeString">编码字符</param>
/// <returns>二维码地址</returns>
public static string ReturnGmaQrCode(string codeString)
{
try
{
using (var ms = new MemoryStream())
{
//const string stringtest = "中国inghttps://cache.yisu.com/upload/information/20200311/60/235858.html";
img.Save(path);
return path;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 获取二维码
/// </summary>
/// <param name="strContent">待编码的字符</param>
/// <param name="ms">输出流</param>
///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>
public static bool CreateGmaQrCodeHelper(string strContent, MemoryStream ms)
{
try
{
//误差校正水平
const ErrorCorrectionLevel ecl = ErrorCorrectionLevel.M;
//待编码内容
var content = strContent;
//空白区域
const QuietZoneModules quietZones = QuietZoneModules.Two;
//大小
const int moduleSize = 12;
var encoder = new QrEncoder(ecl);
QrCode qr;
//对内容进行编码,并保存生成的矩阵
if (encoder.TryEncode(content, out qr))
{
var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, quietZones));
render.WriteToStream(qr.Matrix, ImageFormat.Png, ms);
}
else
{
return false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return true;
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。