IronBarcodeで.NET用C#のBarCodeをカスタマイズしてスタイルする
IronBarcodeは、ChangeBarCodeColor()やResizeTo()のようなシンプルなメソッド呼び出しで、色の変更、寸法の変更、注釈の追加など、C#でバーコードをカスタマイズすることができます。
ここ数年、BarCodeの使用はますます一般的になり、データ、ID、WebページのURLの保存など、幅広いアプリケーションで使用されています。 アプリケーションによっては、バーコードが製品に表示されるため、スタイリングオプションの需要が高まっています。 そのため、バーコードの種類によっては、PDF417、Aztec、IntelligentMail、MaxiCode、DataMatrixなど、ユニークな外観を持つものもあります。 サポートされているフォーマットの包括的なリストについては、Supported BarCode Formats ドキュメントを参照してください。
加えて、IronBarcodeはバーコードの色、バーコードのサイズ変更、背景色のような側面を含む、バーコードのスタイルを変更するオプションをユーザーに提供します。 これは、私たちのオープンソースライブラリであるIronDrawingの助けにより実現されています。 これらのスタイリング機能は、IronBarcodeの包括的なバーコード生成機能に基づいています。
クイックスタート: バーコードの色と背景のカスタマイズ
ここでは、開発者がIronBarcodeを使用してバーコードのバーと背景にカスタムカラーをすばやく適用する方法を示す簡単な例を示します。 たった1回の連鎖呼び出しで、スタイル付きBarCodeを生成することがいかに簡単であるか、おわかりいただけるでしょう。 より高度な例については、C# Barcode Image Generator チュートリアルをご覧ください。
今すぐ NuGet で PDF を作成してみましょう:
NuGet パッケージ マネージャーを使用して IronBarcode をインストールします
このコード スニペットをコピーして実行します。
IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128) .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue) .ChangeBackgroundColor(IronSoftware.Drawing.Color.White) .SaveAsImage("styled.png");実際の環境でテストするためにデプロイする
最小限のワークフロー(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");
}
}ResizeTo メソッドはGeneratedBarcodeオブジェクトで呼び出すことができます。 異なる出力フォーマットで作業する場合は、Create Barcode as PDF ガイドも参照してください。以下は、上記のコードスニペットを実行して生成されたバーコード画像です。

サイズ変更前

サイズ変更後
なぜ 1D BarCode に ResizeToMil メソッドを使用するのですか?
IronBarcodeで利用可能なサイズ変更のもう一つの側面は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");
}
}また、このメソッドをGeneratedBarcodeオブジェクトで呼び出すことができます。 正確なバーコード寸法の設定については、Set Barcodes Margins ガイドを参照してください。下の画像では、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");
}
}色付きの 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");
}
}
。前のコードスニペットを拡張すると、バーコードの上部と下部に注釈のフォントとして2つのIron Software.Drawing.Fontオブジェクトをインスタンス化します。 フォントをインスタンス化するために必要なのはフォントファミリーだけですが、サイズやスタイルなどのプロパティを追加で指定することで、より詳細に制御することができます。
AddAnnotationTextAboveBarcode: バーコードの上にカスタム注釈テキストを追加します。AddBarcodeValueTextBelowBarcode: バーコードの値をバーコードの下に追加します。
これら2つのメソッドは、Iron Software.Drawing.Fontオブジェクト、Iron Software.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()はミル寸法を使用してバーコード要素をリサイズするものです。どちらのメソッドも、異なる測定要件に対応しながら品質を維持します。






