IRONSOFTWAREHOME
视频

从DevExpress条形码迁移到IronBarcode

Curtis Chau
Curtis Chau
Updated: 2026年6月20日

如果您使用 DevExpress 的网格、图表、调度程序或透视表控件,请保留这些控件。 此迁移专门用于将BarCodeControl替换为一个可以读取条形码、无头运行并在UI上下文之外部署的库。 本次迁移不会影响您在 WinForms 或Blazor应用程序中使用的 DevExpress UI 控件。 只有条形码特定的代码会发生变化。

导致这种迁移的典型场景有以下三种情况之一:出现读取需求,而 DevExpress 无法满足该需求; 一项新服务需要在ASP.NET Core或云函数中生成条形码,但其中不存在 WinForms 程序集; 或者,当Suite续订时,仅仅为了条形码输出而使用完整的 UI 工具包,其每项功能的成本计算就变得不划算了。

步骤 1:安装IronBarcode

dotnet add package BarCode

如果同一项目中保留了其他 DevExpress 控件,请保留 DevExpress NuGet包。 只有源文件中与条形码相关的代码会发生变化。 如果条形码生成是 DevExpress 出现在特定项目中的唯一原因,并且您在该项目中没有使用其他 DX 控件,则可以在迁移后从该项目中删除 DevExpress 包:

# Only remove DevExpress packages if no other DX controls are used in this project
dotnet remove package DevExpress.Win.Navigation
SHELL

步骤 2:添加许可证初始化

在应用程序启动时添加IronBarcode许可激活,一次在App.xaml.cs或您的主机构建器中:

// In Program.cs (ASP.NET Core) or application entry point
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

无网络通话。 没有错误代码可供检查。 本地验证。

步骤 3:替换条形码特定代码

在代码库中搜索 DevExpress 条形码类型。其他内容保持不变:

# Find barcode-related DevExpress usage — ignore grid, chart, and other DX components
grep -r "BarCodeControl\|Code128Generator\|QRCodeGenerator\|DataMatrixGenerator\|PDF417Generator\|AztecCodeGenerator\|DevExpress.XtraPrinting.BarCode" --include="*.cs" .
grep -r "barCode\.Module\|DrawToBitmap\|BarCode\.Symbology" --include="*.cs" .
SHELL

搜索结果正是您要替换的部件。 没有别的了。

代码迁移示例

代码 128 生成

这是最常见的迁移方式。 具有BarcodeWriter.CreateBarcode调用。

之前 — DevExpress WinForms 控件:

using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.BarCode;
using System.Drawing;
using System.Drawing.Imaging;

public void GenerateCode128(string data, string outputPath)
{
    var barCode = new BarCodeControl();
    var symbology = new Code128Generator();
    symbology.CharacterSet = Code128CharacterSet.CharsetAuto;
    barCode.Symbology = symbology;
    barCode.Text = data;
    barCode.Module = 0.02f;
    barCode.ShowText = true;

    barCode.Width = 400;
    barCode.Height = 100;
    var bitmap = new Bitmap(barCode.Width, barCode.Height);
    barCode.DrawToBitmap(bitmap, new Rectangle(0, 0, barCode.Width, barCode.Height));
    bitmap.Save(outputPath, ImageFormat.Png);
    bitmap.Dispose();
}

之后IronBarcode:

// NuGet: dotnet add package BarCode
using IronBarCode;

public void GenerateCode128(string data, string outputPath)
{
    BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
        .ResizeTo(400, 100)
        .SaveAsPng(outputPath);
}

barCode.Module = 0.02f文档单位大小已消失。 .ResizeTo(400, 100)直接处理像素。 手动的.SaveAsPng()替代,从而自动处理大小调整。

二维码生成

之前 — 带有纠错功能的 DevExpress 二维码:

using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.BarCode;
using System.Drawing;
using System.Drawing.Imaging;

public void GenerateQrCode(string url, string outputPath)
{
    var barCode = new BarCodeControl();
    var symbology = new QRCodeGenerator();
    symbology.ErrorCorrectionLevel = QRCodeErrorCorrectionLevel.H;
    symbology.CompactionMode = QRCodeCompactionMode.AlphaNumeric;
    barCode.Symbology = symbology;
    barCode.Text = url;

    barCode.Width = 500;
    barCode.Height = 500;
    var bitmap = new Bitmap(barCode.Width, barCode.Height);
    barCode.DrawToBitmap(bitmap, new Rectangle(0, 0, barCode.Width, barCode.Height));
    bitmap.Save(outputPath, ImageFormat.Png);
    bitmap.Dispose();
}

之后IronBarcode:

using IronBarCode;

public void GenerateQrCode(string url, string outputPath)
{
    QRCodeWriter.CreateQrCode(url, 500, QRCodeWriter.QrErrorCorrectionLevel.Highest)
        .SaveAsPng(outputPath);
}

QRCodeWriter.QrErrorCorrectionLevel.HighestCompactionMode.AlphaNumeric设置由IronBarcode根据内容自动处理。

带有品牌标识的二维码(全新功能——DevExpress 尚不支持):

using IronBarCode;

public void GenerateBrandedQrCode(string url, string logoPath, string outputPath)
{
    QRCodeWriter.CreateQrCode(url, 500, QRCodeWriter.QrErrorCorrectionLevel.Highest)
        .AddBrandLogo(logoPath)
        .SaveAsPng(outputPath);
}

数据矩阵生成

之前 — DevExpress:

using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.BarCode;

var barCode = new BarCodeControl();
var symbology = new DataMatrixGenerator();
symbology.MatrixSize = DataMatrixSize.Matrix26x26;
barCode.Symbology = symbology;
barCode.Text = "PART-7734-X";
// ... DrawToBitmap pattern

之后IronBarcode:

using IronBarCode;

BarcodeWriter.CreateBarcode("PART-7734-X", BarcodeEncoding.DataMatrix)
    .ResizeTo(260, 260)
    .SaveAsPng("datamatrix.png");

PDF417 生成

之前 — DevExpress:

using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.BarCode;

var barCode = new BarCodeControl();
barCode.Symbology = new PDF417Generator();
barCode.Text = "SHIPMENT-DATA-2026";
// ... DrawToBitmap pattern

之后IronBarcode:

using IronBarCode;

BarcodeWriter.CreateBarcode("SHIPMENT-DATA-2026", BarcodeEncoding.PDF417)
    .ResizeTo(400, 150)
    .SaveAsPng("pdf417.png");

新增阅读功能(全新功能)

DevExpress 不提供读取 API。 如果确实需要阅读相关内容,那么IronBarcode就能立即体现其价值:

using IronBarCode;

// Read from an image file
var results = BarcodeReader.Read("uploaded-label.png");
foreach (var result in results)
{
    Console.WriteLine($"Found {result.Format}: {result.Value}");
}

// Read with options for better accuracy on difficult images
var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    MaxParallelThreads = 4
};
var detailedResults = BarcodeReader.Read("multi-barcode-sheet.png", options);

ASP.NET Core条形码端点

这在没有WinForms变通方法的情况下通过BarCodeControl无法实现。 IronBarcode原生支持此功能:

using IronBarCode;

// In Program.cs or a controller
app.MapGet("/label/{sku}", (string sku) =>
{
    var pngBytes = BarcodeWriter.CreateBarcode(sku, BarcodeEncoding.Code128)
        .ResizeTo(400, 100)
        .ToPngBinaryData();

    return Results.File(pngBytes, "image/png", $"{sku}.png");
});

app.MapGet("/qr/{data}", (string data) =>
{
    var pngBytes = QRCodeWriter.CreateQrCode(data, 300, QRCodeWriter.QrErrorCorrectionLevel.Highest)
        .ToPngBinaryData();

    return Results.File(pngBytes, "image/png");
});

从PDF文件中读取条形码

IronBarcode可以直接读取 PDF 文件。 无需 PdfiumViewer,无需渲染循环,无需额外软件包:

using IronBarCode;

// Read all barcodes from all pages of a PDF
var results = BarcodeReader.Read("shipping-manifest.pdf");
foreach (var result in results)
{
    Console.WriteLine($"Barcode: {result.Value} | Format: {result.Format}");
}

常见迁移问题

barCode.Module 使用文档单位,而非像素

barCode.Module控制文档单位中最窄条的宽度(取决于渲染DPI上下文)。 这不是像素计数。 在迁移时,不要尝试将模块值转换为像素—而是决定您想要的像素尺寸并直接使用.ResizeTo(width, height)

// Before: barCode.Module = 0.02f  — document units, indirect sizing
// After:
.ResizeTo(400, 100)  // explicit pixel dimensions

DrawToBitmap 需要预先分配位图

旧模式分配了特定大小的DrawToBitmap以在其中渲染。 IronBarcode的.SaveAsPng()在内部处理所有这些。 您无需预先分配任何资源。

// Before: must know size upfront, allocate, draw, save, dispose
barCode.Width = 400;
barCode.Height = 100;
var bitmap = new Bitmap(barCode.Width, barCode.Height);
barCode.DrawToBitmap(bitmap, new Rectangle(0, 0, barCode.Width, barCode.Height));
bitmap.Save(path, ImageFormat.Png);
bitmap.Dispose();

// After: size specified once, everything else handled
BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
    .ResizeTo(400, 100)
    .SaveAsPng(path);

WinForms 数据绑定

如果BarcodeWriter.CreateBarcode。 IronBarcode不参与 WinForms 数据绑定——它是一个程序化 API,而不是一个 UI 控件。 如果需要在表单上显示生成的条形码,则生成图像字节并将其设置到PictureBox

using IronBarCode;

// Generate and display in a WinForms PictureBox
private void UpdateBarcodeDisplay(string value)
{
    var bytes = BarcodeWriter.CreateBarcode(value, BarcodeEncoding.Code128)
        .ResizeTo(400, 100)
        .ToPngBinaryData();

    using var ms = new System.IO.MemoryStream(bytes);
    pictureBox1.Image = System.Drawing.Image.FromStream(ms);
}

命名空间替换

要移除的旧导入项:

// Remove these
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.BarCode;

新增导入项:

// Add this
using IronBarCode;

API 映射参考

DevExpress 条形码IronBarcode等效物
new BarCodeControl()静态 — 无实例
new Code128Generator() + barCode.Symbology = symbologyBarcodeEncoding.Code128参数
new QRCodeGenerator() + QRCodeErrorCorrectionLevel.HQRCodeWriter.CreateQrCode(data, size, QRCodeWriter.QrErrorCorrectionLevel.Highest)
new DataMatrixGenerator() + DataMatrixSize.Matrix26x26BarcodeWriter.CreateBarcode(data, BarcodeEncoding.DataMatrix)
new PDF417Generator()BarcodeWriter.CreateBarcode(data, BarcodeEncoding.PDF417)
new AztecCodeGenerator()BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Aztec)
barCode.Text = valueCreateQrCode的第一个参数
barCode.Module = 0.02f.ResizeTo(width, height)以像素为单位
barCode.ShowText = true.AddBarcodeText()
DrawToBitmap(bitmap, rect).SaveAsPng(path)
new Bitmap(w, h) + 手动处理不需要
位图 → MemoryStream → HTTP.ToPngBinaryData()
无读取 APIBarcodeReader.Read(path)
using DevExpress.XtraEditors + using DevExpress.XtraPrinting.BarCodeusing IronBarCode

迁移清单

使用以下 grep 模式查找所有需要更新的文件:

grep -r "BarCodeControl\|Code128Generator\|QRCodeGenerator\|DataMatrixGenerator\|PDF417Generator\|AztecCodeGenerator" --include="*.cs" .
grep -r "barCode\.Module\|barCode\.Symbology\|DrawToBitmap\|DevExpress\.XtraPrinting\.BarCode" --include="*.cs" .
SHELL

逐场比赛进行分析:

  • using DevExpress.XtraPrinting.BarCode;
  • new BarCodeControl() + 符号设置
  • new QRCodeGenerator() + 符号设置
  • barCode.Module = X
  • DrawToBitmap + bitmap.Save模式
  • .ToPngBinaryData()替换位图到内存流模式
  • 在应用程序启动时添加IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
  • 在需要读取功能的地方添加BarcodeReader.Read()调用

如果一个项目仅使用 DevExpress 来处理条形码控件,而没有使用其他 DX 组件,则在替换所有条形码引用后,删除 DevExpress NuGet包。 如果项目使用了 DevExpress 网格、图表或其他 UI 控件,请保留这些包——此迁移仅涉及条形码代码。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

相关文章

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户