![在 WPF 中生成并显示条码图像](images/WPF-Barcode-Generator-CSharp.jpg#center)
条形码包含有关产品或公司的信息,以机器可读的形式直观呈现。条码广泛用于跟踪货物和库存管理。我们可以在 WPF 应用程序中轻松生成各种类型的条码。在本文中,我们将学习如何在 WPF 应用程序中生成和显示条形码图像。完成上述步骤后,我们将在 C# 中拥有自己的 WPF 条码生成器。那么让我们开始吧。
文章应涵盖以下主题:
WPF条码生成器的特点
我们的 WPF 条码生成器将具有以下功能。
C# 条码生成器 API
我们将使用 Aspose.BarCode for .NET API 生成条形码图像并在 WPF 应用程序中预览它们。它是一个功能丰富的 API,可让您生成、扫描和读取各种 条形码类型。此外,它允许操作生成的条形码的外观,例如背景颜色、条形颜色、旋转角度、x 尺寸、图像质量、分辨率、标题、大小等等。
创建 WPF 条码生成器的步骤
我们可以按照以下步骤在 WPF 应用程序中生成和显示条形码图像:
- 首先,创建一个新项目并选择 WPF Application 项目模板。
![Select the project template.](images/select_project_template.jpg#center)
Select the project template.
接下来,输入项目的名称,例如“BarcodeGen”。
然后,选择 .NET 框架,然后选择创建。
接下来,打开 NuGet 包管理器并安装 Aspose.BarCode for .NET 包。
![为 .NET 安装 Aspose.BarCode](images/Install_Aspose_Barcode_Nuget.jpg#center)
为 .NET 安装 Aspose.BarCode
- 然后,添加一个新类 Barcode.cs 来定义条形码。
public class Barcode
{
public string? Text { get; set; }
public BaseEncodeType? BarcodeType { get; set; }
public BarCodeImageFormat ImageType { get; set; }
}
- 接下来,打开 MainWindow.xaml 并添加所需的控件,如下所示:
![添加所需的控件](images/Add_Controls.jpg#center)
添加所需的控件
您还可以将 MainWindow.xaml 的内容替换为以下脚本。
<Window x:Class="BarcodeGen.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:BarcodeGen"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Width="800" Height="384">
<Grid.RowDefinitions>
<RowDefinition Height="191*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Select Barcode Type:" HorizontalAlignment="Left" Margin="10,16,0,0" VerticalAlignment="Top" FontSize="14" FontWeight="Bold"/>
<ComboBox x:Name="comboBarcodeType" HorizontalAlignment="Left" Margin="10,47,0,305" Width="273" Text="Select Barcode Type" IsReadOnly="True" SelectedIndex="-1" FontSize="14" Height="30">
<ComboBoxItem Content="Code128"></ComboBoxItem>
<ComboBoxItem Content="Code11"></ComboBoxItem>
<ComboBoxItem Content="Code32"></ComboBoxItem>
<ComboBoxItem Content="QR"></ComboBoxItem>
<ComboBoxItem Content="DataMatrix"></ComboBoxItem>
<ComboBoxItem Content="EAN13"></ComboBoxItem>
<ComboBoxItem Content="EAN8"></ComboBoxItem>
<ComboBoxItem Content="ITF14"></ComboBoxItem>
<ComboBoxItem Content="PDF417"></ComboBoxItem>
</ComboBox>
<Button Name="btnGenerate" Click="btnGenerate_Click" Content="Generate Barcode" HorizontalAlignment="Left" Margin="10,346,0,0" VerticalAlignment="Top" Height="28" Width="273" FontSize="14" FontWeight="Bold"/>
<Label Content="Enter Your Text:" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" FontSize="14" FontWeight="Bold"/>
<TextBox Name="tbCodeText" TextWrapping="Wrap" Margin="10,123,517,134" Width="273" Height="125"/>
<Label Content="Select Image Format:" HorizontalAlignment="Left" Margin="10,253,0,0" VerticalAlignment="Top" FontSize="14" FontWeight="Bold"/>
<RadioButton Name="rbPng" Content="Png" GroupName="rbImageType" Margin="10,285,739,77" Width="51" Height="20" FontSize="14" IsChecked="True"/>
<RadioButton Name="rbJpg" Content="Jpeg" GroupName="rbImageType" Margin="121,285,628,77" Width="51" Height="20" FontSize="14"/>
<RadioButton Name="rbBmp" Content="Bmp" GroupName="rbImageType" Margin="232,285,517,77" Width="51" Height="20" FontSize="14"/>
<CheckBox Name="cbGenerateWithOptions" Height="20" Margin="10,321,517,41" Content="Generate with Options" />
<GroupBox Header="View Generated Barcode" Margin="317,0,22,0" FontSize="14" FontWeight="Bold">
<Image Name="imgDynamic" Margin="6,-6,7,6" Stretch="None" />
</GroupBox>
</Grid>
</Window>
- 然后,打开 MainWindow.xaml.cs 类并添加 btnGenerateClick 事件来处理 Generate Barcode 按钮的单击操作。
private void btnGenerate_Click(object sender, RoutedEventArgs e)
{
// 将默认设置为PNG
var imageType = "Png";
// 获取用户选择的图片格式
if(rbPng.IsChecked == true)
{
imageType = rbPng.Content.ToString();
}
else if(rbBmp.IsChecked == true)
{
imageType = rbBmp.Content.ToString();
}
else if(rbJpg.IsChecked == true)
{
imageType = rbJpg.Content.ToString();
}
// 从枚举中获取图像格式
var imageFormat = (BarCodeImageFormat)Enum.Parse(typeof(BarCodeImageFormat), imageType.ToString());
// 将默认设置为 Code128
var encodeType = EncodeTypes.Code128;
// 获取用户选择的条码类型
if (!string.IsNullOrEmpty(comboBarcodeType.Text))
{
switch (comboBarcodeType.Text)
{
case "Code128":
encodeType = EncodeTypes.Code128;
break;
case "ITF14":
encodeType = EncodeTypes.ITF14;
break;
case "EAN13":
encodeType = EncodeTypes.EAN13;
break;
case "Datamatrix":
encodeType = EncodeTypes.DataMatrix;
break;
case "Code32":
encodeType = EncodeTypes.Code32;
break;
case "Code11":
encodeType = EncodeTypes.Code11;
break;
case "PDF417":
encodeType = EncodeTypes.Pdf417;
break;
case "EAN8":
encodeType = EncodeTypes.EAN8;
break;
case "QR":
encodeType = EncodeTypes.QR;
break;
}
}
// 初始化条码对象
Barcode barcode = new Barcode();
barcode.Text = tbCodeText.Text;
barcode.BarcodeType = encodeType;
barcode.ImageType = imageFormat;
try
{
string imagePath = "";
if (cbGenerateWithOptions.IsChecked == true)
{
// 生成带有附加选项的条形码并获取图像路径
imagePath = GenerateBarcodeWithOptions(barcode);
}
else
{
// 生成条码并获取图片路径
imagePath = GenerateBarcode(barcode);
}
// 显示图像
Uri fileUri = new Uri(Path.GetFullPath(imagePath));
imgDynamic.Source = new BitmapImage(fileUri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
- 之后,添加一个生成条形码的函数。
private string GenerateBarcode(Barcode barcode)
{
// 图片路径
string imagePath = comboBarcodeType.Text + "." + barcode.ImageType;
// 初始化条码生成器
BarcodeGenerator generator = new BarcodeGenerator(barcode.BarcodeType, barcode.Text);
// 保存图像
generator.Save(imagePath, barcode.ImageType);
return imagePath;
}
- 最后,运行应用程序。
生成带有附加选项的条码
我们还可以生成带有特定于条形码类型的附加选项的条形码。在 WPF 条码生成器中,我们添加了一个复选框来生成带有选项的条码。它将调用以下函数,为不同的条形码类型指定附加选项。
private string GenerateBarcodeWithOptions(Barcode barcode)
{
// 图片路径
string imagePath = comboBarcodeType.Text + "." + barcode.ImageType;
// 初始化条码生成器
BarcodeGenerator generator = new BarcodeGenerator(barcode.BarcodeType, barcode.Text);
if(barcode.BarcodeType == EncodeTypes.QR)
{
generator.Parameters.Barcode.XDimension.Pixels = 4;
//设置自动版本
generator.Parameters.Barcode.QR.QrVersion = QRVersion.Auto;
//设置自动二维码编码类型
generator.Parameters.Barcode.QR.QrEncodeType = QREncodeType.Auto;
}
else if(barcode.BarcodeType == EncodeTypes.Pdf417)
{
generator.Parameters.Barcode.XDimension.Pixels = 2;
generator.Parameters.Barcode.Pdf417.Columns = 3;
}
else if(barcode.BarcodeType == EncodeTypes.DataMatrix)
{
//将 DataMatrix ECC 设置为 140
generator.Parameters.Barcode.DataMatrix.DataMatrixEcc = DataMatrixEccType.Ecc200;
}
else if(barcode.BarcodeType == EncodeTypes.Code32)
{
generator.Parameters.Barcode.XDimension.Millimeters = 1f;
}
else
{
generator.Parameters.Barcode.XDimension.Pixels = 2;
//设置 BarHeight 40
generator.Parameters.Barcode.BarHeight.Pixels = 40;
}
// 保存图像
generator.Save(imagePath, barcode.ImageType);
return imagePath;
}
您可以在文档中阅读有关 条码类型的生成细节 的更多信息。
演示 WPF 条码生成器
下面是我们刚刚创建的 WPF Barcode Generator 应用程序的演示。
![演示 WPF 条码生成器](images/demo-barcode-generator.gif#center)
演示 WPF 条码生成器
下载源代码
您可以从 GitHub 下载 WPF Barcode Generator 应用程序的完整源代码。
获得免费许可证
您可以获得免费的临时许可证 试用该库而不受评估限制。
结论
在本文中,我们学习了如何在 WPF 应用程序中生成各种类型的条形码。我们还了解了如何以编程方式预览生成的条形码图像。此外,您可以使用 文档 了解更多关于 Aspose.BarCode for .NET API 的信息。如有任何歧义,请随时在我们的 论坛 上与我们联系。