C# での OcrProgress トラッキングの使用方法

IronBarcode を使用して C# for .NET でバーコードをカスタマイズおよびスタイル設定する

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

長年にわたり、バーコードの使用はますます一般的になり、データ、ID、またはウェブページのURLを保存するなど、さまざまなアプリケーションで使用されています。 一部のアプリケーションでは、バーコードが製品に表示されるため、バーコードをスタイルするためのオプションの需要が増加しています。 そのため、一部のバーコードタイプ/エンコーディングは、PDF417AztecIntelligentMailMaxiCodeDataMatrix などのような、独自の外観を持つものが登場しました。

加えて、IronBarcodeはユーザーにバーコードのさらなるスタイリングオプションを提供しており、バーコードの色バーコードのサイズ変更背景色 などの側面を含みます。 これは、私たちのオープンソースライブラリであるIronDrawingの助けにより実現されています。

クイックスタート: バーコードの色と背景のカスタマイズ

こちらは、開発者がIronBarcodeを使用してバーコードのバーや背景にカスタムカラーを素早く適用する方法を示す簡単な例です。 連鎖呼び出しを1回行うだけでスタイルバーバーコードを生成するのがどれほど簡単かが分かります。

Nuget Icon今すぐ NuGet で PDF を作成してみましょう:

  1. NuGet パッケージ マネージャーを使用して IronBarcode をインストールします

    PM > Install-Package BarCode

  2. このコード スニペットをコピーして実行します。

    IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128)
        .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue)
        .ChangeBackgroundColor(IronSoftware.Drawing.Color.White)
        .SaveAsImage("styled.png");
  3. 実際の環境でテストするためにデプロイする

    今すぐ無料トライアルでプロジェクトに IronBarcode を使い始めましょう
    arrow pointer

バーコードサイズ変更の例

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");
    }
}
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");
    }
}
Imports IronBarCode

Public Class BarcodeResizer
	Public Shared Sub ResizeBarcode(ByVal barcodeText As String, ByVal newWidth As Integer, ByVal newHeight As Integer)
		' Generate a barcode
		BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128).ResizeTo(newWidth, newHeight).SaveAsImage("resized_barcode.png")
	End Sub
End Class
$vbLabelText   $csharpLabel

ResizeTo メソッドはGeneratedBarcodeオブジェクトで呼び出すことができます。 以下は、上記のコードスニペットを実行することで生成されたバーコード画像です。

Barcode before resize
Barcode after resize

ResizeToMilメソッドを使用

IronBarcodeで利用可能なサイズ変更のもう一つの側面はResizeToMilメソッドです。 ResizeToメソッドとは異なり、次のコンポーネントを調整します:

  • バーコード要素: 最も狭いバーコード要素の幅、千分の一インチ(mil)で測定。
  • 高さ: バーコードの高さ、インチで測定(デフォルトは1インチ)。
  • 解像度: dpi(デフォルトは96 DPI)。

このメソッドは1Dバーコードに特に適しています。

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");
    }
}
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");
    }
}
Imports IronBarCode

Public Class BarcodeResizer
	Public Shared Sub ResizeBarcodeToMil(ByVal barcodeText As String, ByVal elementWidthMil As Integer, ByVal heightInches As Integer, Optional ByVal dpi As Integer = 96)
		' Generate a barcode
		BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128).ResizeToMil(elementWidthMil, heightInches, dpi).SaveAsImage("resized_barcode_mil.png")
	End Sub
End Class
$vbLabelText   $csharpLabel

また、このメソッドをGeneratedBarcodeオブジェクトで呼び出すことができます。 以下の画像では、ResizeToMilメソッドを適用した結果が見られます。バーコードの端の余白がなくなり、最も狭い要素とバーコードの高さが、メソッドに与えられたパラメータ値に応じて調整されます。

Barcode before ResizeToMil
Barcode after 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");
    }
}
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");
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for color manipulation

Public Class BarcodeColorChanger
	Public Shared Sub ChangeBarcodeColors(ByVal barcodeText As String, ByVal barcodeColor As Color, ByVal 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
End Class
$vbLabelText   $csharpLabel
カラーバーコード

バーコード注釈追加の例

IronBarcodeはバーコード注釈の追加とスタイリングのオプションも提供しています。 注釈のスタイリングは、IronDrawingの機能を利用して、注釈の色とフォントを編集することを含みます。

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");
    }
}
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");
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for font and color manipulation

Public Class BarcodeAnnotator
	Public Shared Sub AnnotateBarcode(ByVal barcodeText As String, ByVal annotationText As String, ByVal annotationFont As Font, ByVal annotationColor As Color, ByVal 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
End Class
$vbLabelText   $csharpLabel
注釈付きのカラーバーコード

前のコードスニペットを拡張すると、バーコードの上部と下部に注釈のフォントとして2つのIron Software.Drawing.Fontオブジェクトをインスタンス化します。 フォントファミリーのみがフォントをインスタンス化するのに必要です。

  • AddAnnotationTextAboveBarcode: バーコードの上にカスタム注釈テキストを追加します。
  • AddBarcodeValueTextBelowBarcode: バーコードの値をバーコードの下に追加します。

これら2つのメソッドは、Iron Software.Drawing.Fontオブジェクト、Iron Software.Drawing.Colorオブジェクト、およびバーコードとテキスト間の間隔の量を同じパラメータとして受け取ります。 さらに、AddAnnotationTextAboveBarcodeメソッドは、注釈テキスト用の文字列を必要とし、バーコードの上にカスタムテキストを追加します。

IronBarcodeは、ユーザーに幅広いバーコードのカスタマイズとスタイリングの機会を提供しており、想像力以外の制限はありません。 QRコードのカスタマイズについてもっと知りたい場合は、"QRコードにロゴを追加する方法"を参照してください。

よくある質問

.NET C# でバーコードの色を変更するにはどうすればよいですか?

IronBarcode の ChangeBarCodeColor メソッドを使用して .NET C# でバーコードの色を変更できます。これにより、デザインのニーズに合わせてバーコードの外観をカスタマイズできます。

.NET C# でバーコードをリサイズするための利用可能なメソッドは何ですか?

IronBarcode には、ピクセル単位でバーコードをリサイズする ResizeTo および 1D バーコードに適した千分の一インチ単位で幅を調整する ResizeToMil などのメソッドがあります。

C# でバーコードにテキスト注釈を追加できますか?

はい、IronBarcode の AddAnnotationTextAboveBarcode および AddBarcodeValueTextBelowBarcode メソッドを使用して、C# でバーコードにテキスト注釈を追加できます。これにより、ラベル付けと情報を強化できます。

リサイズしたバーコードが読み取れなくなった場合はどうすればよいですか?

小さなサイズでリサイズされたバーコードが読み取り不可能になる場合、IronBarcode はそれらの値を無視して、バーコードが機能を維持することを保証します。

.NET C# で高品質のバーコードレンダリングを確保するにはどうすればよいですか?

高品質のバーコードレンダリングは、サイズ調整時に画像品質を保ちながら、IronBarcode のロスレスリサイズメソッド(ResizeTo または ResizeToMil)を使用することで達成できます。

バーコードの色と注釈スタイルをサポートしているライブラリはどれですか?

IronDrawing はオープンソースのライブラリで、色の操作や注釈のスタイリングを支援し、IronBarcode で創造的で個性的なバーコードデザインを可能にします。

C# でバーコード注釈のフォントを変更することは可能ですか?

はい、Iron Software.Drawing.Font オブジェクトを使用してバーコード注釈のフォントをカスタマイズし、バーコードの上や下のテキストスタイリングに柔軟性を提供できます。

C# でのバーコードカスタマイズのためのライブラリはどこからダウンロードできますか?

バーコードカスタマイズ用の C# ライブラリは、https://www.nuget.org/packages/BarCode/ からダウンロードできます。これにより、IronBarcode を使用してバーコードのカスタマイズが始められます。

.NET C# でバーコードの背景色をカスタマイズするにはどうすればよいですか?

IronBarcode の ChangeBackgroundColor メソッドを使用して .NET C# でバーコードの背景色をカスタマイズできます。これにより、ユニークなデザインとブランディングの機会が提供されます。

リサイズ後にバーコードの読み取れる状態を維持する最良の方法は何ですか?

リサイズ後の読み取り性を維持するには、IronBarcode のリサイズメソッドを使用します。これにより、バーコードの寸法が機能や明瞭性を損なわないようにします。

Hairil Hasyimi Bin Omar
ソフトウェアエンジニア
すべての優れたエンジニアのように、ハイリルは熱心な学習者です。彼はC#、Python、Javaの知識を磨き、その知識を活用してIron Softwareのチームメンバーに価値を追加しています。ハイリルはマレーシアのマラ工科大学からIron Softwareのチームに参加し、化学およびプロセス工学の学士号を取得しました。
準備はできましたか?
Nuget ダウンロード 1,979,979 | Version: 2025.11 リリース