如何使用IronBarcode在C#中輸出資料格式

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
已更新: 2026年8月2日

IronBarcode提供多種條碼讀取的輸出格式,包括BarcodeImage、BarcodeType、BinaryValue、座標、尺寸、頁碼、方向、文字和值屬性。 這些格式可針對各種使用情況,以程式化的方式處理 BarCode 資料。

IronBarcode 不只是簡單地讀取條碼並在主控台中列印數值,而是提供更多的功能。 它提供多種輸出格式,為使用者處理讀取結果鋪路。 這些格式包括如條碼圖片、條碼類型、BinaryValue、座標、高度、寬度、頁碼、條碼、頁面方向、text和value等屬性。

使用者可以在程式中進一步操作這些屬性。 讓我們來探討一下如何使用這些屬性,以及在哪些用例中這些屬性會有所幫助。

快速開始:一行讀取條碼值和類型

此範例展示如何使用 IronBarcode 從圖片讀取條碼——一行程式碼即可載入,然後立即列印條碼的數值和類型。 非常適合快速入門。如需更全面的範例,請參考 條碼快速入門指南。

  1. 1使用 IronBarcode 安裝 NuGet Package Manager

    PM > Install-Package BarCode

  2. 2複製並運行這段程式碼。

    var result = IronBarCode.BarcodeReader.Read("input.png");
    Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronBarcode,並使用 免費試用
    arrow pointer

可用的輸出格式及其用例是什麼?

BarcodeResult 儲存了多種實用的屬性,清單如下:

  • BarcodeImage
  • BarcodeType
  • BinaryValue
  • 座標、Height 和 Width
  • PageNumber
  • Barcode 和 PageOrientation
  • Text 和 Value

每個屬性在 BarCode 處理工作流程中都有特定的用途。 無論是構建庫存管理系統、文件處理管道,還是品質控制應用,這些資料格式都提供了從各種來源讀取條碼的靈活性。

如何提取和儲存條碼圖像?

IronBarcode讀取圖像後,圖像中找到的條碼會以AnyBitmap類型作為BarcodeImage屬性儲存在BarcodeResult中。 屬性 BarcodeImage 儲存找到的條碼圖像。 使用者可擷取此物件以進一步處理影像,或將其儲存為永久副本。 這樣可以省去從影像中擷取 BarCode 影像的額外程式碼,提供效率與易用性。

請看下面的程式碼片段,它展示了此輸出格式的可能用例:

using IronBarCode;
using IronSoftware.Drawing;
using System.Collections.Generic;

// Read barcode from PDF file
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Create list for barcodes
List<AnyBitmap> barcodeList = new List<AnyBitmap>();

foreach (BarcodeResult barcode in result)
{
    barcodeList.Add(barcode.BarcodeImage);
}

// Create multi-page TIFF
AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif");

上面的程式碼片段說明了此輸出格式的一個使用案例。 具體而言,它會從 PDF 文件中偵測到的 BarCode 建立多頁 TIFF 影像。 首先,我們掃描或偵測樣本 PDF 中的條碼。 然後,我們建立一個 AnyBitmap 列表,將 BarcodeImage 屬性中的資訊儲存在此。 最後,我們使用此列表通過 CreateMultiFrameTiff 方法生成多頁的 TIFF。 此技術在處理多頁 GIF 和 TIFF 檔時特別實用。

請注意: BarcodeImage屬性從BarcodeResult中只儲存在讀取過程中找到的條碼圖像,而不是整個輸入圖像本身。

如何以程式方式識別不同的條碼類型?

此屬性有助於判斷輸入影像或文件中存在何種類型的 BarCode。 但是,限制條件是圖像內的條碼類型必須是 IronBarcode 所支援和可讀取的。 要了解 IronBarcode 支援的條碼類型,請參考這篇 文章。 另外,您可以查看完整版支援的條碼格式清單,以確保符合您的具體需求。

下面的程式碼片段示範如何透過列印值到主控台來擷取影像中的 BarCode 值和 BarCode 類型。

using IronBarCode;
using System;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("bc3.png");

// Output barcode type to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine("The barcode value is " + barcode.ToString() + " and the barcode type is " + barcode.BarcodeType);
}

從上面的程式碼片段中,我們通過在輸入圖像上呼叫 BarcodeReader.Read() 方法進行條碼讀取。 這會傳回一個 BarcodeResults 物件,儲存了圖像中所有可用條碼的 BarcodeResult。 接下來,我們迭代這個 BarcodeResults 物件,檢索 BarcodeResult 並將條碼的 value 和 type 輸出到主控台。 此方法適用於各種類型的條碼,包括像 Code 39 條碼 這樣的專業格式。

何時應使用二進位數值輸出?

使用 IronBarcode,使用者可以透過存取 BarcodeResult 物件的 BinaryValue 屬性獲取條碼 value 的位元組陣列。 這讓使用者能在程式中進一步操作條碼 value。 二進位 value 輸出在處理加密資料、編碼在條碼中的檔案附件或與需要位元組級資料處理的系統整合時特別有用。

下面的程式碼片段展示了將條碼 value 作為二進位資料進行檢索的一個實例用例:

using IronBarCode;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");

int i = 1;
foreach (BarcodeResult barcode in result)
{
    var binaryValue = barcode.BinaryValue;
    var barcodeType = IronBarCode.BarcodeEncoding.QRCode;

    // Create QR code
    GeneratedBarcode generatedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType);

    // Export QR code
    generatedBarcode.SaveAsPng($"qrFromBinary{i}.png");
    i++;
}

透過觀察上面的程式碼片段,我們建立了一個簡單的程序,可以將圖像中的多個條碼轉換為單獨的新的二進位編碼檔案。 首先,我們掃描範例 PNG 影像中的條碼。 一旦檢測到這些條碼,我們便會遍歷它們,存取 BinaryValue 屬性,並用它建立新的二進位檔案。 當您需要讀取多個條碼和逐個處理其二進位資料時,這項技術非常有價值。

如何存取條碼位置和尺寸?

您可以透過 BarcodeResult 物件的 Points 屬性,取得條碼在影像檔案或文件中的位置(以 PointF[] 表示的條碼角點座標),並透過 Height 和 Width 取得其高度與寬度。這些屬性有助於擷取條碼的位置與尺寸。這些空間資訊對於需要精確定位的應用至關重要,例如自動文件處理、品質控制系統,或透過裁切區域提升條碼掃描效率。

讓我們來展示 BarCode 的位置和尺寸。

using IronBarCode;
using IronSoftware.Drawing;
using System.Linq;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");

AnyBitmap bitmap = AnyBitmap.FromFile("multiple-barcodes.png");

foreach (BarcodeResult barcode in result)
{
    PointF[] barcodePoints = barcode.Points;

    float x1 = barcodePoints.Select(b => b.X).Min();
    float y1 = barcodePoints.Select(b => b.Y).Min();

    Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width!, (int)barcode.Height!);

    bitmap = bitmap.Redact(rectangle, Color.Magenta);

    // Save the image
    bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png);
}
三個條碼範例(A、B、C),顯示不同的編碼資料但具有相似的視覺圖樣
三個經遮蔽的內容區塊,含有無法辨識的文字片段

上面的程式碼片段會遮蔽影像檔案中的多個條碼。此操作結合了 IronBarcode 和 IronDrawing 兩個程式庫。首先使用 BarcodeReader.Read() 方法讀取影像中的條碼,以取得 BarcodeResult 物件及其屬性;同時將輸入影像檔案轉換為 AnyBitmap 物件,以便套用遮蔽方法。取得 BarcodeResults 物件後,透過迴圈逐一取得影像中每個條碼的 Points、Width 和 Height(由 Points 推匯出各條碼左上角的 x1/y1),再將這些值用於 AnyBitmap.Redact() 方法的 CropRectangle 屬性。

為什麼頁碼對於多頁文件很重要?

使用者可檢索找到 BarCode 的頁碼。 對於使用包含多個 BarCode 的多頁文件,並需要知道在文件中找到的 BarCode 位置以便進一步處理的使用者而言,這是一項很有幫助的功能。 此功能對於從 PDF 文件中閱讀條碼或在企業應用中批次處理文件時至關重要。

請看下面的程式碼片段:

using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Output page number to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine("The barcode value " + barcode.ToString() + " is found on page number " + barcode.PageNumber);
}

上面的程式碼片段展示了一個使用者需要程式傳回在多頁 PDF 文件中找到的條碼value和其頁碼的用例。 該程式碼使用 BarcodeReader.ReadPdf() 方法讀取多頁 PDF 文件中的條碼,傳回 BarcodeResults 物件,儲存找到的每個 BarcodeResult。 我們會應用迴圈,遍歷物件中的每個條目,以檢索條碼的 value 以及找到條碼的頁碼。 除了這個使用案例之外,使用者也可以使用這個屬性來除錯文件中的所有 BarCode 是否都被讀取。

請注意: 此屬性傳回的值從 1 開始計數,也就是第一頁一律為一,而非零。

如何檢測條碼旋轉和頁面方向?

使用 IronBarcode,使用者可以檢索條碼方向和找到條碼的頁面方向等資訊。 要提取這兩個資訊,請存取 BarcodeResult 物件的 Rotation 和 PageOrientation 屬性。 Rotation 傳回一個表示找到的條碼旋轉角度的整數。 這項功能與 圖像方向糾正 能力一起協同工作,以確保無論掃描角度如何都能準確讀取條碼。

請看下面的程式碼片段:

using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Output page orientation and rotation to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine(barcode.Value);
    Console.WriteLine(barcode.PageOrientation);
    Console.WriteLine(barcode.Rotation);
}

上面的程式碼範例是使用附帶的範例 PDF 輸入運行,以證明使用者可以分別透過獲取 pageOrientation 和條碼 rotation 取得頁面方向和條碼旋轉。 此功能主要用於除錯目的。

請注意: IronBarcode只能讀取旋轉為BarcodeResult.PageOrientation、BarcodeResult.Rotation、0、90、180及270度的條碼。 如果條碼的旋轉值與所列值不同,IronBarcode 將不會傳回任何值。 PageOrientation 傳回一個 PageOrientation 物件,其組成包括 Portrait 或 Landscape。

文字屬性和數值屬性之間有何區別?

當然,使用 IronBarcode 時使用者會想檢索的主要屬性是其 value 和 text。 這兩個屬性經常可以互換使用,傳回相同的 value。 除此之外,使用者還可以使用 BarcodeResult.ToString() 方法達到相同的效果。 在使用專業應用或以串流形式匯出條碼資料時,這些屬性提供靈活的方法來以您偏好的格式存取條碼內容。

以下程式碼片段示範如何操作:

using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("barcodestamped3.pdf");

// Output text value to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine(barcode.Value);
    Console.WriteLine(barcode.Text);
    Console.WriteLine(barcode.ToString());
}

從上面的程式碼片段來看,使用者只需要幾行程式碼就可以使用 IronBarcode 讀取影像中的 BarCode。 在迭代 BarcodeReader.Read() 方法傳回的 BarcodeResults 後,我們將取得的 Value 和 Text 屬性,以及使用 BarcodeResult.ToString() 方法的結果輸出到主控台,以展示所有這些傳回相同的 value。

簡而言之,IronBarcode 是一個完美的 API,使用者可以對條碼執行多種操作,不僅限於條碼的寫入和解碼。 透過支援多種輸出資料格式,使用者可以對 IronBarcode 傳回的 BarcodeResult 物件進行更多操作。

常見問題

IronBarcode可用的條碼輸出格式有哪些?

IronBarcode提供多種輸出格式,如BarcodeImage、BarcodeType、BinaryValue、座標、尺寸、頁碼、方向、文本和值屬性。這些格式便於滿足.NET應用程式中的各種資料處理需求。

如何閱讀並立即列印條碼的值和類型?

使用IronBarcode,您可以從圖片中讀取條碼並通過一行代碼立即列印其值和類型。例如:var result = IronBarCode.BarcodeReader.Read("input.png"); Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");

如何使用IronBarcode提取並保存條碼圖片?

條碼讀取後,可以使用BarcodeImage屬性來訪問圖片。然後可以使用IronBarcode的工具根據需要處理或保存該圖片。

二進位值輸出在條碼處理中有什麼用途?

BinaryValue允許用戶檢索條碼的位元組陣列,對於處理加密資料或與要求位元組級資料操作的系統集成特別有用。

如何確定圖片中條碼的位置和尺寸?

IronBarcode提供如Points、Height和Width等屬性來確定條碼的位置和尺寸,這使得需要精確空間資料的應用程式可以實現。

在閱讀多頁文件時,頁碼資訊為何重要?

頁碼資訊幫助識別多頁文件中每個條碼所在的位置,從而有效處理並除錯含有大量條碼的文件。

IronBarcode如何處理條碼旋轉和頁面取向問題?

IronBarcode提供Rotation和PageOrientation屬性來幫助您識別並正確處理可能旋轉或處於不同方向頁面上的條碼。

IronBarcode中的Text和Value屬性有什麼區別?

Text和Value屬性通常返回相同的資料,代表條碼的內容。不過,它們在以首選格式訪問條碼資訊方面提供了靈活性。

使用IronBarcode的輸出資料格式的應用程式有哪些好處?

在庫存管理、文件處理和品質控制中的應用程式因IronBarcode的多樣資料格式而獲益匪淺,因為它們提供廣泛的條碼資料處理選擇。

IronBarcode能否從多頁PDF文件中讀取條碼?

是的,IronBarcode可以使用BarcodeReader.ReadPdf()等方法從多頁PDF文件中讀取條碼,並告知每個條碼所在的頁碼。

準備好開始了嗎?

NuGet 下載次數 2,575,633版本:2026.11剛剛發布