WPF制作二维码和条形码
2021-01-22 08:13
标签:one express exce iter rect def ons cep panel 1. 下载 zxing.dll,并添加引用; 2. 界面代码 using System; WPF制作二维码和条形码 标签:one express exce iter rect def ons cep panel 原文地址:https://www.cnblogs.com/siyunianhua/p/12084333.html
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Niumag_WFF"
mc:Ignorable="d"
Title="QRCode" Height="450" Width="650">
3. 后台代码
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using ZXing;
using ZXing.Common;
namespace Niumag_WFF
{
///
/// QRCode.xaml 的交互逻辑
///
public partial class QRCode : Window
{
public QRCode()
{
InitializeComponent();
GeneratorBar(txtbox.Text);
GeneratorQR(txtbox.Text);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
imageQR.Source = null;
try
{
GeneratorQR(txtbox.Text);
}
catch (Exception ex)
{
txtbox.Text = ex.Message;
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
imageBar.Source = null;
try
{
GeneratorBar(txtbox.Text);
}
catch(Exception ex)
{
txtbox.Text = ex.Message + "(code39合法字符集 [0-9A-Z+-*/%. ] 共43个)";
}
}
// 生成二维码
private Image GeneratorQR(string msg)
{
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE
};
writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); // 编码问题
writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
int codeSizeInPixels = 256; // 设置图片长宽
writer.Options.Height = codeSizeInPixels;
writer.Options.Width = codeSizeInPixels;
writer.Options.Margin = 0; // 设置边框
BitMatrix bm = writer.Encode(msg);
Bitmap img = writer.Write(bm);
imageQR.Source = BitmapToBitmapImage(img);
return img;
}
// 生成条形码
private Image GeneratorBar(string msg)
{
MultiFormatWriter mutiWriter = new MultiFormatWriter();
BitMatrix bm = mutiWriter.encode(msg, BarcodeFormat.CODE_39, 350, 256);
Bitmap img = new BarcodeWriter().Write(bm);
imageBar.Source = BitmapToBitmapImage(img);
return img;
}
// Bitmap --> BitmapImage
public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
stream.Position = 0;
BitmapImage result = new BitmapImage();
result.BeginInit();
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
return result;
}
}
// BitmapImage --> Bitmap
public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
Bitmap bitmap = new Bitmap(outStream);
return new Bitmap(bitmap);
}
}
}
}