条形码包含有关产品或公司的信息,以机器可读的形式直观地表示。条码广泛用于跟踪货物和库存管理。我们可以在 WPF 应用程序中轻松生成各种类型的条码。在本文中,我们将学习如何在 WPF 应用程序中生成和显示条形码图像。完成上述步骤后,我们将在 C# 中拥有自己的WPF 条码生成器。所以让我们开始吧。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.BarCode 最新下载(qun:761297826)https://www.evget.com/product/576/download

文章应涵盖以下主题:

  1. WPF条码生成器的特点
  2. C# 条码生成器 API
  3. 创建 WPF 条码生成器的步骤
  4. 生成带有附加选项的条码
  5. 演示 WPF 条码生成器
  6. 下载源代码

WPF条码生成器的特点

我们的 WPF 条码生成器将具有以下功能。

  1. 生成以下类型的条码符号:

    • 代码128
    • 代码 11
    • 代码 39
    • 二维码
    • 数据矩阵
    • EAN13
    • EAN8
    • ITF14
    • PDF417
  2. 将生成的条码图像保存为以下格式:
    • PNG
    • JPEG
    • BMP
  3. 预览生成的条形码图像。

C# 条码生成器 API

我们将使用Aspose.BarCode for .NET API 来生成条形码图像并在 WPF 应用程序中预览它们。它是一个功能丰富的 API,可让您生成、扫描和读取各种条形码类型。此外,它允许操纵生成的条形码的外观,例如背景颜色、条形颜色、旋转角度、x 尺寸、图像质量、分辨率、标题、大小等等。

创建 WPF 条码生成器的步骤

我们可以按照以下步骤在 WPF 应用程序中生成和显示条形码图像:

  • 首先,创建一个新项目并选择WPF Application项目模板。

  • 接下来,输入项目的名称,例如“BarcodeGen”。

  • 然后,选择 .NET 框架,然后选择创建.

  • 接下来,打开NuGet 包管理器并安装Aspose.BarCode for .NET包。

  • 然后,添加一个新类Barcode.cs来定义条形码。
public class Barcode
{
public string? Text { get; set; }public BaseEncodeType? BarcodeType { get; set; }public BarCodeImageFormat ImageType { get; set; }
}
  • 接下来,打开MainWindow.xaml并添加所需的控件,如下所示:

您还可以将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>
view rawWPF-Barcode-Generator_MainWindow.xaml hosted with ❤ by GitHub
然后,打开MainWindow.xaml.cs类并添加btnGenerate_Click事件来处理Generate Barcode按钮的单击操作。
private void btnGenerate_Click(object sender, RoutedEventArgs e)
{
// Set default as Png
var imageType = "Png";// Get the user selected image format
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();
}// Get image format from enum
var imageFormat = (BarCodeImageFormat)Enum.Parse(typeof(BarCodeImageFormat), imageType.ToString());// Set default as Code128
var encodeType = EncodeTypes.Code128;// Get the user selected barcode type
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;
}
}// Initalize barcode object
Barcode barcode = new Barcode();
barcode.Text = tbCodeText.Text;
barcode.BarcodeType = encodeType;
barcode.ImageType = imageFormat;try
{
string imagePath = "";if (cbGenerateWithOptions.IsChecked == true)
{
// Generate barcode with additional options and get the image path
imagePath = GenerateBarcodeWithOptions(barcode);
}
else
{
// Generate barcode and get image path
imagePath = GenerateBarcode(barcode);
}// Display the image
Uri fileUri = new Uri(Path.GetFullPath(imagePath));
imgDynamic.Source = new BitmapImage(fileUri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
  • 之后,添加一个生成条形码的函数。
private string GenerateBarcode(Barcode barcode)
{
// Image path
string imagePath = comboBarcodeType.Text + "." + barcode.ImageType;// Initilize barcode generator
BarcodeGenerator generator = new BarcodeGenerator(barcode.BarcodeType, barcode.Text);// Save the image
generator.Save(imagePath, barcode.ImageType);return imagePath;
}
  • 最后,运行应用程序。

用 Java 自定义瑞士二维码

我们还可以生成带有特定于条形码类型的附加选项的条形码。在 WPF 条码生成器中,我们添加了一个复选框来生成带有选项的条码。它将调用以下函数,为不同的条形码类型指定附加选项。

private string GenerateBarcodeWithOptions(Barcode barcode)
{
// Image path
string imagePath = comboBarcodeType.Text + "." + barcode.ImageType;// Initilize barcode generator
BarcodeGenerator generator = new BarcodeGenerator(barcode.BarcodeType, barcode.Text);if(barcode.BarcodeType == EncodeTypes.QR)
{
generator.Parameters.Barcode.XDimension.Pixels = 4;
//set Auto version
generator.Parameters.Barcode.QR.QrVersion = QRVersion.Auto;
//Set Auto QR encode type
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)
{
//set DataMatrix ECC to 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;
//set BarHeight 40
generator.Parameters.Barcode.BarHeight.Pixels = 40;
}// Save the image
generator.Save(imagePath, barcode.ImageType);return imagePath;
}

生成带有附加选项的条码

我们还可以生成带有特定于条形码类型的附加选项的条形码。在 WPF 条码生成器中,我们添加了一个复选框来生成带有选项的条码。它将调用以下函数,为不同的条形码类型指定附加选项。

private string GenerateBarcodeWithOptions(Barcode barcode)
{
// Image path
string imagePath = comboBarcodeType.Text + "." + barcode.ImageType;// Initilize barcode generator
BarcodeGenerator generator = new BarcodeGenerator(barcode.BarcodeType, barcode.Text);if(barcode.BarcodeType == EncodeTypes.QR)
{
generator.Parameters.Barcode.XDimension.Pixels = 4;
//set Auto version
generator.Parameters.Barcode.QR.QrVersion = QRVersion.Auto;
//Set Auto QR encode type
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)
{
//set DataMatrix ECC to 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;
//set BarHeight 40
generator.Parameters.Barcode.BarHeight.Pixels = 40;
}// Save the image
generator.Save(imagePath, barcode.ImageType);return imagePath;
}

演示 WPF 条码生成器

下面是我们刚刚创建的 WPF Barcode Generator 应用程序的演示。

条码控件Aspose.BarCode入门教程(4):C# 中的 WPF 条码生成器相关推荐

  1. 条码控件Aspose.BarCode入门教程(7):如何在Java 中的 GS1-128 条码生成器

    Aspose.BarCode for .NET 是一个功能强大的API,可以从任意角度生成和识别多种图像类型的一维和二维条形码.开发人员可以轻松添加条形码生成和识别功能,以及在.NET应用程序中将生成 ...

  2. ActiveReports 报表控件官方中文入门教程 (2)-创建、数据源、浏览以及发布

    ActiveReports 报表控件官方中文入门教程 (2)-创建.数据源.浏览以及发布 原文:ActiveReports 报表控件官方中文入门教程 (2)-创建.数据源.浏览以及发布 本篇文章将阐述 ...

  3. Expression Blend实例中文教程(13) - 控件模板快速入门ControlTemplates

    上篇,介绍了控件样式(Style)和模板(Template)的基础概念,并且演示了使用Blend设计控件样式.本篇将继续介绍使用Blend设计自定义控件模板 - ControlTemplate.Con ...

  4. 文档控件Aspose概述:带你全面了解Aspose产品特点

    Aspose于2002年3月在澳大利亚悉尼创建,旗下产品覆盖文档.图表.PDF.条码.OCR.CAD.HTML.电子邮件等各个文档管理领域,为全球.NET .Java.C ++等10余种平台开发人员提 ...

  5. Code128 Fontware条码控件介绍

    Code128 Fontware条码控件使你的Windows/Linux/UNIX/Mac 下的应用程序创建Code128条码变得极其容易,并且提供了TrueType,PostScript Type ...

  6. ActiveReports 报表控件官方中文新手教程 (1)-安装、激活以及产品资源

     本系列文章主要是面向初次接触 ActiveReports 产品的用户,能够帮助您在三天之内轻松的掌握ActiveReports控件的基本用法,包含安装.激活.创建报表.绑定数据源以及公布等内容. ...

  7. 使用Excel2010条码控件碰到的问题及解决办法

    iamlaosong文 在使用Excel条码过程中碰到几个难以解决的问题,有的问题又因为没有及时发现导致浪费了很多时间和耗材,用户意见也很大.这些问题出现的原因是什么,目前还不知道,所以解决办法也只是 ...

  8. 电子表格控件Spread.NET中文教程汇总

    Spread.NET 是当下最流行的兼容Microsoft Excel的.NET电子表格组件,适用于NET Windows Forms和ASP.NET开发.Spread表格控件在表格数据展现.表格操作 ...

  9. C# Winform控件包 MaterialSkin使用教程 免费开源,支持中文!

    如果没有拿到控件包DLL的可以去这篇文章里自取.C# Winform控件包分享,免费开源,支持中文! 控件比较多,我会抽出时间分控件逐一书写教程,不定时更新,感兴趣的朋友可以关注我. 本文将在以下几个 ...

最新文章

  1. ora-01653表空间扩展失败产生的场景和处理方法
  2. 表单验证失败提示方案(自用)
  3. java守护线程的特性
  4. 从贝泰妮的全域消费者运营,看Quick Audience如何链接产品服务商生态
  5. 大数据的说法 正确的是_前端测试题:(解析)用于播放音频文件的正确HTML5元素是?...
  6. DS18B20 驱动编写
  7. 3、WordCount源码分析
  8. HDU4891 The Great Pan 暴力
  9. 我是如何在GitHub上开源一个项目的(截图说明) (VS2010可以安装git插件)
  10. Atitit.一些公司的开源项目 重大知名开源项目attilax总结
  11. 回声消除技术原理与解决办法
  12. 重装系统后,文件数据被格式化如何恢复?
  13. pyinstaller打包指南,No module named xxx,is only available if OpenCV is installed.虚拟环境打包
  14. 滴滴员工求裁员,阿里不裁员,大佬聊裁员时都在聊什么?
  15. 从Oho到Siri (语言心理学简介)
  16. 剪映+json解析将视频中的声音转换成文本
  17. 卡片互动悬停下载动画
  18. Redis应用项目---抢红包功能(二)
  19. 什么是RTOS系统定义分析
  20. 单片机通过mqtt联网(51单片机和esp01s)

热门文章

  1. sup, inf 与 min, max 的区别
  2. win10 nvidia-mx350 tensorflow开发环境配置
  3. MTT S80相当于什么显卡 mtt s80评测 mtts80显卡性能
  4. python-双色球
  5. 装修避坑指南,看完起码帮你省出4万,少花冤枉钱
  6. linux PS -df,linux中ps跟df命令详解
  7. 解答edge浏览器自带小游戏在哪里
  8. 软件工程(三)暑假班
  9. C语言如何解释abc
  10. 爱奇艺在Hadoop生态中大数据平台实践