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?
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 };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With