IRONSOFTWAREHOME
影片

從DevExpress Barcode遷移到IronBarcode

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

如果您使用DevExpress的表格、圖表、排程器或樞紐控件,保留它們。 此遷移特別是關於用一個能夠讀取條碼的程式庫來替換BarCodeControl,可以無頭運行,並可在UI環境之外部署。 您在WinForms或Blazor應用中依賴的DevExpress UI控件不受此遷移影響。 僅對條碼相關的程式碼進行更改。

促使此遷移的一般情況有三種:需要新增閱讀功能,而DevExpress無法滿足; 新服務需要在ASP.NET Core或WinForms組件不存在的雲端函式中生成條碼; 或套件續費到期,而使用完整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:新增授權初始化

在應用啟動時於App.xaml.cs或您的主機建構器中新增一次IronBarcode授權啟用:

// 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

搜尋結果正是您將替換的部分。 無其他改變。

程式碼遷移範例

Code 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()代替,後者自動處理尺寸。

QR 碼生成

之前 — 擁有錯誤校正功能的DevExpress QR:

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依據內容自動處理。

帶有品牌Logo的QR碼(全新功能 — DevExpress無法實現):

using IronBarCode;

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

Data Matrix生成

之前 — 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需要預分配的Bitmap

舊模式會分配一個特定大小的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) + 手動處置不需要
Bitmap → 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()替代Bitmap-to-MemoryStream圖案
  • 在應用程式啟動時新增IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
  • 在任何需要閱讀功能的地方新增BarcodeReader.Read()呼叫

如果專案中僅使用DevExpress以進行條碼控件,且未使用其他DX組件,在所有條碼引用替換後,請移除DevExpress NuGet套件。 如果專案使用DevExpress表格、圖表或其他UI控件,請保留這些套件不變 — 此遷移僅影響條碼程式碼。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

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天試用金鑰
無需信用卡或帳戶建立