WPF使用ZXing.Net生成二维码遇泛型参数错误的修复咨询
修复ZXing.Net在WPF中生成二维码的“使用泛型类型‘BarcodeReader’需要1个类型参数”错误
问题场景
按照旧教程在WPF应用中集成ZXing.Net实现二维码生成功能,安装最新版ZXing.Net后,复制教程中的代码到MainWindow.xaml.cs,出现编译错误:使用泛型类型‘BarcodeReader
相关代码片段:
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; // 错误相关行 writer = new ZXing.BarcodeWriter() { Format = BarcodeFormat.QR_CODE }; // 错误相关行 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; 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;
错误原因
旧教程使用的是ZXing.Net的旧版本,当时BarcodeWriter是非泛型类;而新版ZXing.Net中BarcodeWriter已改为泛型类BarcodeWriter<TOutput>,必须指定输出类型参数,因此直接实例化BarcodeWriter会触发编译错误。
修复方案
方案1:指定泛型类型参数(兼容原有System.Drawing.Image逻辑)
将BarcodeWriter改为BarcodeWriter<System.Drawing.Image>,明确指定输出类型为System.Drawing.Image,修改后的代码如下:
private void BtnConvert_Click(object sender, RoutedEventArgs e) { try { System.Drawing.Image img = null; BitmapImage bimg = new BitmapImage(); using (var ms = new MemoryStream()) { // 指定泛型参数为System.Drawing.Image BarcodeWriter<System.Drawing.Image> writer; writer = new ZXing.BarcodeWriter<System.Drawing.Image>() { Format = BarcodeFormat.QR_CODE, Options = new ZXing.Common.EncodingOptions { Height = 80, Width = 280, 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; this.tbkbarcodecontent.Text = this.txtbarcodecontent.Text; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
方案2:使用WPF适配的BarcodeWriterPixelData(推荐,避免System.Drawing依赖)
ZXing.Net提供了针对WPF的BarcodeWriterPixelData,可以直接生成像素数据,无需依赖System.Drawing,更适合WPF环境。修改后的代码如下:
首先确保安装ZXing.Net.Bindings.Wpf NuGet包(如果未安装),然后修改代码:
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using System.IO; using ZXing; using ZXing.Common; private void BtnConvert_Click(object sender, RoutedEventArgs e) { try { BitmapImage bimg = new BitmapImage(); var writer = new BarcodeWriterPixelData { Format = BarcodeFormat.QR_CODE, Options = new EncodingOptions { Height = 80, Width = 280, PureBarcode = true, Margin = 0 } }; var pixelData = writer.Write(this.txtbarcodecontent.Text); // 将PixelData转换为BitmapImage using (var ms = new MemoryStream()) { var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height); var bitmapData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length); bitmap.UnlockBits(bitmapData); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; bimg.BeginInit(); bimg.CacheOption = BitmapCacheOption.OnLoad; bimg.StreamSource = ms; bimg.EndInit(); } this.imgbarcode.Source = bimg; this.tbkbarcodecontent.Text = this.txtbarcodecontent.Text; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
注意事项
- 如果使用方案2,需要安装
ZXing.Net.Bindings.WpfNuGet包,该包专门为WPF优化,减少不必要的依赖。 - 新版ZXing.Net中,
EncodingOptions需要通过ZXing.Common命名空间引用,确保代码中添加using ZXing.Common;。
内容的提问来源于stack exchange,提问作者Jessica Sztefka
相关产品推荐
相关产品推荐

