C#zxing生成二维码

本示例是zxing生成二维码信息,需要应用zxing。

    public static class QRCode
    {
        public static Bitmap GetBitmap(string sContent, int iWidth, int iHeight)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions();
            options.DisableECI = true;
            //设置内容编码
            options.CharacterSet = "UTF-8";
            //设置二维码的宽度和高度
            options.Width = iWidth;
            options.Height = iHeight;
            //设置二维码的边距,单位不是固定像素
            options.Margin = 0; // 设置周围空白边距
            writer.Options = options;

            return writer.Write(sContent);
        }

        public static string GetBitmapBase64String(string sContent, int iWidth, int iHeight)
        {
            Bitmap imgBitmap = GetBitmap(sContent, iWidth, iHeight);

            string filename = System.Windows.Forms.Application.StartupPath + "\\QrCodeTemp.jpg";
            imgBitmap.Save(filename, ImageFormat.Jpeg);

            MemoryStream ms = new MemoryStream();
            imgBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            imgBitmap.Dispose();
            string strBaser64 = Convert.ToBase64String(arr);
            return strBaser64;
        }
    }
}

下载地址:

「zxing.dll」https://www.aliyundrive.com/s/CtoCqKCwkcy

posted @ 2022-05-24 16:27  恋上微笑的天使  阅读(736)  评论(0编辑  收藏  举报