1. 下载 zxing.dll,并添加引用;
2. 界面代码
<Window x:Class="Niumag_WFF.QRCode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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">
<Grid Margin="5">
<StackPanel Orientation="Vertical" Margin="5">
<TextBox Name="txtbox" Text="0123456789" Width="600" FontSize="15" Foreground="Green" Margin="5" Padding="5"/>
<StackPanel Orientation="Horizontal" Margin="5">
<Image Name="imageQR" Width="256" Height="256" Margin="5"/>
<Image Name="imageBar" Width="350" Height="256" Margin="5"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="Generator QR" FontSize="28" Width="200" Foreground="Blue" Background="Transparent" BorderBrush="SkyBlue" Margin="5" Click="Button_Click"/>
<Button Content="Generator Bar" FontSize="28" Width="200" Foreground="Blue" Background="Transparent" BorderBrush="SkyBlue" Margin="5" Click="Button_Click_1"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>
3. 后台代码
using System;
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
{
/// <summary>
/// QRCode.xaml 的交互逻辑
/// </summary>
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);
}
}
}
}