我是靠谱客的博主 眯眯眼柜子,最近开发中收集的这篇文章主要介绍C# ThoughtWorks.QRCode.Code 二维码应用程序,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#region 生成二维码(可自定义类型)
        /// <summary>
        /// 生成二维码(可自定义类型)
        /// <param name="filename">二维码名称</param>
        /// <param name="qrStr">要生成的字符</param>
        /// <param name="qrPath">二维码存放路径</param>
        /// <returns></returns>
        /// </summary>
        public static string GenerateQRCode(string filename, string qrStr, string qrPath)
        {
            string result = "";
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            #region 参数赋值
            string bgc = "#FFFFFF"; //默认白色
            string fgc = "#000000"; //默认黑色
            int num = 200;//二维码大小
            //二维码存储版本,值越大可含信息量越大,范围0-40(0:自动适应版本,压缩的信息量将会根据实际传入值确定,只有最高上限的控制,
            //而且图片的大小将会根据信息量自动缩放。1-40:有固定的信息量上限,而且图片的大小会固定在一个大小上,不会根据信息量的多少而变化。)
            qrCodeEncoder.QRCodeVersion = 20;
            //每个小方格的宽度
            qrCodeEncoder.QRCodeScale = 2;
            //前景色
            qrCodeEncoder.QRCodeForegroundColor = ColorTranslator.FromHtml(fgc);
            //背景色
            qrCodeEncoder.QRCodeBackgroundColor = ColorTranslator.FromHtml(bgc);
            //编码方式
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //纠错等级(容错率)
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
            #endregion
            FileStream fs = null;
            int size = num; //二维码目标尺寸
            Bitmap bitmap_code = null; //二维码位图
            try
            {
                //根据字符内容生成二维码图片
                bitmap_code = qrCodeEncoder.Encode(qrStr);
                #region 根据设定的目标尺寸,调整二维码QRCodeScale设置
                if (size > 0)
                {
                    #region 当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸(二维码尺寸过小,逐步增大)
                    while (bitmap_code.Width < size)
                    {
                        qrCodeEncoder.QRCodeScale++;
                        Bitmap bitmap_New = qrCodeEncoder.Encode(qrStr);
                        if (bitmap_New.Width <= size) //过小,继续增大
                        {
                            bitmap_code = new Bitmap(bitmap_New);
                            bitmap_New.Dispose();
                            bitmap_New = null;
                        }
                        else //过大,减小一个尺寸,采用上一尺寸
                        {
                            qrCodeEncoder.QRCodeScale--; //新尺寸未采用
                            bitmap_New.Dispose();
                            bitmap_New = null;
                            break;
                        }
                    }
                    #endregion
                    #region 当设定目标图片尺寸小于生成的尺寸时,逐步减小方格尺寸(二维码尺寸过大,逐步减小)
                    while (bitmap_code.Width > size && qrCodeEncoder.QRCodeScale > 1)
                    {
                        qrCodeEncoder.QRCodeScale--;
                        Bitmap bitmap_New = qrCodeEncoder.Encode(qrStr);
                        bitmap_code = new Bitmap(bitmap_New);
                        bitmap_New.Dispose();
                        bitmap_New = null;
                        if (bitmap_code.Width <= size)
                        {
                            break;
                        }
                    }
                    #endregion
                    #region 如果目标尺寸大于生成的图片尺寸,则为图片增加白边(加减后,若二维码尺寸仍小,可增加白边)
                    if (bitmap_code.Width <= size)
                    {
                        #region 当目标图片尺寸大于二维码尺寸时,将二维码绘制在目标尺寸白色画布的中心位置
                        if (bitmap_code.Width < size)
                        {
                            Bitmap panel = new Bitmap(size, size);
                            Graphics graphic0 = Graphics.FromImage(panel);
                            int p_left = 0;
                            int p_top = 0;
                            if (bitmap_code.Width <= size) //如果原图比目标形状宽
                            {
                                p_left = (size - bitmap_code.Width) / 2;
                            }
                            if (bitmap_code.Height <= size)
                            {
                                p_top = (size - bitmap_code.Height) / 2;
                            }
                            //将生成的二维码图像粘贴至绘图的中心位置
                            graphic0.DrawImage(bitmap_code, p_left, p_top, bitmap_code.Width, bitmap_code.Height);
                            bitmap_code = new Bitmap(panel);
                            panel.Dispose();
                            panel = null;
                            graphic0.Dispose();
                            graphic0 = null;
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
                #region 保存图片
                string filepath = HttpContext.Current.Server.MapPath(qrPath);
                //路径
                if (!Directory.Exists(filepath))
                {
                    Directory.CreateDirectory(filepath);
                }
                //文件
                string filepathall = filepath + filename;
                fs = new FileStream(filepathall, FileMode.OpenOrCreate, FileAccess.Write);
                bitmap_code.Save(fs, ImageFormat.Png);
                #endregion
                result = qrPath.Replace("\", "/") + filename;
            }
            catch (IOException)
            {
                result = "";
            }
            catch (Exception)
            {
                result = "";
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (bitmap_code != null)
                {
                    bitmap_code.Dispose();
                    bitmap_code = null;
                }
            }
            return result;
        }
#endregion

最后

以上就是眯眯眼柜子为你收集整理的C# ThoughtWorks.QRCode.Code 二维码应用程序的全部内容,希望文章能够帮你解决C# ThoughtWorks.QRCode.Code 二维码应用程序所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部