C# 生成二维码(保存到指定路径)

只需引用包:ZXing.Net

 /// <summary>
        /// 生成二维码方法
        /// </summary>
        /// <param name="text">输入的字符串</param>
        /// <param name="width">二维码宽度</param>
        /// <param name="height">二维码高度</param>
        /// <returns></returns>
        public string QRcode(string text, string width, string height)
        {
            string Response = "";
            try
            {
                BarcodeWriter writer = new BarcodeWriter();
                writer.Format = BarcodeFormat.QR_CODE;
                QrCodeEncodingOptions options = new QrCodeEncodingOptions();
                options.DisableECI = true;
                //设置内容编码
                options.CharacterSet = "UTF-8";
                //将传来的值赋给二维码的宽度和高度
                options.Width = Convert.ToInt32(width);
                options.Height = Convert.ToInt32(height);
                //设置二维码的边距,单位不是固定像素
                options.Margin = 1;
                writer.Options = options;

                Bitmap map = writer.Write(text);
                string di = text + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
                //二维码保存路径
                string path = Path.Combine("C:\\Users\\Administrator\\Desktop\\111", di);
                map.Save(path, ImageFormat.Png);
                map.Dispose();
                Response = "二维码生成成功!";
            }
            catch (Exception)
            {
                Response = "二维码生成失败!";
            }
            return Response;


        }

 

posted on 2021-04-13 11:57  小乐丶  阅读(620)  评论(0编辑  收藏  举报