You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级ZXing.Net至0.16.9后遇两类错误,求解决BitmapRenderer找不到问题

解决ZXing.Net 0.16.9升级后的BitmapRenderer找不到问题

问题原因

从ZXing.Net 0.16.6升级到0.16.9时,官方将针对System.Drawing的渲染组件从核心包拆分到了独立的ZXing.Windows.Compatibility NuGet包中,同时BitmapRenderer的命名空间从ZXing.Rendering变更为ZXing.Windows.Compatibility,因此原代码中找不到该类型。

解决方案

步骤1:安装ZXing.Windows.Compatibility包

在NuGet包管理器中安装ZXing.Windows.Compatibility包,或通过Package Manager Console执行命令:

Install-Package ZXing.Windows.Compatibility

步骤2:修改代码

有两种修复方式可选:

方式一:保留手动设置Renderer

替换原有的using指令,更新Renderer实例化代码:

using System;
using System.Drawing;
using System.IO;
using ZXing;
using ZXing.QrCode.Internal;
using ZXing.Windows.Compatibility; // 替换原ZXing.Rendering

namespace BarCode.Commons
{
    public class BarCodeUtil
    {
        public static Bitmap GenerateBar(string text, int margin = 0, int width = 1000, int height = 500)
        {
            var bw = new BarcodeWriter<Bitmap>();
            var encOptions = new ZXing.Common.EncodingOptions
            {
                Width = width + (2 * margin),
                Height = height + (2 * margin),
                Margin = margin,
                PureBarcode = true
            };
            encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            bw.Renderer = new BitmapRenderer(); // 现在可正常识别该类型
            bw.Options = encOptions;
            bw.Format = BarcodeFormat.CODE_128;
            Bitmap bm = bw.Write(text);
            return bm;
        }
    }
}

方式二:省略手动设置Renderer(更简洁)

BarcodeWriter<Bitmap>默认已绑定正确的Bitmap渲染器,可直接删除bw.Renderer = new BitmapRenderer();这一行,代码依然正常运行:

using System;
using System.Drawing;
using System.IO;
using ZXing;
using ZXing.QrCode.Internal;

namespace BarCode.Commons
{
    public class BarCodeUtil
    {
        public static Bitmap GenerateBar(string text, int margin = 0, int width = 1000, int height = 500)
        {
            var bw = new BarcodeWriter<Bitmap>();
            var encOptions = new ZXing.Common.EncodingOptions
            {
                Width = width + (2 * margin),
                Height = height + (2 * margin),
                Margin = margin,
                PureBarcode = true
            };
            encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            bw.Options = encOptions;
            bw.Format = BarcodeFormat.CODE_128;
            Bitmap bm = bw.Write(text);
            return bm;
        }
    }
}

内容的提问来源于stack exchange,提问作者Nguyen Quoc Bao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 04:15:33