You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.2 KiB
72 lines
2.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using QRCoder;
|
|
|
|
namespace Common.Helper.Code
|
|
{
|
|
public class CodeHelper
|
|
{
|
|
|
|
private static Bitmap QcCode(string text)
|
|
{
|
|
|
|
var qrGenerator = new QRCodeGenerator();
|
|
|
|
var qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
|
|
|
|
var qrCode = new QRCode(qrCodeData);
|
|
|
|
return qrCode.GetGraphic(20, Color.Black, Color.White, new Bitmap(Path.Combine(Directory.GetCurrentDirectory(), "Image", "Logo.jpg")));
|
|
}
|
|
|
|
private static Bitmap QcCodeTitle(string text,string title)
|
|
{
|
|
var qrCodeImage = QcCode(text);
|
|
var bitmap = new Bitmap(500, 450);
|
|
var g = Graphics.FromImage(bitmap);
|
|
var font = new Font("SimSun", 24, FontStyle.Regular);
|
|
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
|
g.Clear(Color.White);
|
|
g.DrawImage(qrCodeImage, 50, 0, 400, 400);
|
|
var solidBrush = new SolidBrush(Color.Black);
|
|
g.DrawString(title, font, solidBrush, 190, 400);
|
|
g.Dispose();
|
|
qrCodeImage.Dispose();
|
|
return bitmap;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取二维码
|
|
/// </summary>
|
|
/// <param name="text">文本</param>
|
|
/// <param name="title"></param>
|
|
/// <param name="filePath">生成文件地址</param>
|
|
public static void QcCodeForFilePath(string text, string title, string filePath)
|
|
{
|
|
|
|
var map = QcCodeTitle(text,title);
|
|
map.Save(filePath + title, ImageFormat.Png);
|
|
map.Dispose();
|
|
}
|
|
/// <summary>
|
|
/// 获取带标题的二维码
|
|
/// </summary>
|
|
/// <param name="text"></param>
|
|
/// <param name="title"></param>
|
|
/// <returns></returns>
|
|
|
|
public static string QcCodeBase64(string text,string title)
|
|
{
|
|
using var ms = new MemoryStream();
|
|
var map = QcCodeTitle(text,title);
|
|
map.Save(ms, ImageFormat.Png);
|
|
map.Dispose();
|
|
return Convert.ToBase64String(ms.GetBuffer());
|
|
}
|
|
|
|
}
|
|
}
|
|
|