Using the generic type BarcodeReader<T> requires 1 type arguments
Asked Answered
C

1

2

I was trying to add QR code generator in my WPF app using instruction provided here: https://www.kailashsblogs.com/2020/05/generate-qr-code-in-wpf.html?m=0&fbclid=IwAR0CMMRGwE1eH-ASW4jVzML1oyEszLeSWAosK2QfuwOKCot_665O2aR1tEA

I installed ZXing.Net. But after copying code to my MainWindow.xaml.cs I was given errors "using the generic type 'barcodereader<t> ' requires 1 type arguments"

    private void BtnConvert_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            System.Drawing.Image img = null;
            BitmapImage bimg = new BitmapImage();
            using (var ms = new MemoryStream())
            {
                BarcodeWriter writer; <- HERE
                writer = new ZXing.BarcodeWriter() { Format = BarcodeFormat.QR_CODE }; <-HERE
                writer.Options.Height = 80;
                writer.Options.Width = 280;
                writer.Options.PureBarcode = true;
                img = writer.Write(this.txtbarcodecontent.Text);
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Position = 0;
                bimg.BeginInit();
                bimg.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                bimg.CacheOption = BitmapCacheOption.OnLoad;
                bimg.UriSource = null;
                bimg.StreamSource = ms;
                bimg.EndInit();
                this.imgbarcode.Source = bimg;// return File(ms.ToArray(), "image/jpeg");  
                this.tbkbarcodecontent.Text = this.txtbarcodecontent.Text;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using ZXing;
using System.Drawing;

How can I repair it?

Conferva answered 18/5, 2023 at 14:39 Comment(1)
I do not see any reference to BarcodeReader in your sample code. Can you point out the offending line number as indicated by the compiler?Cardboard
W
3

If you use ZXing.Net with .Net Core / .Net Standard you have to use one of the different binding packages. They contain specific BarcodeReader and BarcodeWriter implementations for the different image libraries. https://www.nuget.org/packages?q=zxing.bindings That's by design because .Net core doesn't include a bitmap implementation in the core package.

For example, add the package https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility and change the line writer = new ZXing.BarcodeWriter() { Format = BarcodeFormat.QR_CODE }; to writer = new ZXing.Windows.Compatibility.BarcodeWriter() { Format = BarcodeFormat.QR_CODE };

Wonderful answered 25/5, 2023 at 18:20 Comment(1)
This should be the accepted answer because it works. Very helpful. BTW: same applies to BarcodeReader().Mosa

© 2022 - 2024 — McMap. All rights reserved.