本帖最后由 箭头_Row 于 2025-7-10 00:30 编辑
最初看到Gile的開源庫:關於圖中生成二維碼的程序
具體做了如下優化:
一、下載后發現其引用的QRCoder庫是離線庫,故優化項目文件:InsertQRCodeBlock.csproj
二、取消項目:AutoCADQRCoder,合併至:InsertQRCodeBlock。
三、漢化了對話框。
生成圖形如圖,另源碼是用WINFORM寫的界面,需優化參數設置,因其沒有做到保存上一次的參數設置。
詳見代碼:
優化后庫:AutocadQRCoder: All the magic of AutocadQRCoder comes from the basic use of QRCoder library to create AutoCAD entities (block or hatch) featuring QR Codes.
Gile的開源庫:gileCAD/AutocadQRCoder: QRCoder for AutoCAD
网友答: 为什么不是用我博客,
而且.csproj应该叫项目文件,
所谓的头文件是函数声明,是C/C++才有...网友答: 本帖最后由 箭头_Row 于 2025-7-10 00:29 编辑
優化了下語法,計時器改名了,博客上可以指定大小新建二維碼:
网友答:
网友答:

參數設置為10,只支持119個字節。
最初看到Gile的開源庫:關於圖中生成二維碼的程序
具體做了如下優化:
一、下載后發現其引用的QRCoder庫是離線庫,故優化項目文件:InsertQRCodeBlock.csproj
二、取消項目:AutoCADQRCoder,合併至:InsertQRCodeBlock。
三、漢化了對話框。
生成圖形如圖,另源碼是用WINFORM寫的界面,需優化參數設置,因其沒有做到保存上一次的參數設置。
詳見代碼:
優化后庫:AutocadQRCoder: All the magic of AutocadQRCoder comes from the basic use of QRCoder library to create AutoCAD entities (block or hatch) featuring QR Codes.
Gile的開源庫:gileCAD/AutocadQRCoder: QRCoder for AutoCAD
网友答: 为什么不是用我博客,
而且.csproj应该叫项目文件,
所谓的头文件是函数声明,是C/C++才有...网友答: 本帖最后由 箭头_Row 于 2025-7-10 00:29 编辑
優化了下語法,計時器改名了,博客上可以指定大小新建二維碼:

- namespace JoinBox
- {
- public class CmdTestQrCodeGeneratorClass
- {
- [CommandMethod(nameof(CmdTest_QRCodeGenerator))]
- public void CmdTest_QRCodeGenerator()
- {
- // var msg = "二维码测试";
- var msg = "https://www.cnblogs.com/JJBox";
- // msg = "https://www.cnblogs.com/JJBox/p/14485956.html";
- QrCodeEncoder qr = new(msg);
- qr.Build();
- var bits = qr.ModuleMatrix;
- //这里是新建图片到桌面上
- qr.BuildBitmap();
- var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- var file = Path.Combine(desktop, "QRCode.png");
- qr.SaveBitmap(file);
- if (bits is null)
- return;
- using DBTrans tr = new();
- CreateQrCodeHatch(tr, bits, size: 50); //不需要背景
- }
- /// <summary>
- /// 创建二维码填充
- /// </summary>
- /// <param name="tr"></param>
- /// <param name="bits">二维码矩阵</param>
- /// <param name="needBackground">是否需要背景</param>
- /// <param name="size">尺寸</param>
- private static void CreateQrCodeHatch(
- DBTrans tr,
- IEnumerable<BitArray> bits,
- bool needBackground = true,
- int size = 100
- )
- {
- const string hatchName = "Solid";
- HatchInfo? hatchBackground = null;
- if (needBackground)
- {
- var hatchColor = Color.FromRgb(255, 255, 255); //填充背景_白色
- //var hatchColor = Color.FromRgb(255, 0, 0); //填充背景_红色
- hatchBackground = new HatchInfo(false).Mode1PreDefined(hatchName).Action(ent => ent.Color = hatchColor);
- }
- //填充前景_其他颜色
- // Color hatch2Color = Color.FromRgb(160, 200, 58); //绿色
- Color hatch2Color = Color.FromRgb(1, 1, 1); //黑色
- var hatch = new HatchInfo(false).Mode1PreDefined(hatchName).Action(ent => ent.Color = hatch2Color);
- ChangeHatch(bits, hatch, hatchBackground, size);
- hatch.Build(tr.CurrentSpace);
- hatchBackground?.Build(tr.CurrentSpace);
- //删除生成的边界
- hatch.EraseBoundary();
- hatchBackground?.EraseBoundary();
- }
- /// <summary>
- /// 修改填充
- /// </summary>
- private static void ChangeHatch(
- IEnumerable<BitArray> bits,
- HatchInfo hatch,
- HatchInfo? hatchBackground,
- int size = 100
- )
- {
- DBTrans tr = DBTrans.Top;
- //设置多段线的凸度,数量和点集一样,而且自动赋值0
- var bulges = new DoubleCollection(new double[5]);
- int row = 0;
- foreach (var bitArray in bits)
- {
- for (int column = 0; column < bitArray.Length; column++)
- {
- //创建矩形框5个点,否则填充是不闭合的
- var p1 = new Point2d(column * size, row * size);
- var p2 = new Point2d(p1.X + size, p1.Y);
- var p3 = new Point2d(p2.X, p1.Y + size);
- var p4 = new Point2d(p1.X, p3.Y);
- Point2dCollection pts = [p1, p2, p3, p4];
- if (bitArray.Get(column))
- hatch.AppendLoop(pts, bulges, tr.CurrentSpace); //前景黑块
- else
- hatchBackground?.AppendLoop(pts, bulges, tr.CurrentSpace); //背景白色
- }
- row--; //换行
- }
- }
- }
- }

- using System.Drawing.Imaging;
- using QRCoder;
- using Image = System.Drawing.Image;
- namespace JoinBox
- {
- public class QrCodeEncoder
- {
- public QRCode? QrCode;
- public List<BitArray>? ModuleMatrix;
- private readonly string _content;
- private readonly int _version;
- private readonly System.Drawing.Color _qrBitmapColor1 = System.Drawing.Color.Black;
- private readonly System.Drawing.Color _qrBitmapColor2 = System.Drawing.Color.White;
- public Image? QrBitmap;
- private static Dictionary<string, ImageFormat>? _extDict;
- /// <summary>
- /// 创建二维码
- /// </summary>
- /// <param name="codeContent">信息</param>
- /// <param name="version">版本1~40,如果是0会依照内容缩放图片</param>
- /// <returns></returns>
- public QrCodeEncoder(string codeContent, int version = 10)
- {
- _content = codeContent;
- //当参数一是: https://www.cnblogs.com/JJBox/p/14485956.html
- //测试手机QQ,参数2为5以下不能识别.
- //如果参数1内容多的话,参数2偏大则更容易识别.
- //(是否设置为一个比例而不是一个固定版本?)
- _version = version;
- }
- /// <summary>
- /// 构建
- /// </summary>
- public void Build()
- {
- var generator = new QRCodeGenerator();
- var data = generator.CreateQrCode(
- _content,
- QRCodeGenerator.ECCLevel.H, //这里设置容错率的一个级别
- false, //手机QQ能不能扫描就看这里了
- false, //手机QQ能不能扫描就看这里了
- QRCodeGenerator.EciMode.Utf8,
- _version
- );
- QrCode = new QRCode(data);
- ModuleMatrix = data.ModuleMatrix;
- }
- /// <summary>
- /// 构建位图
- /// </summary>
- /// <param name="pixel">像素点大小</param>
- /// <param name="size">图标尺寸</param>
- /// <param name="border">图标边框厚度</param>
- /// <param name="whiteEdge">二维码白边</param>
- /// <returns></returns>
- public void BuildBitmap(int pixel = 5, int size = 5, int border = 0, bool whiteEdge = false)
- {
- if (QrCode == null)
- throw new ArgumentNullException(nameof(QrCode));
- QrBitmap = QrCode.GetGraphic(pixel, _qrBitmapColor1, _qrBitmapColor2, null, size, border, whiteEdge);
- }
- /// <summary>
- /// 保存位图
- /// </summary>
- /// <param name="imgPath">带文件名后缀的完整路径</param>
- /// <returns></returns>
- public void SaveBitmap(string imgPath)
- {
- if (QrBitmap is null)
- throw new ArgumentNullException(nameof(QrBitmap));
- //反射ImageFormat获取匹配后缀类型
- if (!Path.HasExtension(imgPath))
- throw new ArgumentNullException($"路径没有后缀");
- var ext = Path.GetExtension(imgPath).ToLower();
- ext = ext.Replace(".", string.Empty).Replace("jpg", "jpeg");
- if (_extDict == null) //仅运行程序的第一次执行
- {
- _extDict = new Dictionary<string, ImageFormat>();
- var image = typeof(ImageFormat);
- var properties = image.GetProperties();
- foreach (var item in properties)
- {
- var name = item.Name.ToLower();
- if (name.Contains("guid") || _extDict.ContainsKey(name))
- continue;
- if (item.GetValue(image, null) is ImageFormat imageFormat)
- _extDict.Add(name, imageFormat);
- }
- }
- if (_extDict.TryGetValue(ext, out var value))
- QrBitmap.Save(imgPath, value);
- }
- }
- }

- int byteCount = Encoding.UTF8.GetByteCount(TextInputBox.Text);
- if (byteCount > 119)
- {
- Acaop.ShowAlertDialog($"当前字节数{byteCount},\n字节数超过了119个!");
- return;
- }
參數設置為10,只支持119個字節。