使用 QRCoder 实现生成二维码

1. Nuget 安装 QRCoder

2. 调用 QRCoder 生成二维码的 Bitmap 对象

		/// <summary>
		/// 把文字编码成二维码
		/// </summary>
		/// <param name="text">需要编码的文字内容</param>
		/// <param name="version">版本 1 ~ 40</param>
		/// <param name="pixel">像素点大小</param>
		/// <param name="iconsize">图标尺寸</param>
		/// <param name="iconboder">图标边框厚度</param>
		/// <param name="whiteEdge">二维码白边</param>
		/// <returns></returns>
		public static Bitmap Encode(string text, int version, int pixel, int iconsize, int iconboder, bool whiteEdge)
		{
			QRCoder.QRCodeGenerator qrCodeGenerator = new QRCoder.QRCodeGenerator();

			QRCoder.QRCode code = new QRCoder.QRCode(qrCodeGenerator.CreateQrCode(
				text,
				QRCoder.QRCodeGenerator.ECCLevel.M,
				true,
				true,
				QRCoder.QRCodeGenerator.EciMode.Utf8,
				version));

			Bitmap icon = new Bitmap(iconsize, iconsize, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, icon, iconsize, iconboder, whiteEdge);

			return bmp;
		}

3. 生成二维码并绑定到 Image(WPF)控件中

<Window x:Class="NT.TestQRCode.MainWindow"
        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:NT.TestQRCode"
        mc:Ignorable="d"
        Title="二维码编码测试" Height="600" Width="1000" ResizeMode="NoResize">
	<Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="1*"/>
			<ColumnDefinition Width="1*"/>
		</Grid.ColumnDefinitions>
		<Grid Grid.Row="0" Grid.Column="0">
			<Border Grid.Row="0" Grid.Column="0" Width="400" Height="400" Margin="10" BorderThickness="1" BorderBrush="Gray">
				<Image x:Name="imgQRCode" VerticalAlignment="Center" HorizontalAlignment="Center"/>
			</Border>
		</Grid>
		<Grid Grid.Row="0" Grid.Column="1" Margin="10" Width="400" Height="400">
			<Grid.RowDefinitions>
				<RowDefinition Height="40"/>
				<RowDefinition Height="40"/>
				<RowDefinition Height="40"/>
				<RowDefinition Height="40"/>
				<RowDefinition Height="40"/>
				<RowDefinition Height="40"/>
			</Grid.RowDefinitions>
			
			<TextBlock Text="请输入需要编码的文字:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"/>
			
			<TextBox x:Name="tbxText" Grid.Row="1" Grid.Column="0" VerticalContentAlignment="Center">
				<TextBox.Resources>
					<VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Left">
						<VisualBrush.Visual>
							<TextBlock FontStyle="Italic" Text="请输入需要编码的文字"/>
						</VisualBrush.Visual>
					</VisualBrush>
				</TextBox.Resources>
				<TextBox.Style>
					<Style TargetType="TextBox">
						<Style.Triggers>
							<Trigger Property="Text" Value="{x:Null}">
								<Setter Property="Background" Value="{StaticResource HintText}"/>
							</Trigger>
							<Trigger Property="Text" Value="">
								<Setter Property="Background" Value="{StaticResource HintText}"/>
							</Trigger>
						</Style.Triggers>
					</Style>
				</TextBox.Style>
			</TextBox>

			<Button x:Name="btnRefresh"
					Grid.Row="3"
					Grid.Column="0"
					Width="150"
					Content="刷新二维码"
					HorizontalAlignment="Left"
					Click="btnRefresh_Click" />
		</Grid>
	</Grid>
</Window>
private void btnRefresh_Click(object sender, RoutedEventArgs e)
		{
			this.imgQRCode.Source = null;

			if(string.IsNullOrWhiteSpace(this.tbxText.Text))
			{
				this.tbxText.Focus();
				return;
			}

			string text = this.tbxText.Text.Trim();
			int version = 5;
			int pixel = 7;
			int iconsize = 15;
			int iconboder = 5;
			bool whiteEdge = true;
			Bitmap bitmap = QREncoder.QREncoder.Encode(text, version, pixel, iconsize, iconboder, whiteEdge);

			MemoryStream ms = new MemoryStream();
			bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
			ms.Position = 0;
			BitmapImage bi = new BitmapImage();
			bi.BeginInit();
			bi.StreamSource = ms;
			bi.EndInit();

			this.imgQRCode.Source = bi;
		}

  

posted @ 2021-03-01 14:35  wild-coffee  阅读(888)  评论(0编辑  收藏  举报