跳至頁尾內容
影片

如何在C#中一次讀取多個條碼

從DevExpress Barcode遷移到IronBarcode

如果您使用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
# 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";
// In Program.cs (ASP.NET Core) or application entry point
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
Imports IronBarCode

' In Program.vb (ASP.NET Core) or application entry point
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

無需網路呼叫。 無需錯誤碼檢查。 本地驗證。

步驟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" .
# 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();
}
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();
}
Imports DevExpress.XtraEditors
Imports DevExpress.XtraPrinting.BarCode
Imports System.Drawing
Imports System.Drawing.Imaging

Public Sub GenerateCode128(data As String, outputPath As String)
    Dim barCode As New BarCodeControl()
    Dim symbology As 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
    Dim bitmap As New Bitmap(barCode.Width, barCode.Height)
    barCode.DrawToBitmap(bitmap, New Rectangle(0, 0, barCode.Width, barCode.Height))
    bitmap.Save(outputPath, ImageFormat.Png)
    bitmap.Dispose()
End Sub
$vbLabelText   $csharpLabel

之後 — 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);
}
// NuGet: dotnet add package BarCode
using IronBarCode;

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

Public Sub GenerateCode128(data As String, outputPath As String)
    BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128) _
        .ResizeTo(400, 100) _
        .SaveAsPng(outputPath)
End Sub
$vbLabelText   $csharpLabel

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();
}
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();
}
Imports DevExpress.XtraEditors
Imports DevExpress.XtraPrinting.BarCode
Imports System.Drawing
Imports System.Drawing.Imaging

Public Sub GenerateQrCode(url As String, outputPath As String)
    Dim barCode As New BarCodeControl()
    Dim symbology As New QRCodeGenerator()
    symbology.ErrorCorrectionLevel = QRCodeErrorCorrectionLevel.H
    symbology.CompactionMode = QRCodeCompactionMode.AlphaNumeric
    barCode.Symbology = symbology
    barCode.Text = url

    barCode.Width = 500
    barCode.Height = 500
    Dim bitmap As New Bitmap(barCode.Width, barCode.Height)
    barCode.DrawToBitmap(bitmap, New Rectangle(0, 0, barCode.Width, barCode.Height))
    bitmap.Save(outputPath, ImageFormat.Png)
    bitmap.Dispose()
End Sub
$vbLabelText   $csharpLabel

之後 — IronBarcode:

using IronBarCode;

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

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

Public Sub GenerateQrCode(url As String, outputPath As String)
    QRCodeWriter.CreateQrCode(url, 500, QRCodeWriter.QrErrorCorrectionLevel.Highest) _
        .SaveAsPng(outputPath)
End Sub
$vbLabelText   $csharpLabel

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);
}
using IronBarCode;

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

Public Sub GenerateBrandedQrCode(url As String, logoPath As String, outputPath As String)
    QRCodeWriter.CreateQrCode(url, 500, QRCodeWriter.QrErrorCorrectionLevel.Highest) _
        .AddBrandLogo(logoPath) _
        .SaveAsPng(outputPath)
End Sub
$vbLabelText   $csharpLabel

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
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
Imports DevExpress.XtraEditors
Imports DevExpress.XtraPrinting.BarCode

Dim barCode As New BarCodeControl()
Dim symbology As New DataMatrixGenerator()
symbology.MatrixSize = DataMatrixSize.Matrix26x26
barCode.Symbology = symbology
barCode.Text = "PART-7734-X"
' ... DrawToBitmap pattern
$vbLabelText   $csharpLabel

之後 — IronBarcode:

using IronBarCode;

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

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

BarcodeWriter.CreateBarcode("PART-7734-X", BarcodeEncoding.DataMatrix) _
    .ResizeTo(260, 260) _
    .SaveAsPng("datamatrix.png")
$vbLabelText   $csharpLabel

PDF417生成

之前 — DevExpress:

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

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

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

Dim barCode As New BarCodeControl()
barCode.Symbology = New PDF417Generator()
barCode.Text = "SHIPMENT-DATA-2026"
' ... DrawToBitmap pattern
$vbLabelText   $csharpLabel

之後 — IronBarcode:

using IronBarCode;

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

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

BarcodeWriter.CreateBarcode("SHIPMENT-DATA-2026", BarcodeEncoding.PDF417) _
    .ResizeTo(400, 150) _
    .SaveAsPng("pdf417.png")
$vbLabelText   $csharpLabel

新增讀取功能(全新功能)

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);
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);
Imports IronBarCode

' Read from an image file
Dim results = BarcodeReader.Read("uploaded-label.png")
For Each result In results
    Console.WriteLine($"Found {result.Format}: {result.Value}")
Next

' Read with options for better accuracy on difficult images
Dim options As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = True,
    .MaxParallelThreads = 4
}
Dim detailedResults = BarcodeReader.Read("multi-barcode-sheet.png", options)
$vbLabelText   $csharpLabel

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");
});
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");
});
Imports IronBarCode

' In Program.vb or a controller
app.MapGet("/label/{sku}", Function(sku As String)
    Dim pngBytes = BarcodeWriter.CreateBarcode(sku, BarcodeEncoding.Code128) _
        .ResizeTo(400, 100) _
        .ToPngBinaryData()

    Return Results.File(pngBytes, "image/png", $"{sku}.png")
End Function)

app.MapGet("/qr/{data}", Function(data As String)
    Dim pngBytes = QRCodeWriter.CreateQrCode(data, 300, QRCodeWriter.QrErrorCorrectionLevel.Highest) _
        .ToPngBinaryData()

    Return Results.File(pngBytes, "image/png")
End Function)
$vbLabelText   $csharpLabel

從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}");
}
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}");
}
Imports IronBarCode

' Read all barcodes from all pages of a PDF
Dim results = BarcodeReader.Read("shipping-manifest.pdf")
For Each result In results
    Console.WriteLine($"Barcode: {result.Value} | Format: {result.Format}")
Next
$vbLabelText   $csharpLabel

常見遷移問題

barCode.Module使用文件單元,不是像素

barCode.Module控制著狹窄條的寬度,單位為文件單位(依賴於渲染DPI上下文)。 這不是像素數。 進行遷移時,不要嘗試將模塊值轉換為像素 - 而是決定您想要的像素尺寸並使用.ResizeTo(width, height)直接進行。

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

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);
// 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);
' Before: must know size upfront, allocate, draw, save, dispose
barCode.Width = 400
barCode.Height = 100
Dim bitmap As 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)
$vbLabelText   $csharpLabel

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);
}
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);
}
Imports IronBarCode
Imports System.IO

' Generate and display in a WinForms PictureBox
Private Sub UpdateBarcodeDisplay(value As String)
    Dim bytes = BarcodeWriter.CreateBarcode(value, BarcodeEncoding.Code128) _
        .ResizeTo(400, 100) _
        .ToPngBinaryData()

    Using ms As New MemoryStream(bytes)
        pictureBox1.Image = System.Drawing.Image.FromStream(ms)
    End Using
End Sub
$vbLabelText   $csharpLabel

命名空間替換

需要移除的舊引入:

// Remove these
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.BarCode;
// Remove these
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.BarCode;
$vbLabelText   $csharpLabel

需要新增的新引入:

// Add this
using IronBarCode;
// Add this
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

API 映射參考

DevExpress條碼 IronBarcode等效
new BarCodeControl() 靜態——無需實例
new Code128Generator() + barCode.Symbology = symbology BarcodeEncoding.Code128參數
new QRCodeGenerator() + QRCodeErrorCorrectionLevel.H QRCodeWriter.CreateQrCode(data, size, QRCodeWriter.QrErrorCorrectionLevel.Highest)
new DataMatrixGenerator() + DataMatrixSize.Matrix26x26 BarcodeWriter.CreateBarcode(data, BarcodeEncoding.DataMatrix)
new PDF417Generator() BarcodeWriter.CreateBarcode(data, BarcodeEncoding.PDF417)
new AztecCodeGenerator() BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Aztec)
barCode.Text = value CreateQrCode的第一個參數
barCode.Module = 0.02f .ResizeTo(width, height)在像素中
barCode.ShowText = true .AddBarcodeText()
DrawToBitmap(bitmap, rect) .SaveAsPng(path)
new Bitmap(w, h) + 手動處置 不需要
Bitmap → MemoryStream → HTTP .ToPngBinaryData()
無讀取 API BarcodeReader.Read(path)
using DevExpress.XtraEditors + using DevExpress.XtraPrinting.BarCode using IronBarCode

遷移檢查清單

使用這個grep模式找到每個需要更新的文件:

grep -r "BarCodeControl\|Code128Generator\|QRCodeGenerator\|DataMatrixGenerator\|PDF417Generator\|AztecCodeGenerator" --include="*.cs" .
grep -r "barCode\.Module\|barCode\.Symbology\|DrawToBitmap\|DevExpress\.XtraPrinting\.BarCode" --include="*.cs" .
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控件,請保留這些套件不變 — 此遷移僅影響條碼程式碼。

常見問題

為什麼我應該從DevExpress Barcode遷移到IronBarcode?

常見的理由包括簡化授權(去除SDK +運行時鑰匙的複雜性)、消除吞吐量限制、獲得原生PDF支持、改善Docker/CI/CD部署,並減少生產程式碼中的API樣板。

我如何使用IronBarcode替換DevExpress的API呼叫?

用IronBarCode.License.LicenseKey = "key"替換實例建立和授權樣板。用BarcodeReader.Read(path)替換讀取呼叫,用BarcodeWriter.CreateBarcode(data, encoding)替換寫入呼叫。靜態方法不需要實例管理。

從DevExpress Barcode遷移到IronBarcode時,程式碼需要更改多少?

大多數遷移會導致較少的程式碼行。授權樣板、實例構造函式和顯式格式配置被移除。核心讀/寫操作映射為較短的IronBarcode等價物,且結果物件更清晰。

在遷移期間,我是否需要同時安裝DevExpress Barcode和IronBarcode?

不需要。大多數遷移是直接替換而不是並行操作。一次遷移一個服務類,替換NuGet引用,並更新實例化和API呼叫模式後再移動到下一個類。

IronBarcode的NuGet包名稱是什麼?

包名是'IronBarCode'(大寫B和C)。可以用'Install-Package IronBarCode'或'dotnet add package IronBarCode'安裝。程式碼中的using指令是'using IronBarCode;'。

IronBarcode與DevExpress Barcode相比如何簡化Docker部署?

IronBarcode是沒有外部SDK文件或掛載許可配置的NuGet封裝。在Docker中,設置IRONBARCODE_LICENSE_KEY環境變數,包在啟動時處理許可驗證。

在從DevExpress遷移後,IronBarcode會自動檢測所有條碼格式嗎?

是的。IronBarcode自動檢測所有支持的格式的符號學。不需要顯式的BarcodeTypes枚舉。如果格式已經知道並且性能很重要,BarcodeReaderOptions允許限制搜索空間作為優化。

IronBarcode是否可以不使用單獨的庫讀取PDF中的條碼?

可以。BarcodeReader.Read("document.pdf")原生處理PDF文件。結果包括每個條碼發現的頁碼、格式、值和置信度。不需要外部PDF渲染步驟。

IronBarcode如何處理並行條碼處理?

IronBarcode的靜態方法是無狀態且執行緒安全的。直接使用Parallel.ForEach遍歷文件列表,不需要每個執行緒的實例管理。BarcodeReaderOptions.MaxParallelThreads控制內部執行緒預算。

從DevExpress Barcode遷移到IronBarcode時,哪些結果屬性發生變化?

常見的重命名:BarcodeValue變為Value,BarcodeType變為Format。IronBarcode結果還新增了Confidence和PageNumber。整個解決方案的搜索和替換可處理現有結果處理程式碼中的重新命名。

我如何在CI/CD管道中設置IronBarcode授權?

將IRONBARCODE_LICENSE_KEY作為管道機密儲存,在應用程式啟動程式碼中分配IronBarCode.License.LicenseKey。一個機密涵蓋所有環境,包括開發、測試、分期和生產。

IronBarcode是否支持生成帶有自定義樣式的QR碼?

支持。QRCodeWriter.CreateQrCode()支持通過ChangeBarCodeColor()自定義顏色,通過AddBrandLogo()嵌入logo,可配置錯誤校正級別,以及包括PNG、JPG、PDF和流在內的多種輸出格式。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話