在C&#35中建立1BPP條碼影像

Curtis Chau
Curtis Chau
Updated: 2026年5月9日

IronBarcode使您能夠在C#中建立緊湊的To1BppBinaryData()等方法減少檔案大小同時保持機器可讀性。

批量條碼處理需要考慮大小和維度。 1BPP條碼影像提供了快速處理和精確機器讀取的有效解決方案。 1BPP影像為單色,只顯示黑白。每個像素使用一個位元的"0"表示黑色,"1"表示白色,或反之亦然。

1BPP條碼影像提供了最小可能的檔案大小,每個像素僅以一個位元表示。 此緊湊尺寸為儲存和傳輸條碼影像至印表機提供了效率,特別是熱敏印表機或優化資料庫儲存時。 對於處理多種條碼格式的開發者,1BPP影像在所有支持型別中提供一致的效能。

IronBarcode支持將條碼轉換為1BPP影像及其他,提供所有情況的靈活性。 無論您是從各種資料來源建立條碼還是優化現有的條碼工作流程,1BPP轉換都提供重大優勢。 此指南涵蓋了IronBarcode為建立1BPP條碼影像提供的所有選項。

1BPP)條碼影像@@--AH2EG--@@

此範例展示了IronBarcode如何僅用一行程式碼產生緊湊的單色1BPP條碼。適合高對比度掃描和快速批量處理。

  1. 1Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

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

    var img = IronBarCode.BarcodeWriter.CreateBarcode("12345", IronBarCode.BarcodeWriterEncoding.EAN8).To1BppImage();
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronBarcode,透過免費試用
    arrow pointer

我如何建立1BPP條碼影像?

使用1BPP影像很簡單。 此方法將條碼儲存為1BPP影像。 該過程類似於建立標準條碼影像但具有降低檔案大小的優勢。

using IronBarCode;

// Create a barcode with "12345" encoded in the EAN8 format
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);

// Converts the barcode into a 1bpp image
var anyBitmap = myBarcode.To1BppImage();

所得影像在大幅縮小檔案大小的同時保持條碼資訊。這在處理條碼樣式自訂時十分有用,當此時不需要顏色資訊。

為什麼To1BppImage方法能夠產生更小的檔案大小?

32位元來產生更小的檔案大小。 這獲得了比標準32:1的壓縮比。 對於典型的6KB。

哪些影像格式支持1BPP輸出?

IronBarcode支持多種影像格式進行TIFF。 1BPP影像。 TIFF也為黑白影像提供了優秀的壓縮。 有關支持的格式的詳細資訊,請參閱輸出資料格式指南。

我應該什麼時候使用1BPP影像而不是標準條碼格式?

使用1BPP影像當:

  • 使用熱敏或單色印表機
  • 儲存數千個條碼到資料庫
  • 通過有限頻寬傳輸條碼
  • 與需要單色影像的舊系統整合
  • 在檔案大小重要的批量操作中處理條碼

我如何將條碼儲存為1BPP位圖檔案?

要儲存為位圖,使用含檔案路徑的SaveAs1BppBitmap方法。 當您需要匯出條碼用於列印或歸檔時,該方法證明了其有用性。

using IronBarCode;

// Create a barcode with "12345" encoded in the EAN8 format
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);

// Save the barcode as a 1bpp Bitmap
myBarcode.SaveAs1BppBitmap("1bppImage.bmp");

使用位圖格式1BPP條碼的益處是什麼?

位圖格式為1BPP條碼提供了多種優勢:

  • 本機支持:1-bit深度支持
  • 無壓縮失真:BMP保存確切的像素資料,不像JPEG
  • 廣泛相容:幾乎所有影像軟體都支持
  • 快速處理:無需解壓縮即可讀取
  • 列印準備好:直接與大多數印表機驅動相容

我如何指定已儲存位圖的自定義檔案路徑?

使用絕對或相對路徑指定自定義檔案路徑。 在Azure或AWS Lambda等環境中工作時,確保目標目錄的寫入許可權:

// Relative path
myBarcode.SaveAs1BppBitmap("./output/barcode.bmp");

// Absolute path
myBarcode.SaveAs1BppBitmap(@"C:\BarcodeOutput\barcode.bmp");

// Using environment variables
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "barcode.bmp");
myBarcode.SaveAs1BppBitmap(path);

如果檔案路徑已經存在會發生什麼?

SaveAs1BppBitmap預設情況下會覆蓋現有檔案。 為防止意外覆蓋:

string filePath = "barcode.bmp";
if (File.Exists(filePath))
{
    // Add timestamp or increment filename
    filePath = $"barcode_{DateTime.Now:yyyyMMdd_HHmmss}.bmp";
}
myBarcode.SaveAs1BppBitmap(filePath);

我如何將條碼轉換為1BPP二進位資料?

要儲存為二進位資料,使用To1BppBinaryData方法。 二進位格式使得資料能夠在應用中使用並與現有系統整合。 該方法類似於將條碼匯出為流但針對1BPP格式進行了優化。

using IronBarCode;

// Create a barcode with "12345" encoded in the EAN8 format
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);

// Save the barcode as a 1pp binary data object
var byteData = myBarcode.To1BppBinaryData();

為何我需要二進位資料而不是影像檔案?

二進位資料相對於基於檔案的儲存提供了優勢:

  • 資料庫整合:直接儲存於BLOB欄位中
  • 記憶效能:無需檔案I/O開銷
  • API傳輸:容易通過REST API發送
  • 動態生成:無需文件系統存取即可產生條碼
  • 雲端部署:完美適用於無伺服器環境

我如何將二進位條碼資料與資料庫整合?

二進位條碼資料可以無縫地與現代資料庫整合。 此範例展示了儲存和檢索:

// Storing barcode in database
public void StoreBarcodeInDatabase(string barcodeValue, string connectionString)
{
    var barcode = BarcodeWriter.CreateBarcode(barcodeValue, BarcodeWriterEncoding.Code128);
    byte[] barcodeData = barcode.To1BppBinaryData();
    
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        string query = "INSERT INTO BarcodeTable (Value, ImageData, CreatedDate) VALUES (@value, @data, @date)";
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            cmd.Parameters.AddWithValue("@value", barcodeValue);
            cmd.Parameters.AddWithValue("@data", barcodeData);
            cmd.Parameters.AddWithValue("@date", DateTime.Now);
            
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

二進位條碼資料的常見用例是什麼?

二進位條碼資料通常用於:

  • Web API:無需臨時檔案即可返回條碼影像
  • 報告生成:將條碼直接嵌入到PDF報告中
  • 電子郵件系統:將條碼附加到自動化電子郵件中
  • 快取系統:儲存在Redis或Memcached中
  • 微服務:在服務之間傳遞條碼資料

對於進階情境,請探索非同步條碼處理以供大批量應用。

常見問題

什麼是1BPP條碼圖像,為什麼我要使用它們?

1BPP(1位元每像素)條碼圖像是僅顯示黑白的單色圖像,每個像素僅使用一位元。IronBarcode使您能夠建立這些緊湊的圖像,提供最小可能的文件大小,同時保持機器可讀性。它們非常適合熱敏列印機、資料庫儲存優化和大量條碼處理。

如何在C#中建立1BPP條碼圖像?

使用IronBarcode,您可以使用To1BppImage()方法僅用一行程式碼建立1BPP條碼圖像。只需使用:var img = IronBarCode.BarcodeWriter.CreateBarcode("12345", IronBarCode.BarcodeWriterEncoding.EAN8).To1BppImage();

有哪些方法可以生成1BPP條碼?

IronBarcode提供三個主要方法來建立1BPP條碼圖像:To1BppImage()用於標準圖像轉換,SaveAs1BppBitmap()用於直接保存為位圖文件,To1BppBinaryData()用於生成適合直接傳輸到列印機或儲存系統的二進制資料。

我可以在轉換成1BPP格式之前調整條碼大小嗎?

可以,IronBarcode允許您在1BPP轉換之前自定義條碼。您可以使用ResizeTo()方法調整尺寸,確保條碼在轉換為緊湊的1BPP格式之前滿足特定的尺寸要求。

1BPP條碼適用於所有條碼格式嗎?

IronBarcode支持將所有支持的條碼格式轉換為1BPP圖像。無論您是在處理EAN8、Code128、QR碼或任何其他支持的格式,1BPP轉換都能提供一致的性能,並在各種型別中保持機器可讀性。

使用1BPP條碼對熱敏列印機有什麼好處?

1BPP條碼圖像特別適合熱敏列印機,因為它們符合列印機的單色輸出能力。IronBarcode的1BPP轉換大幅減少文件大小,從而加快傳輸到列印機的速度,並提高列印工作流的效率,特別是在高容量情境中。

What are the advantages of using binary data for 1BPP barcodes?

Using binary data for 1BPP barcodes offers several advantages, such as easy integration with databases, memory efficiency for applications, straightforward transmission via APIs, and suitability for serverless cloud deployments.

How can I integrate 1BPP binary barcode data with a database?

You can integrate 1BPP binary barcode data with a database by storing the byte array directly in a BLOB field. This allows for efficient storage and retrieval without the need for file-based systems.

When should I use 1BPP images instead of standard barcode formats?

1BPP images should be used when working with devices or applications that require monochrome printing, when storing a large number of barcodes in databases, transmitting over restricted bandwidth, or integrating with legacy systems that need monochrome images.

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

閱讀更多

準備好開始了嗎?

Nuget Downloads 2,549,320版本:2026.11剛剛發布