.net 生成二维码图片
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns>Bitmap对象</returns>
public Image GenerateQRCode(string text, int width, int height)
{
using QRCodeGenerator qrGenerator = new QRCodeGenerator();
using QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
using QRCoder.BitmapByteQRCode bitmapByteQRCode = new BitmapByteQRCode(qrCodeData);
var datas = bitmapByteQRCode.GetGraphic(5, "#000000", "#ffffff");
Image image = Image.Load(datas);
image.Mutate(ctx =>
{
ctx.Resize(width, height);
});
return image;
}
/// <summary>
/// 生成带Logo的二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="logoImage">Logo</param>
/// <returns>Bitmap对象</returns>
public Image GenerateQRCodeWithLogo(string text, int width, int height, Image logoImage)
{
var qrCodeImage = GenerateQRCode(text, width, height);
// 计算插入 logo 的位置
int x = (width - logoImage.Width) / 2;
int y = (height - logoImage.Width) / 2;
qrCodeImage.Mutate(ctx => {
ctx.DrawImage(logoImage, new Point(x, y), 1);
});
return qrCodeImage;
}