跳至頁尾內容
使用IRONBARCODE

VB .NET條碼字體:如何在沒有字體依賴的情況下生成和列印條碼

如果您想知道如何在實際應用中處理 VB .NET 條碼字型,那麼您並不孤單。 條碼字型如Code 39 或 Code 128,在超過簡單示範後常常會造成問題,字型需要安裝在每個地方,列印機不總是能夠識別它們,並且當某些東西缺失時部署可能會中斷。

本文展示了一種更簡單的方法:在 VB.NET 中使用 IronBarcode 生成條碼影像。 這是我們在內部推薦的方法,因為它避免了條碼字型通常引起的部署和列印問題。

到最後,您將了解為什麼條碼字型難以滿足需求,以及如何在 Visual Basic 中以程式方式生成 Code 39 條碼(以及更多)僅需幾行程式碼。

開始免費試用 IronBarcode 並按照下面的範例在幾分鐘內生成條碼。

什麼是條碼字型以及為什麼它們不夠好?

條碼字型是特殊的字體,它將字元映射到條碼圖樣。 要使用 Code 39 字型建立條碼,開發人員將資料編碼為字串,並用起始和結束字元(通常是星號)包裝它,然後使用已安裝的字型渲染文字。 字型會將該字串視覺上轉換為可掃描的條碼。

這種基於字型的方法需要每台機器和列印機都安裝條碼字型。 Code 39 字型要求在源程式碼中手動處理結束字元和檢查碼,條碼下方的人類可讀文字需要用像 Arial 這樣的標準字型進行單獨格式化。 相容性問題經常出現,無論是在 Crystal Reports 整合中,Visual Studio 設計器視圖中,還是當為不同系統環境的部署開發時。

像 IronBarcode 這樣的程式化條碼生成器解決了這些問題。 它直接將條碼生成為條碼影像文件 — PNG、JPEG、GIF、TIFF 或 BMP — 無需安裝字型,無需分發麻煩。 每個生成的條碼影像都遵循條碼符號型別規範,輸出是一個可攜帶的條碼影像,準備好供任何列印機或文件使用。 這是本文其餘部分所涵蓋的方法。

如何在 Visual Basic 中無需字型生成 Code 39 條碼?

在 VB.NET 中生成 Code 39 條碼僅需幾行程式碼使用 IronBarcode。 以下程式碼展示了如何編碼資料並導出它:

Imports IronBarCode
Module Program
    Sub Main(args As String())
        ' Generate a Code 39 barcode from a string value
        Dim myBarcode = BarcodeWriter.CreateBarcode("HELLO-2025", BarcodeWriterEncoding.Code39)
        ' Export the generated barcode as a PNG file
        myBarcode.SaveAsPng("Code39Barcode.png")
    End Sub
End Module
Imports IronBarCode
Module Program
    Sub Main(args As String())
        ' Generate a Code 39 barcode from a string value
        Dim myBarcode = BarcodeWriter.CreateBarcode("HELLO-2025", BarcodeWriterEncoding.Code39)
        ' Export the generated barcode as a PNG file
        myBarcode.SaveAsPng("Code39Barcode.png")
    End Sub
End Module
VB .NET

輸出條碼圖像

VB .NET 條碼字型:如何在無字型依賴的情況下生成和列印條碼:圖像 1 - 程式碼範例的輸出圖像

BarcodeWriter.CreateBarcode 接受資料字串和條碼符號作為參數。 使用 BarcodeWriterEncoding.Code39,IronBarcode 處理完整的編碼規範,開始字元、結束字元和檢查碼都自動新增,無需手動字串操作。 這是相較於 Code 39 條碼字型的巨大進步,忘記星號或錯誤計算檢查碼會生成不可讀的條碼。

Code 39 是一種線性條碼符號,編碼大寫字母、數字和特殊字元,使 Code 39 條碼非常適合標籤和庫存情境。 IronBarcode 的條碼生成器還通過相同的 CreateBarcode 方法支持 Code 128、QR 碼、EAN-13、UPC-A、Data Matrix 以及數十種其他格式。 上述源程式碼適用於任何 VB.NET 專案、控制台、桌面或網頁應用程式。 每個條碼影像還可以導出為 TIFF 或 BMP 以滿足專門的列印需求。

如何樣式化並將條碼導出為多種格式?

IronBarcode 的流暢 API 使得 自訂條碼變得簡單。 以下範例程式碼展示了如何設置邊距大小、調整條碼大小,以及在 Code 128 範例中新增註釋文字:

Imports IronBarCode
Imports IronSoftware.Drawing
Module Program
    Sub Main(args As String())
        ' Generate a Code 128 barcode with styling
        Dim styledBarcode = BarcodeWriter.CreateBarcode("PKG-98765", BarcodeWriterEncoding.Code128)
        ' Set margin size in pixels around the barcode
        styledBarcode.SetMargins(10, 10, 10, 10)
        ' Resize — x dimension (width) and y dimension (height) in pixels
        styledBarcode.ResizeTo(400, 120)
        ' Add annotation with Arial font above barcodes
        styledBarcode.AddAnnotationTextAboveBarcode("Package Label")
        ' Display encoded value as readable text below barcodes
        styledBarcode.AddBarcodeValueTextBelowBarcode()
        ' Export barcodes to JPEG and GIF formats
        styledBarcode.SaveAsJpeg("Styled.jpeg")
        styledBarcode.SaveAsGif("Styled.gif")
    End Sub
End Module
Imports IronBarCode
Imports IronSoftware.Drawing
Module Program
    Sub Main(args As String())
        ' Generate a Code 128 barcode with styling
        Dim styledBarcode = BarcodeWriter.CreateBarcode("PKG-98765", BarcodeWriterEncoding.Code128)
        ' Set margin size in pixels around the barcode
        styledBarcode.SetMargins(10, 10, 10, 10)
        ' Resize — x dimension (width) and y dimension (height) in pixels
        styledBarcode.ResizeTo(400, 120)
        ' Add annotation with Arial font above barcodes
        styledBarcode.AddAnnotationTextAboveBarcode("Package Label")
        ' Display encoded value as readable text below barcodes
        styledBarcode.AddBarcodeValueTextBelowBarcode()
        ' Export barcodes to JPEG and GIF formats
        styledBarcode.SaveAsJpeg("Styled.jpeg")
        styledBarcode.SaveAsGif("Styled.gif")
    End Sub
End Module
VB .NET

輸出樣式化條碼

VB .NET 條碼字型:如何在無字型依賴的情況下生成和列印條碼:圖象 2 - 特定樣式的條碼範例

SetMargins 接受每側的像素值或單一值來設置統一的邊距大小,條碼圖像周圍的白色空間。 ResizeTo 控制條碼的 x 維度和 y 維度的像素大小。 對於物理單位,ResizeToMil 指定以千分之一英吋的寬度進行測量,以配置的 DPI 支持真實單位精度,這在掃描儀相容性對精確測量有要求時非常有用。

AddBarcodeValueTextBelowBarcode 自動將編碼字串作為人類可讀文字新增到條碼圖像下方,無需單獨的字型或繪圖程式碼。 這些功能模擬條碼字型提供的功能,但作為可移植的條碼影像物件生成條碼,您可以將其保存為 BMP、TIFF 或其他任何支持的格式。 有關導出選項的更多資訊,請參閱 輸出資料格式指南

如何從 Visual Basic 應用程式生成和列印條碼標籤?

一旦生成條碼,列印成標籤就很簡單。 條碼生成器輸出標準影像,任何列印機都能處理,無需考慮字型依賴。 以下程式碼建立一個產品標籤用的 QR 碼條碼:

Imports IronBarCode
Module Program
    Sub Main(args As String())
        ' Generate a QR code barcode for a product label
        Dim qrBarcode = QRCodeWriter.CreateQrCode("https://example.com/product/12345", 300)
        ' Export barcode to file path for printer output
        qrBarcode.SaveAsJpeg("ProductLabel.jpeg")
        ' Send the exported file to a printer via System.Drawing
        Console.WriteLine("Barcode saved — ready for printer")
    End Sub
End Module
Imports IronBarCode
Module Program
    Sub Main(args As String())
        ' Generate a QR code barcode for a product label
        Dim qrBarcode = QRCodeWriter.CreateQrCode("https://example.com/product/12345", 300)
        ' Export barcode to file path for printer output
        qrBarcode.SaveAsJpeg("ProductLabel.jpeg")
        ' Send the exported file to a printer via System.Drawing
        Console.WriteLine("Barcode saved — ready for printer")
    End Sub
End Module
VB .NET

範例程式碼生成的條碼

VB .NET 條碼字型:如何在無字型依賴的情況下生成和列印條碼:圖像 3 - 準備好列印的生成條碼

無論列印 Code 39 條碼、Code 128 運輸條碼、還是庫存使用的 QR 條碼,以下過程是相同的:生成條碼作為條碼影像,導出到一個檔案,然後發送到列印機。 在生成過程中已正確處理條碼的方向。 對於高級場景,請注意 VB.NET 條碼列印教程詳細介紹了配置和方向選項。 本文還與 VB.NET 條碼生成器教程良好搭配,以獲取其他範例模式,並查閱 IronBarcode 文件站點獲取完整的 API 參考。

結論

本文介紹了如何超越 VB.NET 中的傳統條碼字型,並使用 IronBarcode 以程式方式生成條碼。 無論您需要 Code 39 條碼、Code 128 條碼、QR 條碼或其他線性和 2D 條碼,IronBarcode 是一個便利的條碼生成器,可在編譯的 Visual Basic .NET 應用程式中產生高品質的條碼影像,對於開發 .NET 項目條碼功能的開發者來說,是一個有價值的功能。 本文中的源程式碼範例可根據您的應用程式要求適應任何條碼符號。

現在開始使用IronBarcode。
green arrow pointer

準備好獲取生產授權了嗎? 探索 IronBarcode 授權選項,找出適合您項目的方案,或從 NuGet 下載免費試用版

常見問題

using VB.NET 條碼字型有哪些挑戰?

條碼字型如 Code 39 和 Code 128 可能會在 VB.NET 應用中引發問題,因為它們需要安裝在每台機器上,並且印表機可能無法始終正確呈現它們。如果任何元件丟失,這可能會導致部署問題。

IronBarcode 如何簡化 VB.NET 中的條碼生成?

IronBarcode 允許您在 VB.NET 中生成條碼圖像,而不依賴於條碼字型。這消除了字型安裝的需要,並確保在不同平台和印表機上的一致呈現。

我可以使用 IronBarcode 生成 Code 128 條碼嗎?

是的,您可以輕鬆地使用 IronBarcode 生成 Code 128 條碼。它提供功能來建立沒有處理字型複雜性的條碼圖像。

IronBarcode 是否提供免費試用?

是的,IronBarcode 提供免費試用。這使您可以在購買前測試 VB.NET 應用中的條碼生成能力。

using IronBarcode 相較於傳統條碼字型有哪些優勢?

using IronBarcode 可消除字型依賴,確保更好的印表機相容性並簡化部署。它提供了在 VB.NET 應用中處理條碼的更可靠和更有效的方法。

Curtis Chau
技術作家

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

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

Iron 支援團隊

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