如何在 C# 中為條碼新增邊距

This article was translated from English: Does it need improvement?
Translated
View the article in English

條碼的可靠性不僅取決於條形和空格。 程式碼周圍的空白區域,稱為"靜默區"或邊距,同樣至關重要。 這個靜默區使掃描器能夠將條碼與標籤上的其他元素(如文字或圖形)區分開來。

如果沒有足夠的餘量,掃描可能會失敗,或者更糟的是,會傳回錯誤的資料。 這在物流和零售業尤其重要,因為掃描失敗會導致巨大的時間和經濟損失。

IronBarcode 提供了簡單的方法來配置這些邊距,確保條碼每次都能準確掃描。在本教學中,我們將探討 IronBarcode 提供的設定條碼邊距的方法。

開始使用 IronBarcode

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

第一步:
green arrow pointer



設定所有邊距

確保適當安靜區域的最簡單、最直接的方法是使用SetMargins方法。 此方法接受一個整數,表示要新增到條碼所有四個邊的空白邊框的像素數。

在這個範例中,我們將建立一個條碼,將邊距設為 100 像素,然後使用SaveAsPng儲存結果。

:path=/static-assets/barcode/content-code-examples/how-to/setting-margin-barcode.cs
using IronBarCode;

// Create a QR code
GeneratedBarcode qrcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode",
    BarcodeWriterEncoding.QRCode
);

// Set consistent margins around the QR code
qrcode.SetMargins(100);
// Save the QR code as a PNG image
qrcode.SaveAsPng("QRCode.png");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出

帶邊距的條碼

從輸出結果可以看出,條碼周圍應用了 100 像素的均勻正方形邊距。

設定每邊邊距

除了套用統一邊距外,IronBarcode 還允許您使用SetMargins方法的重載為每一側指定單獨的邊距。 此重載接受四個整數參數,分別表示上邊距、右邊距、下邊距和左邊距。

在以下範例中,我們定義了具體的邊距:頂部和底部為 10 像素,左側和右側為 5 像素。

:path=/static-assets/barcode/content-code-examples/how-to/setting-multiple-margin.cs
using IronBarCode;

// Create a QR code
GeneratedBarcode qrcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode",
    BarcodeWriterEncoding.QRCode
);

// Set the QR code dimensions 10 pixel on top and bottom, 5 pixels on left and right
qrcode.SetMargins(10, 5, 10, 5);


// Save the QR code as a PNG file
qrcode.SaveAsPng("QRCodeValue.png");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出

個人邊距條碼

常見問題解答

什麼是 BarCode 中的靜區?

靜區(或邊緣)是條碼周圍的空白區域。它對於幫助掃描器區分條碼與其他元素(如文字或圖形)至關重要。

為什麼邊距對於 BarCode 掃描很重要?

邊界可清楚區分 BarCode 與其他元素,避免掃描失敗和錯誤的資料擷取。這在物流和零售業尤其重要,可避免時間和財務損失。

如何使用 IronBarcode 設定條碼的邊距?

IronBarcode 提供稱為「SetMargins」的直接方法,可讓您對條碼套用統一或個別的邊距,確保掃描準確無誤。

我可以用 IronBarcode 在條碼的每一邊應用不同的邊距嗎?

是的,IronBarcode 允許您使用'SetMargins'方法的重載來指定每一面的個別邊距,接受上、右、下、左邊距的四個整數參數。

使用 IronBarcode 設定條碼邊距有什麼好處?

IronBarcode 簡化了配置條碼邊距的過程,確保每次都能正確設定以進行可靠的掃描。

如何開始使用 IronBarcode 設定條碼的邊距?

首先下載 IronBarcode C# 函式庫,使用「CreateBarcode」產生條碼,再使用「SetMargins」套用邊界。最後,使用「SaveAsPng」將條碼儲存為影像。

IronBarcode 提供了什麼方法來為條碼套用統一的邊距?

IronBarcode 提供了'SetMargins'方法,它接受單一整數以在條碼的四邊套用統一的邊距。

是否可以使用 IronBarcode 將帶有邊距的條碼儲存為影像?

是的,在設置所需的邊距後,您可以使用 IronBarcode 中的'SaveAsPng'功能將條碼保存為圖像。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表