ASP.NET CORE 中 生成二维码 QRcode

nuget里安装zxing.net包,或者运行命令:dotnet add package zxing.net

using ZXing.QrCode;

private static byte[] DrawQrCode(string Msg)
{
    byte[] data;
    var qrCodeWriter = new ZXing.BarcodeWriterPixelData
    {
    Format = ZXing.BarcodeFormat.QR_CODE,
    Options = new QrCodeEncodingOptions
    {
        //DisableECI = true,//设置内容编码
        Height = 300,
        Width = 300,
        Margin = 1,
        CharacterSet = "UTF-8"
    }
    };
    var pixelData = qrCodeWriter.Write(Msg);
    using (Bitmap bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
    {
    using (MemoryStream ms = new MemoryStream())
    {
        var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        try
        {
        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image    
        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
        }
        finally
        {
        bitmap.UnlockBits(bitmapData);
        }
        // save to stream as PNG    
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        data = ms.ToArray();
    }
    }
    return data;
}


2020-06-19 ASP.NET CORE

发布评论