IronBarcodeで.NET用C#のBarCodeをカスタマイズしてスタイルする
IronBarcode を使用すると、開発者は C# でバーコードをカスタマイズできます。色の変更、寸法のサイズ変更、注釈の追加が可能で、ChangeBarCodeColor() や ResizeTo() などのシンプルなメソッド呼び出しを使用して完全なスタイル制御が可能です。
ここ数年、BarCodeの使用はますます一般的になり、データ、ID、WebページのURLの保存など、幅広いアプリケーションで使用されています。 アプリケーションによっては、バーコードが製品に表示されるため、スタイリングオプションの需要が高まっています。 そのため、一部のバーコード タイプでは、DataMatrix など、独特な外観が生まれています。 サポートされているフォーマットの包括的なリストについては、Supported BarCode Formats ドキュメントを参照してください。
加えて、IronBarcodeはバーコードの色、バーコードのサイズ変更、背景色のような側面を含む、バーコードのスタイルを変更するオプションをユーザーに提供します。 これは、当社のオープンソースライブラリであるIronDrawingの助けにより実現されています。 これらのスタイリング機能は、IronBarcodeの包括的なバーコード生成機能に基づいています。
クイックスタート: バーコードの色と背景をカスタマイズする
ここでは、開発者がIronBarcodeを使用してバーコードのバーと背景にカスタムカラーをすばやく適用する方法を示す簡単な例を示します。 たった1回の連鎖呼び出しで、スタイル付きBarCodeを生成することがいかに簡単であるか、おわかりいただけるでしょう。 より高度な例については、C# Barcode Image Generator チュートリアルをご覧ください。
-
IronBarcode をNuGetパッケージマネージャでインストール
PM > Install-Package BarCode -
このコード スニペットをコピーして実行します。
IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128) .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue) .ChangeBackgroundColor(IronSoftware.Drawing.Color.White) .SaveAsImage("styled.png"); -
実際の環境でテストするためにデプロイする
今日プロジェクトで IronBarcode を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- バーコードをカスタマイズしてスタイルを設定するための C# ライブラリをダウンロードします
- `ResizeTo`メソッドを使用してロスレス再レンダリングをトリガーします
- バーコード要素のサイズを変更するには、 `ResizeToMil`メソッドを使用します。
- バーコードやその背景の色を変更
- バーコードの上下にバーコード注釈を追加
バーコードのサイズを変更するにはどうすればよいですか?
どのような場合に ResizeTo メソッドを使用する必要がありますか?
バーコードのサイズ変更は、ユーザーがIronBarcodeで実現できるカスタマイズの一つです。 この機能を使用するには、ResizeTo メソッドを呼び出して、バーコードの新しい幅と高さの測定値をピクセル (px) 単位で入力するだけです。 このアクションは、バーコードのロスレス再レンダリングをトリガーします。 この方法では、バーコードの品質を維持しながら寸法を調整できるため、バーコードを特定のレイアウトや印刷サイズに合わせる必要がある場合に最適です。
using IronBarCode;
public class BarcodeResizer
{
public static void ResizeBarcode(string barcodeText, int newWidth, int newHeight)
{
// Generate a barcode
BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
// Resize the barcode
.ResizeTo(newWidth, newHeight)
// Save the resized barcode
.SaveAsImage("resized_barcode.png");
}
// Example usage with different size requirements
public static void ResizeForDifferentFormats()
{
var barcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128);
// Resize for product label
barcode.ResizeTo(200, 50).SaveAsImage("product_label.png");
// Resize for shipping label
barcode.ResizeTo(300, 75).SaveAsImage("shipping_label.png");
// Resize for inventory tag
barcode.ResizeTo(150, 40).SaveAsImage("inventory_tag.png");
}
}
using IronBarCode;
public class BarcodeResizer
{
public static void ResizeBarcode(string barcodeText, int newWidth, int newHeight)
{
// Generate a barcode
BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
// Resize the barcode
.ResizeTo(newWidth, newHeight)
// Save the resized barcode
.SaveAsImage("resized_barcode.png");
}
// Example usage with different size requirements
public static void ResizeForDifferentFormats()
{
var barcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128);
// Resize for product label
barcode.ResizeTo(200, 50).SaveAsImage("product_label.png");
// Resize for shipping label
barcode.ResizeTo(300, 75).SaveAsImage("shipping_label.png");
// Resize for inventory tag
barcode.ResizeTo(150, 40).SaveAsImage("inventory_tag.png");
}
}
Imports IronBarCode
Public Class BarcodeResizer
Public Shared Sub ResizeBarcode(barcodeText As String, newWidth As Integer, newHeight As Integer)
' Generate a barcode
BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128) _
.ResizeTo(newWidth, newHeight) _
.SaveAsImage("resized_barcode.png")
End Sub
' Example usage with different size requirements
Public Shared Sub ResizeForDifferentFormats()
Dim barcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128)
' Resize for product label
barcode.ResizeTo(200, 50).SaveAsImage("product_label.png")
' Resize for shipping label
barcode.ResizeTo(300, 75).SaveAsImage("shipping_label.png")
' Resize for inventory tag
barcode.ResizeTo(150, 40).SaveAsImage("inventory_tag.png")
End Sub
End Class
ResizeTo メソッドは、GeneratedBarcode オブジェクトで呼び出すことができます。 異なる出力フォーマットで作業する場合は、Create Barcode as PDF ガイドも参照してください。以下は、上記のコードスニペットを実行して生成されたバーコード画像です。
サイズ変更前
サイズ変更後
なぜ 1D BarCode に ResizeToMil メソッドを使用するのですか?
IronBarcodeで利用できるサイズ変更のもう 1 つの側面は、ResizeToMil メソッドです。 ResizeTo メソッドとは異なり、このメソッドでは次のコンポーネントを調整します。
- バーコード要素: 最も狭いバーコード要素の幅、千分の一インチ(mil)で測定。
- 高さ: バーコードの高さ、インチで測定(デフォルトは1インチ)。
- 解像度: dpi(デフォルトは96 DPI)。
この方法は、特に1D BarCodeに適しており、正確な測定が重要な産業用アプリケーションで一般的に使用されています。 mil測定システムは、異なるスキャナや印刷条件で一貫したBarCodeの読みやすさを保証する業界標準です。
using IronBarCode;
public class BarcodeResizer
{
public static void ResizeBarcodeToMil(string barcodeText, int elementWidthMil, int heightInches, int dpi = 96)
{
// Generate a barcode
BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
// Resize the barcode to mil
.ResizeToMil(elementWidthMil, heightInches, dpi)
// Save the resized barcode
.SaveAsImage("resized_barcode_mil.png");
}
// Example for different industrial standards
public static void CreateIndustrialBarcodes()
{
// Standard retail barcode (10 mil width, 1 inch height)
BarcodeWriter.CreateBarcode("RETAIL-001", BarcodeEncoding.Code128)
.ResizeToMil(10, 1, 300)
.SaveAsImage("retail_barcode.png");
// High-density warehouse barcode (5 mil width, 0.5 inch height)
BarcodeWriter.CreateBarcode("WAREHOUSE-002", BarcodeEncoding.Code128)
.ResizeToMil(5, 0.5f, 600)
.SaveAsImage("warehouse_barcode.png");
// Large shipping barcode (15 mil width, 2 inch height)
BarcodeWriter.CreateBarcode("SHIP-003", BarcodeEncoding.Code128)
.ResizeToMil(15, 2, 200)
.SaveAsImage("shipping_barcode.png");
}
}
using IronBarCode;
public class BarcodeResizer
{
public static void ResizeBarcodeToMil(string barcodeText, int elementWidthMil, int heightInches, int dpi = 96)
{
// Generate a barcode
BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
// Resize the barcode to mil
.ResizeToMil(elementWidthMil, heightInches, dpi)
// Save the resized barcode
.SaveAsImage("resized_barcode_mil.png");
}
// Example for different industrial standards
public static void CreateIndustrialBarcodes()
{
// Standard retail barcode (10 mil width, 1 inch height)
BarcodeWriter.CreateBarcode("RETAIL-001", BarcodeEncoding.Code128)
.ResizeToMil(10, 1, 300)
.SaveAsImage("retail_barcode.png");
// High-density warehouse barcode (5 mil width, 0.5 inch height)
BarcodeWriter.CreateBarcode("WAREHOUSE-002", BarcodeEncoding.Code128)
.ResizeToMil(5, 0.5f, 600)
.SaveAsImage("warehouse_barcode.png");
// Large shipping barcode (15 mil width, 2 inch height)
BarcodeWriter.CreateBarcode("SHIP-003", BarcodeEncoding.Code128)
.ResizeToMil(15, 2, 200)
.SaveAsImage("shipping_barcode.png");
}
}
Imports IronBarCode
Public Class BarcodeResizer
Public Shared Sub ResizeBarcodeToMil(barcodeText As String, elementWidthMil As Integer, heightInches As Integer, Optional dpi As Integer = 96)
' Generate a barcode
BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128) _
.ResizeToMil(elementWidthMil, heightInches, dpi) _
.SaveAsImage("resized_barcode_mil.png")
End Sub
' Example for different industrial standards
Public Shared Sub CreateIndustrialBarcodes()
' Standard retail barcode (10 mil width, 1 inch height)
BarcodeWriter.CreateBarcode("RETAIL-001", BarcodeEncoding.Code128) _
.ResizeToMil(10, 1, 300) _
.SaveAsImage("retail_barcode.png")
' High-density warehouse barcode (5 mil width, 0.5 inch height)
BarcodeWriter.CreateBarcode("WAREHOUSE-002", BarcodeEncoding.Code128) _
.ResizeToMil(5, 0.5F, 600) _
.SaveAsImage("warehouse_barcode.png")
' Large shipping barcode (15 mil width, 2 inch height)
BarcodeWriter.CreateBarcode("SHIP-003", BarcodeEncoding.Code128) _
.ResizeToMil(15, 2, 200) _
.SaveAsImage("shipping_barcode.png")
End Sub
End Class
このメソッドは、GeneratedBarcode オブジェクトでも呼び出すことができます。 バーコードの正確な寸法設定の詳細については、 "バーコードの余白設定"ガイドをご覧ください。下の画像では、ResizeToMil メソッドを適用した場合の効果を確認できます。バーコードの端の空白が削除され、メソッドに指定されたパラメータ値に従って、バーコードの最も狭い要素と高さが調整されます。
ResizeToMil の前
ResizeToMil後
バーコードと背景の色を変更するにはどうすればよいですか?
バーコードのスタイリングにおいて最も求められている機能の一つは、バーコードと背景の色を両方変更できる機能です。 IronDrawingのおかげで、IronBarcodeはこの機能を提供しています。 GeneratedBarcode オブジェクトで ChangeBarCodeColor メソッドと ChangeBackgroundColor メソッドの両方を使用することで、ユーザーはバーコードとその背景の色を変更できます。 この機能は、ブランディングを目的とする場合や、特別なイベントや製品ラインのテーマバーコードを作成する場合に特に役立ちます。
using IronBarCode;
using IronSoftware.Drawing; // Required for color manipulation
public class BarcodeColorChanger
{
public static void ChangeBarcodeColors(string barcodeText, Color barcodeColor, Color backgroundColor)
{
// Generate a barcode
var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128);
// Change the barcode color
barcode.ChangeBarCodeColor(barcodeColor);
// Change the background color
barcode.ChangeBackgroundColor(backgroundColor);
// Save the colored barcode
barcode.SaveAsImage("colored_barcode.png");
}
// Example: Create branded barcodes with company colors
public static void CreateBrandedBarcodes()
{
// Company brand colors example
var barcode = BarcodeWriter.CreateBarcode("BRAND-2024", BarcodeEncoding.Code128);
// Apply brand colors
barcode.ChangeBarCodeColor(Color.FromHex("#1E3A8A")) // Company blue
.ChangeBackgroundColor(Color.FromHex("#F3F4F6")) // Light gray background
.SaveAsImage("branded_barcode.png");
// Create seasonal variation
var seasonalBarcode = BarcodeWriter.CreateBarcode("HOLIDAY-2024", BarcodeEncoding.Code128);
seasonalBarcode.ChangeBarCodeColor(Color.DarkGreen)
.ChangeBackgroundColor(Color.LightYellow)
.SaveAsImage("seasonal_barcode.png");
}
}
using IronBarCode;
using IronSoftware.Drawing; // Required for color manipulation
public class BarcodeColorChanger
{
public static void ChangeBarcodeColors(string barcodeText, Color barcodeColor, Color backgroundColor)
{
// Generate a barcode
var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128);
// Change the barcode color
barcode.ChangeBarCodeColor(barcodeColor);
// Change the background color
barcode.ChangeBackgroundColor(backgroundColor);
// Save the colored barcode
barcode.SaveAsImage("colored_barcode.png");
}
// Example: Create branded barcodes with company colors
public static void CreateBrandedBarcodes()
{
// Company brand colors example
var barcode = BarcodeWriter.CreateBarcode("BRAND-2024", BarcodeEncoding.Code128);
// Apply brand colors
barcode.ChangeBarCodeColor(Color.FromHex("#1E3A8A")) // Company blue
.ChangeBackgroundColor(Color.FromHex("#F3F4F6")) // Light gray background
.SaveAsImage("branded_barcode.png");
// Create seasonal variation
var seasonalBarcode = BarcodeWriter.CreateBarcode("HOLIDAY-2024", BarcodeEncoding.Code128);
seasonalBarcode.ChangeBarCodeColor(Color.DarkGreen)
.ChangeBackgroundColor(Color.LightYellow)
.SaveAsImage("seasonal_barcode.png");
}
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for color manipulation
Public Class BarcodeColorChanger
Public Shared Sub ChangeBarcodeColors(barcodeText As String, barcodeColor As Color, backgroundColor As Color)
' Generate a barcode
Dim barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
' Change the barcode color
barcode.ChangeBarCodeColor(barcodeColor)
' Change the background color
barcode.ChangeBackgroundColor(backgroundColor)
' Save the colored barcode
barcode.SaveAsImage("colored_barcode.png")
End Sub
' Example: Create branded barcodes with company colors
Public Shared Sub CreateBrandedBarcodes()
' Company brand colors example
Dim barcode = BarcodeWriter.CreateBarcode("BRAND-2024", BarcodeEncoding.Code128)
' Apply brand colors
barcode.ChangeBarCodeColor(Color.FromHex("#1E3A8A")) ' Company blue
.ChangeBackgroundColor(Color.FromHex("#F3F4F6")) ' Light gray background
.SaveAsImage("branded_barcode.png")
' Create seasonal variation
Dim seasonalBarcode = BarcodeWriter.CreateBarcode("HOLIDAY-2024", BarcodeEncoding.Code128)
seasonalBarcode.ChangeBarCodeColor(Color.DarkGreen) _
.ChangeBackgroundColor(Color.LightYellow) _
.SaveAsImage("seasonal_barcode.png")
End Sub
End Class
色付きの BarCode を扱う場合は、バーコードと背景色のコントラストを十分に保ち、読みやすさを確保することが重要です。 QRコード特有のスタイリングオプションについては、Customize and Style QR Codesチュートリアルをご覧ください。
バーコードに注釈を追加するにはどうすればよいですか?
IronBarcodeはバーコード注釈の追加とスタイリングのオプションも提供しています。 注釈のスタイリングは、IronDrawingの機能を利用して、注釈の色とフォントを編集することを含みます。 注釈は、機械読み取り可能な BarCode と並んで、人間が読み取り可能な情報を提供するために不可欠であり、在庫管理、製品ラベリング、および出荷アプリケーションにとって非常に重要です。
using IronBarCode;
using IronSoftware.Drawing; // Required for font and color manipulation
public class BarcodeAnnotator
{
public static void AnnotateBarcode(string barcodeText, string annotationText, Font annotationFont, Color annotationColor, float annotationSpacing)
{
// Generate a barcode
var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128);
// Add annotation above the barcode
barcode.AddAnnotationTextAboveBarcode(annotationText, annotationFont, annotationColor, annotationSpacing);
// Add barcode value text below the barcode
barcode.AddBarcodeValueTextBelowBarcode(annotationFont, annotationColor, annotationSpacing);
// Save the annotated barcode
barcode.SaveAsImage("annotated_barcode.png");
}
// Example: Create product label with annotations
public static void CreateProductLabel()
{
var productCode = "PRD-12345-XL";
var barcode = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.Code128);
// Define fonts for different purposes
var titleFont = new Font("Arial", FontStyle.Bold, 14);
var valueFont = new Font("Arial", FontStyle.Regular, 12);
// Add product name above
barcode.AddAnnotationTextAboveBarcode("Premium Widget XL", titleFont, Color.Black, 5);
// Add product code below
barcode.AddBarcodeValueTextBelowBarcode(valueFont, Color.DarkGray, 3);
// Apply additional styling
barcode.ResizeTo(250, 80)
.SaveAsImage("product_label_annotated.png");
}
}
using IronBarCode;
using IronSoftware.Drawing; // Required for font and color manipulation
public class BarcodeAnnotator
{
public static void AnnotateBarcode(string barcodeText, string annotationText, Font annotationFont, Color annotationColor, float annotationSpacing)
{
// Generate a barcode
var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128);
// Add annotation above the barcode
barcode.AddAnnotationTextAboveBarcode(annotationText, annotationFont, annotationColor, annotationSpacing);
// Add barcode value text below the barcode
barcode.AddBarcodeValueTextBelowBarcode(annotationFont, annotationColor, annotationSpacing);
// Save the annotated barcode
barcode.SaveAsImage("annotated_barcode.png");
}
// Example: Create product label with annotations
public static void CreateProductLabel()
{
var productCode = "PRD-12345-XL";
var barcode = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.Code128);
// Define fonts for different purposes
var titleFont = new Font("Arial", FontStyle.Bold, 14);
var valueFont = new Font("Arial", FontStyle.Regular, 12);
// Add product name above
barcode.AddAnnotationTextAboveBarcode("Premium Widget XL", titleFont, Color.Black, 5);
// Add product code below
barcode.AddBarcodeValueTextBelowBarcode(valueFont, Color.DarkGray, 3);
// Apply additional styling
barcode.ResizeTo(250, 80)
.SaveAsImage("product_label_annotated.png");
}
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for font and color manipulation
Public Class BarcodeAnnotator
Public Shared Sub AnnotateBarcode(barcodeText As String, annotationText As String, annotationFont As Font, annotationColor As Color, annotationSpacing As Single)
' Generate a barcode
Dim barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
' Add annotation above the barcode
barcode.AddAnnotationTextAboveBarcode(annotationText, annotationFont, annotationColor, annotationSpacing)
' Add barcode value text below the barcode
barcode.AddBarcodeValueTextBelowBarcode(annotationFont, annotationColor, annotationSpacing)
' Save the annotated barcode
barcode.SaveAsImage("annotated_barcode.png")
End Sub
' Example: Create product label with annotations
Public Shared Sub CreateProductLabel()
Dim productCode = "PRD-12345-XL"
Dim barcode = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.Code128)
' Define fonts for different purposes
Dim titleFont = New Font("Arial", FontStyle.Bold, 14)
Dim valueFont = New Font("Arial", FontStyle.Regular, 12)
' Add product name above
barcode.AddAnnotationTextAboveBarcode("Premium Widget XL", titleFont, Color.Black, 5)
' Add product code below
barcode.AddBarcodeValueTextBelowBarcode(valueFont, Color.DarkGray, 3)
' Apply additional styling
barcode.ResizeTo(250, 80).SaveAsImage("product_label_annotated.png")
End Sub
End Class
前のコード スニペットの拡張として、バーコードの上と下の注釈のフォントとして機能する 2 つの新しい IronSoftware.Drawing.Font オブジェクトをインスタンス化します。 フォントをインスタンス化するために必要なのはフォントファミリーだけですが、サイズやスタイルなどのプロパティを追加で指定することで、より詳細に制御することができます。
AddAnnotationTextAboveBarcode: バーコードの上にカスタム注釈テキストを追加します。AddBarcodeValueTextBelowBarcode: バーコードの下にバーコード値を追加します。
これら 2 つのメソッドは同じパラメータを受け入れます: IronSoftware.Drawing.Font オブジェクト、 IronSoftware.Drawing.Color オブジェクト、およびバーコードとテキスト間の間隔の量。 さらに、AddAnnotationTextAboveBarcode メソッドでは、バーコードの上にカスタム テキストが追加されるため、注釈テキスト用の文字列が必要です。
IronBarcodeはバーコードのスタイリングに幅広いカスタマイズオプションを提供しています。 アノテーションでUnicodeサポートを必要とするアプリケーションについては、Unicode BarCodeの記述ガイドをご覧ください。QRコードのカスタマイズについては、"QRコードをカスタマイズしてロゴを追加する方法"を参照してください。 スタイル付きバーコードをさまざまな形式にエクスポートするには、Create Barcode as HTML チュートリアルを参照してください。
よくある質問
C#でバーコードの色を変更するには?
IronBarcodeはChangeBarCodeColor()メソッドを提供し、バーコードの色を簡単にカスタマイズできます。バーコードの作成後にこのメソッドをチェーンするだけで、IronSoftware.Drawing.Colorパレットから任意の色を適用することができます。
品質を損なうことなくバーコードのサイズを変更するには、どのような方法を使用すればよいですか?
IronBarcodeのResizeTo()メソッドを使用すると、品質を損なうことなくバーコードのサイズを変更できます。このメソッドはピクセルの指定された幅と高さでバーコードのロスレス再レンダリングをトリガーし、特定のレイアウトや印刷要件に合わせて寸法を調整しながら鮮明さを維持します。
バーコードの背景色をカスタマイズできますか?
はい、IronBarcodeではChangeBackgroundColor()メソッドを使用してバーコードの背景をカスタマイズすることができます。この機能により、IronSoftware.Drawing.Colorパレットを使用して任意の背景色を設定することができ、デザイン要件とのシームレスな統合が可能になります。
どのバーコード形式が独自のスタイルオプションをサポートしていますか?
IronBarcodeはPDF417、Aztec、IntelligentMail、MaxiCode、DataMatrixを含むユニークな外観を持つ様々なバーコードフォーマットをサポートしています。IronBarcodeのスタイリングメソッドによりカスタマイズが可能です。
BarCode に注釈を追加するにはどうすればよいですか?
IronBarcodeを使用すると、バーコードの上下に注釈を追加し、読みやすさを向上させ、追加のコンテキストを提供することができます。この機能は、バーコードの横に人間が読めるテキスト、製品コード、その他の識別情報を追加する場合に特に便利です。
ResizeTo メソッドと ResizeToMil メソッドの違いは何ですか?
IronBarcodeには2つのリサイズメソッドがあります:ResizeTo()はピクセルベースのリサイズをロスレスで再レンダリングするもので、ResizeToMil()はミル寸法を使用してバーコード要素をリサイズするものです。どちらのメソッドも、異なる測定要件に対応しながら品質を維持します。

