Search code examples
c#barcodezxingzxing.net

ZXing The type or namespace name 'BitmapRenderer' could not be found


I just updated ZXing.Net from version 0.16.6 to 0.16.9, and currently, I am encountering the following two errors:

  • Using the generic type 'BarcodeWriter<TOutput>' requires 1 type arguments

  • The type or namespace name 'BitmapRenderer' could not be found (are you missing a using directive or an assembly reference?)

using System;
using System.Drawing;
using System.IO;
using ZXing;
using ZXing.QrCode.Internal;
using 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(); // The first error is here
            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(); // The second error is here
            bw.Options = encOptions;
            bw.Format = BarcodeFormat.CODE_128;
            Bitmap bm = bw.Write(text);
            return bm;
        }
    }
}

For the first error, it seems that I need to specify one output type, and I have modified it to the following command:

var bw = new BarcodeWriter<Bitmap>();

However, I don't know how to resolve the second error.


Solution

  • I assume that you are using .Net Core or .Net 5.0 or above. In that case, the base nuget package ZXing.Net doesn't include a BarcodeWriter implementation for a specific bitmap implementation. Only the old classic .Net framework 4.x includes the Bitmap classes by default.

    If my assumption is correct, you have to select one of the binding packages: https://www.nuget.org/packages?q=Zxing.Net.Binding Perhaps this specific one should fit your needs: https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility (ZXing.Windows.Compatibility.BarcodeWriter) Change your code to

    var bw = new ZXing.Windows.Compatibility.BarcodeWriter();
    

    or add the following "using" statement at the top of your file

    using ZXing.Windows.Compatibility;