C# 生成带Logo的二维码及生成二维码

先下载ZXing.dll

using ZXing;
using ZXing.QrCode;
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="text">二维码内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap GenQrCodePic(string text, int width, int height)
{
    BarcodeWriter writer = new BarcodeWriter();
    writer.Format = BarcodeFormat.QR_CODE;
    QrCodeEncodingOptions options = new QrCodeEncodingOptions()
    {
        DisableECI = true,//设置内容编码
        CharacterSet = "UTF-8",  //设置二维码的宽度和高度
        Width = width,
        Height = height,
        Margin = 1//设置二维码的边距,单位不是固定像素
    };

    writer.Options = options;
    Bitmap map = writer.Write(text);
    return map;
}
/// <summary>
/// 生成带Logo的二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public static Bitmap GenQrCodeLogoPic(string text, string logoPath, int width, int height)
{
    Bitmap logo = new Bitmap(logoPath);
    //计算插入图片的大小和位置
    int middleW = Math.Min((int)(width / 4), logo.Width);
    int middleH = Math.Min((int)(height / 4), logo.Height);
    int middleL = (width - middleW) / 2;
    int middleT = (height - middleH) / 2;
    构造二维码写码器
    Bitmap bmpimg = GenQrCodePic(text, width, height);
    using (Graphics g = Graphics.FromImage(bmpimg))
    {
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.DrawImage(bmpimg, 0, 0, width, height);
        //白底将二维码插入图片
        g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
        g.DrawImage(logo, middleL, middleT, middleW, middleH);
    }
    return bmpimg;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值