跳至頁尾內容
影片

如何在C#中將條碼作為流建立

從Barcode4NET遷移到IronBarcode

此遷移指南涵蓋了去除Barcode4NET的依賴,並用IronBarcode來替代,提供逐步指導、程式碼比較以及.NET開發者進行此轉換的實際範例。 Barcode4NET在沒有正式公告的情況下達到了生命終止——新許可證停止銷售,從未發佈NuGet包,並且該程式庫僅在.NET Framework上運行。 本指南是實用且具體的:涵蓋了移除DLL引用、通過NuGet安裝IronBarcode及轉換生成程式碼。 它還涵蓋了遷移後可用的閱讀和PDF功能,這些功能在Barcode4NET中根本不可用。

為什麼從Barcode4NET遷移

從Barcode4NET遷移的團隊報告了這些一致的觸發因素:

無新許可證可用:Barcode4NET的許可證不再通過ComponentSource或任何其它渠道銷售。 如果開發者加入了使用Barcode4NET的團隊,沒有機制為他們購買席位。 加入項目的承包商或需要處理條碼功能的第二位開發者無法在原始產品條款下合法許可。

無NuGet包:這不僅僅是一個小不便——這是與現代.NET工作流程的根本不相容。 每個複製庫存庫的開發者必須知道DLL的位置。 CI/CD管道必須手動配置才能定位它。 dotnet restore命令無法解決此問題。 DLL要麼檢入源程式碼控制,要麼維護在私人構件儲存中,兩者均需要持續的手動維護。

不支持.NET 5, 6, 7, 8或9:Barcode4NET專為.NET Framework設計。 當組織升級其目標框架到net8.0時,構建中斷,因為程式庫是編譯在不再被引用的.NET Framework組件上。 不會有補丁到來。 如果團隊需要遷移到現代.NET以提高性能、進行Linux或Docker部署,或獲得長期的微軟支持,Barcode4NET是個難以逾越的障礙。

無安全修補程式:程式庫凍結在開發停止時的狀態。 自那時以來發現的任何安全問題都無法永遠解決。 沒有問題追蹤器,無法聯繫的維護者,也沒有供應商響應過程。

無錯誤修正:如果某個符號變體或條碼尺寸的極端情況出現渲染問題,沒有解決途徑。 程式庫不會更新。

基本問題

最常見的發現時刻是開發者在.NET現代化項目過程中升級目標框架時。 構建失敗,因為Barcode4NET的DLL是編譯在不再提供的新目標的.NET Framework組件上。 問題不在於生成API本身——而是不能向前移動的底層依賴性。

// Barcode4NET — locked to .NET Framework, no NuGet package
// Requires manual DLL reference in every .csproj:
// <Reference Include="Barcode4NET">
//   <HintPath>..\ThirdParty\Barcode4NET\Barcode4NET.dll</HintPath>
// </Reference>
using Barcode4NET;
using System.Drawing;
using System.Drawing.Imaging;

var barcode = new Barcode4NET.Barcode();
barcode.Symbology = Symbology.Code128;
barcode.Data = "ITEM-12345";
barcode.Width = 300;
barcode.Height = 100;
Bitmap barcodeImage = barcode.GenerateBarcode();
barcodeImage.Save(outputPath, ImageFormat.Png);
// Barcode4NET — locked to .NET Framework, no NuGet package
// Requires manual DLL reference in every .csproj:
// <Reference Include="Barcode4NET">
//   <HintPath>..\ThirdParty\Barcode4NET\Barcode4NET.dll</HintPath>
// </Reference>
using Barcode4NET;
using System.Drawing;
using System.Drawing.Imaging;

var barcode = new Barcode4NET.Barcode();
barcode.Symbology = Symbology.Code128;
barcode.Data = "ITEM-12345";
barcode.Width = 300;
barcode.Height = 100;
Bitmap barcodeImage = barcode.GenerateBarcode();
barcodeImage.Save(outputPath, ImageFormat.Png);
Imports Barcode4NET
Imports System.Drawing
Imports System.Drawing.Imaging

Dim barcode As New Barcode4NET.Barcode()
barcode.Symbology = Symbology.Code128
barcode.Data = "ITEM-12345"
barcode.Width = 300
barcode.Height = 100
Dim barcodeImage As Bitmap = barcode.GenerateBarcode()
barcodeImage.Save(outputPath, ImageFormat.Png)
$vbLabelText   $csharpLabel
//IronBarcode— NuGet package, .NET Framework 4.6.2 through .NET 9
// dotnet add package IronBarcode
using IronBarCode;

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

BarcodeWriter.CreateBarcode("ITEM-12345", BarcodeEncoding.Code128)
    .ResizeTo(300, 100)
    .SaveAsPng(outputPath);
//IronBarcode— NuGet package, .NET Framework 4.6.2 through .NET 9
// dotnet add package IronBarcode
using IronBarCode;

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

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

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

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

IronBarcode與Barcode4NET:功能比較

功能 Barcode4NET IronBarcode
NuGet包 否——僅手動DLL 是(IronBarcode
.NET Framework 支持 是(僅.NET Framework) 是(.NET Framework 4.6.2+)
.NET Core / .NET 5+支持 是(.NET 5、6、7、8、9)
Linux / macOS支援
Docker支援
Azure / AWS Lambda
條碼生成
條碼讀取/掃描 是(BarcodeReader.Read()
PDF 條碼讀取 是(原生,無需額外程式庫)
帶標誌的QR碼 是(AddBrandLogo()
流暢可連結API
活躍維護 否(生命終止)
新許可證可用 是——Lite $749,Plus $1,499,Pro $2,999,Unlimited $5,999
安全修補程式
CI/CD管道整合 手動DLL複製步驟 標準dotnet restore

快速開始:Barcode4NET到IronBarcode遷移

可以立即開始遷移,採取以下基本步驟。

步驟 1:替換 NuGet 包

Barcode4NET從未作為NuGet包分發,因此沒有dotnet remove package命令。 手動移除它。

從源程式碼控制中刪除DLL並分階段進行刪除:

git rm -r ThirdParty/Barcode4NET/
git rm -r ThirdParty/Barcode4NET/
SHELL

從每個<Reference>元素。在解決方案中搜尋以查找需要更新的所有檔案:

grep -rl "Barcode4NET" --include="*.csproj" .
grep -rl "Barcode4NET" --include="*.csproj" .
SHELL

從包含它的每個.csproj檔案中移除此元素:

<Reference Include="Barcode4NET">
  <HintPath>..\ThirdParty\Barcode4NET\Barcode4NET.dll</HintPath>
</Reference>
<Reference Include="Barcode4NET">
  <HintPath>..\ThirdParty\Barcode4NET\Barcode4NET.dll</HintPath>
</Reference>
XML

然後安裝IronBarcode:

dotnet add package IronBarcode
dotnet add package IronBarcode
SHELL

該單一命令新增了IronBarcode並解決了所有依賴性。 無需DLL管理,無需構件儲存,無需手動複製步驟。

步驟 2:更新命名空間

替換使用Barcode4NET的每個檔案頂部的舊using指令:

// Before
using Barcode4NET;

// After
using IronBarCode;
// Before
using Barcode4NET;

// After
using IronBarCode;
Imports Barcode4NET

' After
Imports IronBarCode
$vbLabelText   $csharpLabel

步驟 3:初始化授權

在應用程式啟動時新增許可證初始化——在Startup.cs或您的應用程式引導的任何位置:

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

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

程式碼遷移範例

基礎標籤生成

最常見的Barcode4NET模式:建立物件、設置屬性、調用Bitmap,然後保存它。

Barcode4NET 方法:

using Barcode4NET;
using System.Drawing;
using System.Drawing.Imaging;

public void SaveLabel(string sku, string outputPath)
{
    var barcode = new Barcode4NET.Barcode();
    barcode.Symbology = Symbology.Code128;
    barcode.Data = sku;
    barcode.Width = 400;
    barcode.Height = 120;
    Bitmap barcodeImage = barcode.GenerateBarcode();
    barcodeImage.Save(outputPath, ImageFormat.Png);
}
using Barcode4NET;
using System.Drawing;
using System.Drawing.Imaging;

public void SaveLabel(string sku, string outputPath)
{
    var barcode = new Barcode4NET.Barcode();
    barcode.Symbology = Symbology.Code128;
    barcode.Data = sku;
    barcode.Width = 400;
    barcode.Height = 120;
    Bitmap barcodeImage = barcode.GenerateBarcode();
    barcodeImage.Save(outputPath, ImageFormat.Png);
}
Imports Barcode4NET
Imports System.Drawing
Imports System.Drawing.Imaging

Public Sub SaveLabel(sku As String, outputPath As String)
    Dim barcode As New Barcode4NET.Barcode()
    barcode.Symbology = Symbology.Code128
    barcode.Data = sku
    barcode.Width = 400
    barcode.Height = 120
    Dim barcodeImage As Bitmap = barcode.GenerateBarcode()
    barcodeImage.Save(outputPath, ImageFormat.Png)
End Sub
$vbLabelText   $csharpLabel

IronBarcode 方法:

// NuGet: dotnet add package IronBarcode
using IronBarCode;

public void SaveLabel(string sku, string outputPath)
{
    BarcodeWriter.CreateBarcode(sku, BarcodeEncoding.Code128)
        .ResizeTo(400, 120)
        .SaveAsPng(outputPath);
}
// NuGet: dotnet add package IronBarcode
using IronBarCode;

public void SaveLabel(string sku, string outputPath)
{
    BarcodeWriter.CreateBarcode(sku, BarcodeEncoding.Code128)
        .ResizeTo(400, 120)
        .SaveAsPng(outputPath);
}
Imports IronBarCode

Public Sub SaveLabel(sku As String, outputPath As String)
    BarcodeWriter.CreateBarcode(sku, BarcodeEncoding.Code128) _
        .ResizeTo(400, 120) _
        .SaveAsPng(outputPath)
End Sub
$vbLabelText   $csharpLabel

屬性設置塊合併為一個流暢的鏈。 Height屬性分配。 barcodeImage.Save()調用。 有關更多生成選項,請參見IronBarcode條碼生成文件

WinForms PictureBox顯示

如果您的Barcode4NET程式碼將MemoryStream載入字節陣列。

Barcode4NET 方法:

using Barcode4NET;
using System.Drawing;

private void RefreshBarcodeDisplay(string data)
{
    var barcode = new Barcode4NET.Barcode();
    barcode.Symbology = Symbology.Code128;
    barcode.Data = data;
    barcode.Width = 300;
    barcode.Height = 100;
    pictureBox1.Image = barcode.GenerateBarcode();
}
using Barcode4NET;
using System.Drawing;

private void RefreshBarcodeDisplay(string data)
{
    var barcode = new Barcode4NET.Barcode();
    barcode.Symbology = Symbology.Code128;
    barcode.Data = data;
    barcode.Width = 300;
    barcode.Height = 100;
    pictureBox1.Image = barcode.GenerateBarcode();
}
Imports Barcode4NET
Imports System.Drawing

Private Sub RefreshBarcodeDisplay(data As String)
    Dim barcode As New Barcode4NET.Barcode()
    barcode.Symbology = Symbology.Code128
    barcode.Data = data
    barcode.Width = 300
    barcode.Height = 100
    pictureBox1.Image = barcode.GenerateBarcode()
End Sub
$vbLabelText   $csharpLabel

IronBarcode 方法:

using IronBarCode;
using System.Drawing;
using System.IO;

private void RefreshBarcodeDisplay(string data)
{
    byte[] pngBytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
        .ResizeTo(300, 100)
        .ToPngBinaryData();

    using var stream = new MemoryStream(pngBytes);
    pictureBox1.Image = Image.FromStream(stream);
}
using IronBarCode;
using System.Drawing;
using System.IO;

private void RefreshBarcodeDisplay(string data)
{
    byte[] pngBytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
        .ResizeTo(300, 100)
        .ToPngBinaryData();

    using var stream = new MemoryStream(pngBytes);
    pictureBox1.Image = Image.FromStream(stream);
}
Imports IronBarCode
Imports System.Drawing
Imports System.IO

Private Sub RefreshBarcodeDisplay(data As String)
    Dim pngBytes As Byte() = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128) _
        .ResizeTo(300, 100) _
        .ToPngBinaryData()

    Using stream As New MemoryStream(pngBytes)
        pictureBox1.Image = Image.FromStream(stream)
    End Using
End Sub
$vbLabelText   $csharpLabel

MemoryStream步驟是唯一的結構增補。 其他所有都是從屬性設置模式到流暢鏈的直接翻譯。

QR 碼生成

Barcode4NET 方法:

using Barcode4NET;

var barcode = new Barcode4NET.Barcode();
barcode.Symbology = Symbology.QRCode;
barcode.Data = "https://example.com/product/99";
barcode.Width = 300;
barcode.Height = 300;
Bitmap qrImage = barcode.GenerateBarcode();
qrImage.Save("qr.png", System.Drawing.Imaging.ImageFormat.Png);
using Barcode4NET;

var barcode = new Barcode4NET.Barcode();
barcode.Symbology = Symbology.QRCode;
barcode.Data = "https://example.com/product/99";
barcode.Width = 300;
barcode.Height = 300;
Bitmap qrImage = barcode.GenerateBarcode();
qrImage.Save("qr.png", System.Drawing.Imaging.ImageFormat.Png);
Imports Barcode4NET
Imports System.Drawing.Imaging

Dim barcode As New Barcode4NET.Barcode()
barcode.Symbology = Symbology.QRCode
barcode.Data = "https://example.com/product/99"
barcode.Width = 300
barcode.Height = 300
Dim qrImage As Bitmap = barcode.GenerateBarcode()
qrImage.Save("qr.png", ImageFormat.Png)
$vbLabelText   $csharpLabel

IronBarcode 方法:

using IronBarCode;

QRCodeWriter.CreateQrCode("https://example.com/product/99", 300)
    .SaveAsPng("qr.png");
using IronBarCode;

QRCodeWriter.CreateQrCode("https://example.com/product/99", 300)
    .SaveAsPng("qr.png");
Imports IronBarCode

QRCodeWriter.CreateQrCode("https://example.com/product/99", 300) _
    .SaveAsPng("qr.png")
$vbLabelText   $csharpLabel

IronBarcode對QR碼使用專用QRCodeWriter類,這提供了存取QR特定選項,例如錯誤修正水平和品牌標誌嵌入。 傳給QRCodeWriter.CreateQrCode()的第二個參數是方形圖像的像素大小。

新增條碼讀取

Barcode4NET沒有讀取API。 如果您的應用程式處理掃描文件、驗證生成的條碼或從PDF發票中提取資料,則這項功能在無需新增第二個程式庫的情況下立即可用。

Barcode4NET 方法:

//否reading API available in Barcode4NET.
// Required a separate library (e.g., ZXing.Net) to decode barcodes.
//否reading API available in Barcode4NET.
// Required a separate library (e.g., ZXing.Net) to decode barcodes.
'否reading API available in Barcode4NET.
' Required a separate library (e.g., ZXing.Net) to decode barcodes.
$vbLabelText   $csharpLabel

IronBarcode 方法:

using IronBarCode;

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

// Read all barcodes from a PDF — pages are handled automatically
var pdfResults = BarcodeReader.Read("invoice-batch.pdf");
foreach (var result in pdfResults)
{
    Console.WriteLine($"Page {result.PageNumber}: {result.Value}");
}
using IronBarCode;

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

// Read all barcodes from a PDF — pages are handled automatically
var pdfResults = BarcodeReader.Read("invoice-batch.pdf");
foreach (var result in pdfResults)
{
    Console.WriteLine($"Page {result.PageNumber}: {result.Value}");
}
Imports IronBarCode

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

' Read all barcodes from a PDF — pages are handled automatically
Dim pdfResults = BarcodeReader.Read("invoice-batch.pdf")
For Each result In pdfResults
    Console.WriteLine($"Page {result.PageNumber}: {result.Value}")
Next
$vbLabelText   $csharpLabel

從PDF讀取無需額外程式庫。 BarcodeReader.Read() 在本地處理圖像和PDF輸入。 查看條碼讀取文件以獲取配置選項,包括讀取速度和多條碼檢測。

升級目標框架

Barcode4NET一旦被移除,.NET Framework限制隨之消除。 更新.csproj中:

Barcode4NET 方法:


<PropertyGroup>
  <TargetFramework>net472</TargetFramework>
</PropertyGroup>

<PropertyGroup>
  <TargetFramework>net472</TargetFramework>
</PropertyGroup>
XML

IronBarcode 方法:


<PropertyGroup>
  <TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<PropertyGroup>
  <TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
XML

IronBarcode支援.NET Framework 4.6.2到.NET 9,因此目標框架可以升級到您組織所需的框架而不受任何條碼程式庫限制。

ASP.NET Core 控制器 - 作為圖像響應返回條碼

如果您同時現代化Web Form應用程式,這是從控制器行動返回條碼圖像的ASP.NET Core模式:

Barcode4NET 方法:

// Barcode4NET was not compatible with ASP.NET Core.
// Web Forms HttpHandler was required for image delivery.
// Barcode4NET was not compatible with ASP.NET Core.
// Web Forms HttpHandler was required for image delivery.
' Barcode4NET was not compatible with ASP.NET Core.
' Web Forms HttpHandler was required for image delivery.
$vbLabelText   $csharpLabel

IronBarcode 方法:

using IronBarCode;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/barcodes")]
public class BarcodesController : ControllerBase
{
    [HttpGet("{data}")]
    public IActionResult Get(string data)
    {
        byte[] pngBytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
            .ResizeTo(400, 120)
            .ToPngBinaryData();

        return File(pngBytes, "image/png");
    }
}
using IronBarCode;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/barcodes")]
public class BarcodesController : ControllerBase
{
    [HttpGet("{data}")]
    public IActionResult Get(string data)
    {
        byte[] pngBytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
            .ResizeTo(400, 120)
            .ToPngBinaryData();

        return File(pngBytes, "image/png");
    }
}
Imports IronBarCode
Imports Microsoft.AspNetCore.Mvc

<ApiController>
<Route("api/barcodes")>
Public Class BarcodesController
    Inherits ControllerBase

    <HttpGet("{data}")>
    Public Function [Get](data As String) As IActionResult
        Dim pngBytes As Byte() = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128) _
            .ResizeTo(400, 120) _
            .ToPngBinaryData()

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

.ToPngBinaryData() 直接返回 byte[] ,這插入到File()結果中而無需任何中間轉換。

Barcode4NET API 到IronBarcode映射參考

Barcode4NET IronBarcode
new Barcode4NET.Barcode() BarcodeWriter.CreateBarcode(data, encoding)
barcode.Symbology = Symbology.Code128 BarcodeEncoding.Code128 (參數給CreateBarcode
barcode.Data = "ITEM-12345" CreateBarcode()的第一個參數
barcode.Width = 300; barcode.Height = 100 .ResizeTo(300, 100)
barcode.GenerateBarcode() 返回 Bitmap .SaveAsPng(path) / .ToPngBinaryData()
Symbology.QRCode BarcodeEncoding.QRCode
Symbology.Code39 BarcodeEncoding.Code39
Symbology.EAN13 BarcodeEncoding.EAN13
Symbology.UPCA BarcodeEncoding.UPCA
<Reference Include="Barcode4NET"> <PackageReference Include="IronBarcode" />
.NET Framework only .NET Framework 4.6.2 through .NET 9
只限生成 生成 + 讀取(BarcodeReader.Read()
無PDF支援 BarcodeReader.Read("doc.pdf") 原生

常見遷移問題及解決方案

問題1:沒有dotnet remove package用於DLL引用

Barcode4NET:由於Barcode4NET從未是NuGet包,因此沒有包管理器命令可以移除它。 .csproj檔案中移除,並且必須明確從源程式碼控制中刪除DLL目錄。

解決方案:運行搜尋以定位引用DLL的所有項目檔案,然後手動刪除這些元素:

grep -rn "Barcode4NET" --include="*.csproj" .
grep -rn "Barcode4NET" --include="*.csproj" .
SHELL

不要將DLL引用保留在新的NuGet引用旁邊——這樣會導致從不明確型別解析產生構建錯誤。

問題2:返回型別從Bitmap更改為字節陣列

Barcode4NET:GenerateBarcode() 返回System.Drawing.Bitmap。 將返回值分配給PictureBox——的程式碼必須被更新。

解決方案:MemoryStream載入字節陣列:

byte[] pngBytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
    .ResizeTo(300, 100)
    .ToPngBinaryData();

using var stream = new MemoryStream(pngBytes);
pictureBox1.Image = Image.FromStream(stream);
byte[] pngBytes = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
    .ResizeTo(300, 100)
    .ToPngBinaryData();

using var stream = new MemoryStream(pngBytes);
pictureBox1.Image = Image.FromStream(stream);
Imports System.IO

Dim pngBytes As Byte() = BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128) _
    .ResizeTo(300, 100) _
    .ToPngBinaryData()

Using stream As New MemoryStream(pngBytes)
    pictureBox1.Image = Image.FromStream(stream)
End Using
$vbLabelText   $csharpLabel

在解決方案中搜尋.GenerateBarcode()將定位所有需要更新的調用位置。

問題3:符號枚舉命名空間更改

Barcode4NET:使用Barcode4NET命名空間的類似值。

解決方案:BarcodeEncoding枚舉值替代。 所有常用名稱都保留——只改變型別名稱:

// Before
barcode.Symbology = Symbology.Code128;

// After
BarcodeEncoding.Code128  // passed as parameter to CreateBarcode()
// Before
barcode.Symbology = Symbology.Code128;

// After
BarcodeEncoding.Code128  // passed as parameter to CreateBarcode()
' Before
barcode.Symbology = Symbology.Code128

' After
BarcodeEncoding.Code128  ' passed as parameter to CreateBarcode()
$vbLabelText   $csharpLabel

解決方案範圍內將BarcodeEncoding.涵蓋了大部分情況。 檢查每個替換以確認上下文。

問題4:寬度和高度現在是方法而不是屬性

Barcode4NET:大小是通過兩個單獨的屬性分配進行配置的:barcode.Width = 300; barcode.Height = 100;

解決方案:使用.ResizeTo(width, height)鏈式方法。 這在語法上有顯著不同,無法被簡單的搜尋替換捕捉——搜尋.Height =在條碼相關程式碼中並更新每個出現:

// Before
barcode.Width = 300;
barcode.Height = 100;

// After — chained on the CreateBarcode call
BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
    .ResizeTo(300, 100)
    .SaveAsPng(outputPath);
// Before
barcode.Width = 300;
barcode.Height = 100;

// After — chained on the CreateBarcode call
BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128)
    .ResizeTo(300, 100)
    .SaveAsPng(outputPath);
' Before
barcode.Width = 300
barcode.Height = 100

' After — chained on the CreateBarcode call
BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128) _
    .ResizeTo(300, 100) _
    .SaveAsPng(outputPath)
$vbLabelText   $csharpLabel

Barcode4NET 遷移清單

遷移前任務

在開始之前在您的解決方案中運行這些搜尋。 它們提供關於遷移範圍的準確畫面:

# Find all Barcode4NET usages in source files
grep -rn "Barcode4NET" --include="*.cs" .

# Find the using directive
grep -rn "using Barcode4NET" --include="*.cs" .

# Find object creation
grep -rn "new Barcode4NET.Barcode()" --include="*.cs" .

# Find Symbology enum usage
grep -rn "Symbology\." --include="*.cs" .

# Find GenerateBarcode calls
grep -rn "\.GenerateBarcode()" --include="*.cs" .

# Find the DLL reference in project files
grep -rn "Barcode4NET" --include="*.csproj" .

# Find DLL references in build scripts
grep -rn "Barcode4NET" --include="*.yml" .
grep -rn "Barcode4NET" --include="*.yaml" .
grep -rn "ThirdParty/Barcode4NET" .
# Find all Barcode4NET usages in source files
grep -rn "Barcode4NET" --include="*.cs" .

# Find the using directive
grep -rn "using Barcode4NET" --include="*.cs" .

# Find object creation
grep -rn "new Barcode4NET.Barcode()" --include="*.cs" .

# Find Symbology enum usage
grep -rn "Symbology\." --include="*.cs" .

# Find GenerateBarcode calls
grep -rn "\.GenerateBarcode()" --include="*.cs" .

# Find the DLL reference in project files
grep -rn "Barcode4NET" --include="*.csproj" .

# Find DLL references in build scripts
grep -rn "Barcode4NET" --include="*.yml" .
grep -rn "Barcode4NET" --include="*.yaml" .
grep -rn "ThirdParty/Barcode4NET" .
SHELL

記下每種模式的檔案數量。 記錄基於座標的繪圖程式碼、字體使用模式以及任何複製DLL的構建腳本步驟。

程式碼更新任務

  1. 從源程式碼控制中刪除ThirdParty/Barcode4NET/ (git rm -r)
  2. 從所有<Reference Include="Barcode4NET">
  3. 從構建腳本和CI/CD管道檔案中移除手動DLL複製步驟
  4. 為解決方案中的每個項目運行dotnet add package IronBarcode
  5. 在應用程式啟動中新增IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
  6. using Barcode4NET;
  7. new Barcode4NET.Barcode()區塊
  8. 在整個過程中用Symbology.X
  9. with.ResizeTo(N, M)替換 barcode.Width = N; barcode.Height = M;
  10. .GenerateBarcode()分配
  11. 更新MemoryStream模式
  12. 如有從TargetFramework
  13. 運行完整構建並修復任何剩餘的編譯錯誤

遷移後測試

  • 比較視覺條碼輸出與已知良好樣本以確認外觀
  • 驗證條碼掃描器硬體可以讀取所有受影響的符號生成的條碼
  • 測試BarcodeEncoding值是否與Code128、Code39、EAN-13、UPC-A和QR碼的預期輸出一致
  • 驗證MemoryStream模式在適用的WinForms控制中正確顯示
  • 確認CI/CD管道僅使用dotnet restore完成,無需手動DLL步驟
  • BarcodeReader.Read()作為遷移的一部分新增,測試條碼讀取功能

遷移到IronBarcode的主要優勢

恢復為新團隊成員授權的能力:IronBarcode是活動的商業產品,提供按確定價位的永久許可證。 新開發者、承包商和團隊新增成員可以立即獲得許可。 開發者加入並無法合法處理條碼相關程式碼的情況不再存在。

標準NuGet依賴管理:遷移後,dotnet restore處理所有問題。 新開發者克隆庫存庫,恢復套件並構建而不需要了解DLL位置、構件儲存或手動複製程式。 Barcode4NET的DLL分發在每個CI/CD管道和入門工作流程中創造的摩擦被消除。

完整.NET版本自由:去除Barcode4NET限制解鎖了.NET現代化項目。 目標框架可以升級到.NET 8、.NET 9或未來版本而不受任何條碼程式庫限制。 Linux部署、Docker容器化和雲無伺服器運行時全部可用。

無需第二個程式庫的條碼讀取:BarcodeReader.Read()本地解碼圖像和PDF中的條碼。 以前將Barcode4NET用於生成,並使用ZXing.Net或其他程式庫進行讀取的團隊可以合併為單一維護依賴。 PDF條碼讀取不需要額外的PDF程式庫。

安全性和合規性姿態:IronBarcode定期獲得安全修補,並在當前CVE資料庫中進行跟蹤。 軟體材料清單審核將不再將Barcode4NET視為生命周期結束的商業依賴,沒有供應商響應過程。 Barcode4NET在安全審核中創造的修復發現,隨著遷移的完成而解決。

活躍的開發節奏:IronBarcode與新的.NET版本同步定期發佈更新。 未來.NET版本的相容性更新通過套件交付,因此程式庫不會在供應商沉默時凍結在當前狀態。

常見問題

我為什麼應該從Barcode4.NET遷移到IronBarcode?

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

我該如何用IronBarcode替換Barcode4.NET API調用?

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

從Barcode4.NET遷移到IronBarcode需要多少程式碼更改?

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

在遷移期間我需要同時安裝Barcode4.NET和IronBarcode嗎?

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

IronBarcode的NuGet包名稱是什麼?

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

相比Barcode4.NET,IronBarcode如何簡化Docker部署?

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

從Barcode4.NET遷移後,IronBarcode會自動檢測所有條形碼格式嗎?

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

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

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

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

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

從Barcode4.NET遷移到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天。
聊天
電子郵件
給我打電話