跳至頁尾內容
影片

如何使用 NET MAUI 在 C# 中讀取和掃描條碼

從 Infragistics Barcode 遷移到 IronBarcode

大多數的 Infragistics 條碼遷移是因為團隊需要在項目中進行條碼閱讀,而僅有生成功能可用──這些項目包括 WinForms、ASP.NET Core 或任何 WPF 外的項目──或是因為事件驅動的 WPF 閱讀器 API 在生產環境中過於不穩定。 TaskCompletionSource 桥接模式,共用的 _result 字段導致併發調用不安全,以及因缺少 SymbologyType 標誌而導致的靜默失敗,都是團隊尋找替代方案的常見原因。

本指南遍及完整的遷移過程:移除 Infragistics 條碼套件、在 WinForms 和 WPF 環境中替換生成和閱讀程式碼,並為 Infragistics 不支援的項目型別新增條碼功能。

快速開始

步驟1:移除 Infragistics Barcode 套件

從您的專案中移除 Infragistics 條碼 NuGet 套件。 依您所使用的目標而定:

# WinForms generation package
dotnet remove package Infragistics.WinForms.Barcode

# WPF reading package
dotnet remove package Infragistics.WPF.BarcodeReader
# WinForms generation package
dotnet remove package Infragistics.WinForms.Barcode

# WPF reading package
dotnet remove package Infragistics.WPF.BarcodeReader
SHELL

如果專案文件同時引用兩者,則將兩者移除。 如果您在使用其他 Infragistics 組件,則移除條碼相關的套件不會影響 Infragistics Ultimate 授權。

步驟2:安裝 IronBarcode

dotnet add package IronBarcode
dotnet add package IronBarcode
SHELL

IronBarcode 是一個單一套件,適用於 WinForms、WPF、ASP.NET Core、控制台、Blazor Server、Docker、Azure Functions 和 AWS Lambda。 您不需要為每個框架單獨使用套件。

步驟3:更新命名空間

將 Infragistics 條碼命名空間替換為 IronBarCode(大寫 B、大寫 C):

// Remove these:
// using Infragistics.Win.UltraWinBarcode;
// using Infragistics.Controls.Barcodes;
// using System.Windows.Media.Imaging; // if only used for barcode BitmapImage loading

// Add this:
using IronBarCode;
// Remove these:
// using Infragistics.Win.UltraWinBarcode;
// using Infragistics.Controls.Barcodes;
// using System.Windows.Media.Imaging; // if only used for barcode BitmapImage loading

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

步驟4:初始化授權

在應用程式啟動時,初始化授權──App.xaml.cs 或您的 DI composition root:

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
Imports IronBarCode

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

程式碼遷移範例

WinForms 生成:UltraWinBarcode 到 BarcodeWriter

WinForms 的生成模式可順利地映射到IronBarcode的 BarcodeWriter

之前(Infragistics WinForms):

using Infragistics.Win.UltraWinBarcode;

var barcode = new UltraWinBarcode();
barcode.Symbology = Symbology.Code128;
barcode.Data = "ITEM-12345";
barcode.SaveTo(outputPath);
using Infragistics.Win.UltraWinBarcode;

var barcode = new UltraWinBarcode();
barcode.Symbology = Symbology.Code128;
barcode.Data = "ITEM-12345";
barcode.SaveTo(outputPath);
Imports Infragistics.Win.UltraWinBarcode

Dim barcode As New UltraWinBarcode()
barcode.Symbology = Symbology.Code128
barcode.Data = "ITEM-12345"
barcode.SaveTo(outputPath)
$vbLabelText   $csharpLabel

之後(IronBarcode):

// NuGet: dotnet add package IronBarcode
using IronBarCode;

BarcodeWriter.CreateBarcode("ITEM-12345", BarcodeEncoding.Code128)
             .SaveAsPng(outputPath);
// NuGet: dotnet add package IronBarcode
using IronBarCode;

BarcodeWriter.CreateBarcode("ITEM-12345", BarcodeEncoding.Code128)
             .SaveAsPng(outputPath);
Imports IronBarCode

BarcodeWriter.CreateBarcode("ITEM-12345", BarcodeEncoding.Code128) _
             .SaveAsPng(outputPath)
$vbLabelText   $csharpLabel

Infragistics 的 Symbology 枚舉值移動到 BarcodeEncoding 枚舉,這是 CreateBarcode() 的參數,而非屬性賦值。 資料成為第一個參數。 SaveTo() 變成 .SaveAsSvg().SaveAsPdf(),視您的輸出格式而定。

如果您需要在保存之前調整條碼的大小:

using IronBarCode;

BarcodeWriter.CreateBarcode("ITEM-12345", BarcodeEncoding.Code128)
             .ResizeTo(400, 100)
             .SaveAsPng(outputPath);
using IronBarCode;

BarcodeWriter.CreateBarcode("ITEM-12345", BarcodeEncoding.Code128)
             .ResizeTo(400, 100)
             .SaveAsPng(outputPath);
Imports IronBarCode

BarcodeWriter.CreateBarcode("ITEM-12345", BarcodeEncoding.Code128) _
             .ResizeTo(400, 100) _
             .SaveAsPng(outputPath)
$vbLabelText   $csharpLabel

WPF 事件驅動的閱讀:Service Class 到兩行

這是在遷移中的最大減少。 Infragistics WPF 的閱讀模式需要完整的服務類:

之前(Infragistics WPF — 約35行):

using Infragistics.Controls.Barcodes;
using System.Windows.Media.Imaging;

private BarcodeReader _reader;
private TaskCompletionSource<string> _result;

public InfragisticsBarcodeService()
{
    _reader = new BarcodeReader();
    _reader.DecodeComplete += OnDecodeComplete;
}

public async Task<string> ReadBarcodeAsync(string imagePath)
{
    _result = new TaskCompletionSource<string>();

    var bitmap = new BitmapImage(new Uri(imagePath, UriKind.Absolute));

    _reader.SymbologyTypes = Symbology.Code128 |
                             Symbology.Code39 |
                             Symbology.QRCode |
                             Symbology.EAN8 |
                             Symbology.EAN13 |
                             Symbology.UpcA |
                             Symbology.Interleaved2Of5;

    _reader.Decode(bitmap);
    return await _result.Task;
}

private void OnDecodeComplete(object sender, ReaderDecodeArgs e)
{
    _result?.TrySetResult(e.Value ?? "No barcode found");
}

public void Dispose()
{
    if (_reader != null)
    {
        _reader.DecodeComplete -= OnDecodeComplete;
        _reader = null;
    }
}
using Infragistics.Controls.Barcodes;
using System.Windows.Media.Imaging;

private BarcodeReader _reader;
private TaskCompletionSource<string> _result;

public InfragisticsBarcodeService()
{
    _reader = new BarcodeReader();
    _reader.DecodeComplete += OnDecodeComplete;
}

public async Task<string> ReadBarcodeAsync(string imagePath)
{
    _result = new TaskCompletionSource<string>();

    var bitmap = new BitmapImage(new Uri(imagePath, UriKind.Absolute));

    _reader.SymbologyTypes = Symbology.Code128 |
                             Symbology.Code39 |
                             Symbology.QRCode |
                             Symbology.EAN8 |
                             Symbology.EAN13 |
                             Symbology.UpcA |
                             Symbology.Interleaved2Of5;

    _reader.Decode(bitmap);
    return await _result.Task;
}

private void OnDecodeComplete(object sender, ReaderDecodeArgs e)
{
    _result?.TrySetResult(e.Value ?? "No barcode found");
}

public void Dispose()
{
    if (_reader != null)
    {
        _reader.DecodeComplete -= OnDecodeComplete;
        _reader = null;
    }
}
Imports Infragistics.Controls.Barcodes
Imports System.Windows.Media.Imaging
Imports System.Threading.Tasks

Private _reader As BarcodeReader
Private _result As TaskCompletionSource(Of String)

Public Sub New()
    _reader = New BarcodeReader()
    AddHandler _reader.DecodeComplete, AddressOf OnDecodeComplete
End Sub

Public Async Function ReadBarcodeAsync(imagePath As String) As Task(Of String)
    _result = New TaskCompletionSource(Of String)()

    Dim bitmap As New BitmapImage(New Uri(imagePath, UriKind.Absolute))

    _reader.SymbologyTypes = Symbology.Code128 Or
                             Symbology.Code39 Or
                             Symbology.QRCode Or
                             Symbology.EAN8 Or
                             Symbology.EAN13 Or
                             Symbology.UpcA Or
                             Symbology.Interleaved2Of5

    _reader.Decode(bitmap)
    Return Await _result.Task
End Function

Private Sub OnDecodeComplete(sender As Object, e As ReaderDecodeArgs)
    _result?.TrySetResult(If(e.Value, "No barcode found"))
End Sub

Public Sub Dispose()
    If _reader IsNot Nothing Then
        RemoveHandler _reader.DecodeComplete, AddressOf OnDecodeComplete
        _reader = Nothing
    End If
End Sub
$vbLabelText   $csharpLabel

之後(IronBarcode — 2 行):

using IronBarCode;

var results = BarcodeReader.Read(imagePath);
return results.FirstOrDefault()?.Value ?? "No barcode found";
using IronBarCode;

var results = BarcodeReader.Read(imagePath);
return results.FirstOrDefault()?.Value ?? "No barcode found";
Imports IronBarCode

Dim results = BarcodeReader.Read(imagePath)
Return If(results.FirstOrDefault()?.Value, "No barcode found")
$vbLabelText   $csharpLabel

整個服務類──實例管理、事件布線、BitmapImage 載入、Dispose()──都已移除。 BarcodeReader.Read() 是同步的、靜態的,並直接接受文件路徑字串。

如果您還需要返回符號型別:

using IronBarCode;

var results = BarcodeReader.Read(imagePath);
var first = results.FirstOrDefault();
if (first != null)
{
    Console.WriteLine($"Value: {first.Value}, Format: {first.Format}");
}
using IronBarCode;

var results = BarcodeReader.Read(imagePath);
var first = results.FirstOrDefault();
if (first != null)
{
    Console.WriteLine($"Value: {first.Value}, Format: {first.Format}");
}
Imports IronBarCode

Dim results = BarcodeReader.Read(imagePath)
Dim first = results.FirstOrDefault()
If first IsNot Nothing Then
    Console.WriteLine($"Value: {first.Value}, Format: {first.Format}")
End If
$vbLabelText   $csharpLabel

result.Value 取代 e.Valueresult.Format 取代 e.Symbology

新增 WinForms 閱讀(以前不可能)

如果您是在僅有生成功能的 WinForms 項目中,閱讀功能現已可以使用同一套件進行:

using IronBarCode;

// In a WinForms button handler or service method — same API as WPF or ASP.NET Core
var results = BarcodeReader.Read(imagePath);
foreach (var result in results)
{
    MessageBox.Show($"Found: {result.Value} ({result.Format})");
}
using IronBarCode;

// In a WinForms button handler or service method — same API as WPF or ASP.NET Core
var results = BarcodeReader.Read(imagePath);
foreach (var result in results)
{
    MessageBox.Show($"Found: {result.Value} ({result.Format})");
}
Imports IronBarCode

' In a WinForms button handler or service method — same API as WPF or ASP.NET Core
Dim results = BarcodeReader.Read(imagePath)
For Each result In results
    MessageBox.Show($"Found: {result.Value} ({result.Format})")
Next
$vbLabelText   $csharpLabel

不需要額外的套件,不需要框架特定的設置。相同的 BarcodeReader.Read() 呼叫在 WinForms 和 WPF 中皆可使用。

ASP.NET Core端點(以前不可能)

在 ASP.NET Core控制器中進行條碼生成和閱讀,Infragistics 包是不可能的。 使用 IronBarcode:

using IronBarCode;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/barcode")]
public class BarcodeController : ControllerBase
{
    [HttpPost("read")]
    public IActionResult ReadBarcode(IFormFile imageFile)
    {
        using var stream = imageFile.OpenReadStream();
        var results = BarcodeReader.Read(stream);
        var values = results.Select(r => new { r.Value, Format = r.Format.ToString() });
        return Ok(values);
    }

    [HttpGet("generate")]
    public IActionResult GenerateBarcode([FromQuery] string data)
    {
        var bytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
                                 .ResizeTo(400, 100)
                                 .ToPngBinaryData();
        return File(bytes, "image/png");
    }
}
using IronBarCode;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/barcode")]
public class BarcodeController : ControllerBase
{
    [HttpPost("read")]
    public IActionResult ReadBarcode(IFormFile imageFile)
    {
        using var stream = imageFile.OpenReadStream();
        var results = BarcodeReader.Read(stream);
        var values = results.Select(r => new { r.Value, Format = r.Format.ToString() });
        return Ok(values);
    }

    [HttpGet("generate")]
    public IActionResult GenerateBarcode([FromQuery] string data)
    {
        var bytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
                                 .ResizeTo(400, 100)
                                 .ToPngBinaryData();
        return File(bytes, "image/png");
    }
}
Imports IronBarCode
Imports Microsoft.AspNetCore.Mvc

<ApiController>
<Route("api/barcode")>
Public Class BarcodeController
    Inherits ControllerBase

    <HttpPost("read")>
    Public Function ReadBarcode(imageFile As IFormFile) As IActionResult
        Using stream = imageFile.OpenReadStream()
            Dim results = BarcodeReader.Read(stream)
            Dim values = results.Select(Function(r) New With {Key .Value = r.Value, Key .Format = r.Format.ToString()})
            Return Ok(values)
        End Using
    End Function

    <HttpGet("generate")>
    Public Function GenerateBarcode(<FromQuery> data As String) As IActionResult
        Dim bytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128) _
                                  .ResizeTo(400, 100) _
                                  .ToPngBinaryData()
        Return File(bytes, "image/png")
    End Function
End Class
$vbLabelText   $csharpLabel

BarcodeReader.Read() 直接接受 Stream──無需先將上傳保存到磁碟。

PDF 條碼閱讀

IronBarcode 可以從 PDF 文件中原生讀取條碼──無需額外的套件:

using IronBarCode;

// Read all barcodes from every page of a PDF
var results = BarcodeReader.Read("document.pdf");
foreach (var result in results)
{
    Console.WriteLine($"Page {result.PageNumber}: {result.Value} ({result.Format})");
}
using IronBarCode;

// Read all barcodes from every page of a PDF
var results = BarcodeReader.Read("document.pdf");
foreach (var result in results)
{
    Console.WriteLine($"Page {result.PageNumber}: {result.Value} ({result.Format})");
}
Imports IronBarCode

' Read all barcodes from every page of a PDF
Dim results = BarcodeReader.Read("document.pdf")
For Each result In results
    Console.WriteLine($"Page {result.PageNumber}: {result.Value} ({result.Format})")
Next
$vbLabelText   $csharpLabel

Infragistics 沒有相應功能──PDF 條碼閱讀需要將 PDF 頁面外部轉換為圖像後再掃描。

批量處理:從順序到並行

之前(Infragistics — 順序的,並行化不安全):

// Must process one at a time — shared _result field is not thread-safe
var service = new InfragisticsBarcodeService();
var results = new List<string>();

foreach (var file in imageFiles)
{
    var value = await service.ReadBarcodeAsync(file);
    results.Add(value);
}

service.Dispose();
// Must process one at a time — shared _result field is not thread-safe
var service = new InfragisticsBarcodeService();
var results = new List<string>();

foreach (var file in imageFiles)
{
    var value = await service.ReadBarcodeAsync(file);
    results.Add(value);
}

service.Dispose();
Imports System.Collections.Generic

' Must process one at a time — shared _result field is not thread-safe
Dim service As New InfragisticsBarcodeService()
Dim results As New List(Of String)()

For Each file In imageFiles
    Dim value As String = Await service.ReadBarcodeAsync(file)
    results.Add(value)
Next

service.Dispose()
$vbLabelText   $csharpLabel

之後(IronBarcode — 並行的,執行緒安全):

using IronBarCode;

// Option 1: pass an array directly —IronBarcodehandles parallelization internally
var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    MaxParallelThreads = 4
};

var results = BarcodeReader.Read(imageFiles.ToArray(), options);
foreach (var result in results)
{
    Console.WriteLine($"{result.Value} ({result.Format})");
}
using IronBarCode;

// Option 1: pass an array directly —IronBarcodehandles parallelization internally
var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    MaxParallelThreads = 4
};

var results = BarcodeReader.Read(imageFiles.ToArray(), options);
foreach (var result in results)
{
    Console.WriteLine($"{result.Value} ({result.Format})");
}
Imports IronBarCode

' Option 1: pass an array directly —IronBarcode handles parallelization internally
Dim options As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = True,
    .MaxParallelThreads = 4
}

Dim results = BarcodeReader.Read(imageFiles.ToArray(), options)
For Each result In results
    Console.WriteLine($"{result.Value} ({result.Format})")
Next
$vbLabelText   $csharpLabel
using IronBarCode;
using System.Collections.Concurrent;

// Option 2: Parallel.ForEach — static BarcodeReader.Read is thread-safe
var allValues = new ConcurrentBag<string>();

Parallel.ForEach(imageFiles, file =>
{
    var results = BarcodeReader.Read(file);
    foreach (var r in results)
        allValues.Add(r.Value);
});
using IronBarCode;
using System.Collections.Concurrent;

// Option 2: Parallel.ForEach — static BarcodeReader.Read is thread-safe
var allValues = new ConcurrentBag<string>();

Parallel.ForEach(imageFiles, file =>
{
    var results = BarcodeReader.Read(file);
    foreach (var r in results)
        allValues.Add(r.Value);
});
Imports IronBarCode
Imports System.Collections.Concurrent

' Option 2: Parallel.ForEach — static BarcodeReader.Read is thread-safe
Dim allValues As New ConcurrentBag(Of String)()

Parallel.ForEach(imageFiles, Sub(file)
    Dim results = BarcodeReader.Read(file)
    For Each r In results
        allValues.Add(r.Value)
    Next
End Sub)
$vbLabelText   $csharpLabel

移除 Dispose 模式

如果您的程式碼在 Infragistics BarcodeReader 周圍實現了 IDisposable,這些可以完全移除。IronBarcode的 BarcodeReader 是一個靜態類。 沒有實例要建立,沒有事件處理器要分離,也沒有 Dispose() 要調用。

// Before: service registered as IDisposable in DI container
services.AddScoped<InfragisticsBarcodeService>(); // implements IDisposable

// After: no registration needed — use static method directly, or wrap in thin service if preferred
// services.AddScoped<IBarcodeService, IronBarcodeService>();
// Before: service registered as IDisposable in DI container
services.AddScoped<InfragisticsBarcodeService>(); // implements IDisposable

// After: no registration needed — use static method directly, or wrap in thin service if preferred
// services.AddScoped<IBarcodeService, IronBarcodeService>();
' Before: service registered as IDisposable in DI container
services.AddScoped(Of InfragisticsBarcodeService)() ' implements IDisposable

' After: no registration needed — use static method directly, or wrap in thin service if preferred
' services.AddScoped(Of IBarcodeService, IronBarcodeService)()
$vbLabelText   $csharpLabel

為 DI 目的提供的薄服務包裝器簡單直接:

using IronBarCode;

public class BarcodeService
{
    public IEnumerable<string> Read(string path)
        => BarcodeReader.Read(path).Select(r => r.Value);

    public byte[] Generate(string data, BarcodeEncoding encoding)
        => BarcodeWriter.CreateBarcode(data, encoding).ToPngBinaryData();
}
using IronBarCode;

public class BarcodeService
{
    public IEnumerable<string> Read(string path)
        => BarcodeReader.Read(path).Select(r => r.Value);

    public byte[] Generate(string data, BarcodeEncoding encoding)
        => BarcodeWriter.CreateBarcode(data, encoding).ToPngBinaryData();
}
Imports IronBarCode

Public Class BarcodeService
    Public Function Read(path As String) As IEnumerable(Of String)
        Return BarcodeReader.Read(path).Select(Function(r) r.Value)
    End Function

    Public Function Generate(data As String, encoding As BarcodeEncoding) As Byte()
        Return BarcodeWriter.CreateBarcode(data, encoding).ToPngBinaryData()
    End Function
End Class
$vbLabelText   $csharpLabel

無需構造函式注入,無需 Dispose(),無需事件管理。

常見遷移問題

移除 SymbologyType 標誌

Infragistics 的 WPF 讀取器上的 SymbologyTypes 屬性要求您列出您想支持的每個條碼格式。IronBarcode沒有對應物——它會自動檢測每次閱讀時支持的所有 50+ 種格式。

這帶來了一個重要的副作用:如果之前某一特定條碼格式因其 SymbologyType 標誌從列表中消失而默默失敗,IronBarcode 現在將找到它。 您可能會看到以前未曾出現過的條碼值出現在生產環境中。 這是正確的行為——那些條碼一直都在。

如果您需要限制返回的格式(出於性能或模糊原因),BarcodeReaderOptions 支持這個功能:

using IronBarCode;

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = false
};

var results = BarcodeReader.Read(imagePath, options);
using IronBarCode;

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = false
};

var results = BarcodeReader.Read(imagePath, options);
Imports IronBarCode

Dim options As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = False
}

Dim results = BarcodeReader.Read(imagePath, options)
$vbLabelText   $csharpLabel

不需要載入 BitmapImage

Infragistics 的 WPF 閱讀器需要通過 new BitmapImage(new Uri(path, UriKind.Absolute)) 載入圖片為 BitmapSource 物件。IronBarcode可以直接接受文件路徑字串、字節陣列和流:

// All of these work — no BitmapImage conversion needed
BarcodeReader.Read("path/to/image.png");
BarcodeReader.Read(File.ReadAllBytes("path/to/image.png"));
BarcodeReader.Read(File.OpenRead("path/to/image.png"));
// All of these work — no BitmapImage conversion needed
BarcodeReader.Read("path/to/image.png");
BarcodeReader.Read(File.ReadAllBytes("path/to/image.png"));
BarcodeReader.Read(File.OpenRead("path/to/image.png"));
' All of these work — no BitmapImage conversion needed
BarcodeReader.Read("path/to/image.png")
BarcodeReader.Read(File.ReadAllBytes("path/to/image.png"))
BarcodeReader.Read(File.OpenRead("path/to/image.png"))
$vbLabelText   $csharpLabel

您可以移除任何僅用於條碼相關圖像載入的 using System.Windows.Media.Imaging; 引入。

移除 TaskCompletionSource 模式

橋接事件驅動的 DecodeComplete 回調到非同步程式碼的 TaskCompletionSource<string> 字段已經被完全移除。 BarcodeReader.Read() 是同步的,可以直接返回結果。

如果您的調用程式碼是 await service.ReadBarcodeAsync(path),您可以簡化為:

// Before
var value = await service.ReadBarcodeAsync(imagePath);

// After
var value = BarcodeReader.Read(imagePath).FirstOrDefault()?.Value ?? "No barcode found";
// Before
var value = await service.ReadBarcodeAsync(imagePath);

// After
var value = BarcodeReader.Read(imagePath).FirstOrDefault()?.Value ?? "No barcode found";
$vbLabelText   $csharpLabel

如果您希望為下游調用者保留非同步接口,而不阻塞執行緒:

public Task<string> ReadBarcodeAsync(string imagePath)
{
    return Task.Run(() =>
    {
        var results = BarcodeReader.Read(imagePath);
        return results.FirstOrDefault()?.Value ?? "No barcode found";
    });
}
public Task<string> ReadBarcodeAsync(string imagePath)
{
    return Task.Run(() =>
    {
        var results = BarcodeReader.Read(imagePath);
        return results.FirstOrDefault()?.Value ?? "No barcode found";
    });
}
Option Strict On



Public Function ReadBarcodeAsync(imagePath As String) As Task(Of String)
    Return Task.Run(Function()
                        Dim results = BarcodeReader.Read(imagePath)
                        Return If(results.FirstOrDefault()?.Value, "No barcode found")
                    End Function)
End Function
$vbLabelText   $csharpLabel

屬性名稱更改

Infragistics IronBarcode
e.Value(在 ReaderDecodeArgs 中) result.Value
e.Symbology(在 ReaderDecodeArgs 中) result.Format
barcode.Data BarcodeWriter.CreateBarcode() 的第一個參數
barcode.Symbology = Symbology.Code128 BarcodeEncoding.Code128(參數)
barcode.SaveTo(path) .SaveAsPng(path) / .SaveAsJpeg(path) / .SaveAsSvg(path)

遷移檢查清單

使用這些 grep 樣式檢查您程式碼庫中所有 Infragistics 條碼的使用情況:

# Namespace references
grep -r "Infragistics.WinForms.Barcode\|Infragistics.Win.UltraWinBarcode" --include="*.cs" .
grep -r "Infragistics.Controls.Barcodes" --include="*.cs" .
grep -r "Infragistics.WPF.BarcodeReader" --include="*.csproj" .

# WinForms generation types
grep -r "UltraWinBarcode" --include="*.cs" .
grep -r "Symbology\.Code" --include="*.cs" .
grep -r "barcode\.SaveTo(" --include="*.cs" .

# WPF reading types and patterns
grep -r "new BarcodeReader()" --include="*.cs" .
grep -r "DecodeComplete" --include="*.cs" .
grep -r "ReaderDecodeArgs" --include="*.cs" .
grep -r "SymbologyType\." --include="*.cs" .
grep -r "e\.Value\|ReaderDecodeArgs" --include="*.cs" .
grep -r "BitmapImage" --include="*.cs" .

# TaskCompletionSource usage (likely barcode-related if near above patterns)
grep -r "TaskCompletionSource" --include="*.cs" .
# Namespace references
grep -r "Infragistics.WinForms.Barcode\|Infragistics.Win.UltraWinBarcode" --include="*.cs" .
grep -r "Infragistics.Controls.Barcodes" --include="*.cs" .
grep -r "Infragistics.WPF.BarcodeReader" --include="*.csproj" .

# WinForms generation types
grep -r "UltraWinBarcode" --include="*.cs" .
grep -r "Symbology\.Code" --include="*.cs" .
grep -r "barcode\.SaveTo(" --include="*.cs" .

# WPF reading types and patterns
grep -r "new BarcodeReader()" --include="*.cs" .
grep -r "DecodeComplete" --include="*.cs" .
grep -r "ReaderDecodeArgs" --include="*.cs" .
grep -r "SymbologyType\." --include="*.cs" .
grep -r "e\.Value\|ReaderDecodeArgs" --include="*.cs" .
grep -r "BitmapImage" --include="*.cs" .

# TaskCompletionSource usage (likely barcode-related if near above patterns)
grep -r "TaskCompletionSource" --include="*.cs" .
SHELL

處理每一個匹配:

  • UltraWinBarcode 使用情況:替換為 BarcodeWriter.CreateBarcode()
  • new BarcodeReader() + DecodeComplete 塊:替換為 BarcodeReader.Read()
  • SymbologyType 標誌列表:完全刪除
  • 用於條碼輸入的 BitmapImage 載入:替換為直接路徑/流/字節
  • ReaderDecodeArgs e 回調正文:提取 e.Valuee.Symbologyresult.Format
  • 條碼服務類上的 IDisposable:如果唯一的可刪除資源是 _reader,則移除

功能比較

功能 Infragistics Barcode IronBarcode
WinForms 條碼生成 是(UltraWinBarcode)
WinForms 條碼閱讀 不提供
WPF 條碼生成 是(XamBarcode)
WPF 條碼閱讀 是(事件驅動) 是(同步)
ASP.NET Core 不提供
控制台 / 工作者服務 不提供
Docker / Linux 不提供
Azure 功能 不提供
Blazor 伺服器 不提供
自動格式檢測 否——需要 SymbologyType 標誌
PDF 條碼讀取 不提供 是(原生)
支持執行緒安全並行閱讀
批量閱讀(內建)
QR 錯誤校正控制 不提供
靜態 API(無實例)
需要 Suite 訂閱 是(Infragistics Ultimate,每年2,399美元起)
永久許可 是——從 749 美元開始

遷移後的主要優勢

WinForms 的條碼閱讀。 如果遷移是由 WinForms 閱讀需求驅動的,這就是主要結果:BarcodeReader.Read(imagePath) 能夠在 WinForms 中編譯和運行,無需額外的設置。

ASP.NET Core 和控制台支持。 Web API、背景服務和控制台工具現在都可以使用同一個套件生成和閱讀條碼。 如果您為這些項目型別維護了單獨的工具鏈或解決方案,這些都已消失。

無事件處理器樣板。 DecodeComplete 處理器、TaskCompletionSource 桥接器、BitmapImage 載入和 Dispose() 清理都被移除。 剩下的就是條碼邏輯。

無符號規範。 由於缺少 SymbologyType 標誌而靜默失敗的格式現在將被正確檢測到。 自動檢測涵蓋所有50多種格式,無需配置。

所有項目型別都使用同一服務類。 一個使用 BarcodeReader.Read()BarcodeWriter.CreateBarcode() 的單一 BarcodeService 包裝器可以在 WinForms、WPF、ASP.NET Core 和解決方案中的任何其他項目中共享。 無框架特定的實現。

不需要單獨的 Infragistics Ultimate 訂閱僅用於條碼功能。 如果條碼支持是您項目擁有 Infragistics Ultimate 訂閱的唯一原因,或是一個重要因素,IronBarcode 是一個獨立的套件,提供從 749 美元起的永久授權選項,適用於單一開發者。

一旦您找到了所有 Infragistics 條碼的使用情況,遷移本身便頗為機械化。 最複雜的部分是 WPF 服務類的替換,即便如此也只是移除類和用 BarcodeReader.Read() 替換調用而已。 生成的遷移是一對一的屬性/方法重命名。 您得到的是一個單一、一致的 API,在您需要的每種項目型別中都能運行。

請注意Infragistics 是其相應所有者的註冊商標。 本網站與 Infragistics 無關,未經其認可或贊助。 所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供資訊參考,並反映了撰寫時公開可用的資訊。

常見問題

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

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

如何用IronBarcode替換Infragistics的API調用?

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

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

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

在遷移期間我需要同時安裝Infragistics Barcode和IronBarcode嗎?

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

IronBarcode的NuGet包名稱是什麼?

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

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

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

從Infragistics遷移後,IronBarcode是否能自動檢測所有條碼格式?

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

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

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

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

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

從Infragistics 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天。
聊天
電子郵件
給我打電話