Wie man QR-Codes und Barcodes in C# für .NET 5 mit Iron Barcode generiert

Barcodes & QRs in C# & VB.NET Applications

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

Das Lesen und Schreiben von Barcodes in C# und allen anderen .NET-Sprachen ist mit unserer IronBarcode-Softwarebibliothek ein einfacher Prozess.

IronBarcode installieren

Der erste Schritt auf der Reise wird die Installation von IronBarcode sein, die durch das Herunterladen von NuGet oder durch Herunterladen der DLL erfolgen kann.

Um das IronBarcode NuGet-Paket zu installieren, können Sie den NuGet Package Manager für Visual Studio verwenden:

Install-Package BarCode

Alternativ können Sie auch mit der dotnet CLI installieren:

dotnet add package IronBarCode

Lesen eines Barcodes oder QR-Codes

Das Lesen eines Barcodes erfordert nur eine einzige Codezeile mit IronBarcode.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
    foreach (BarcodeResult result in results)
    {
        Console.WriteLine(result.Text);
    }
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
	For Each result As BarcodeResult In results
		Console.WriteLine(result.Text)
	Next result
End If
$vbLabelText   $csharpLabel

Mit dieser einen Codezeile haben Sie die Möglichkeit, alle Arten von Barcodes aus dem Eingabedokument mit außergewöhnlicher Leistung zu erkennen und zu scannen – alles, was Sie in einem Schritt benötigen! Diese Methode unterstützt eine Vielzahl von Bildformaten wie JPEG, PNG und BMP sowie PDFs und Mehrbildformate wie GIF und TIFF. Für verbesserte Leistung stehen anpassbare Konfigurationsoptionen zur Verfügung.

Um die Lesegeschwindigkeit zu verbessern, können Sie ein BarcodeReaderOptions-Objekt mit der auf bessere Leistung konfigurierten Speed-Einstellung erstellen. Die Standardeinstellung ist Balanced, aber die Option Faster ist verfügbar, um bestimmte Prüfungen zu überspringen.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};

BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
    Console.WriteLine(result.First().Text);
}
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
	.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}

Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Text)
End If
$vbLabelText   $csharpLabel

Sie können den ScanMode auf OnlyBasicScan setzen, um den Leseprozess zu optimieren.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("MultipleBarcodes.png");

// Loop through the results
foreach (BarcodeResult result in results)
{
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.png")

' Loop through the results
For Each result As BarcodeResult In results
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result
$vbLabelText   $csharpLabel

Andere Konfigurationen umfassen die Angabe der Barcode-Formate, nach denen gescannt werden soll, was die Verarbeitung beschleunigen kann, indem unnötige Scans reduziert werden.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;

BarcodeResults pagedResults = BarcodeReader.Read("MultipleBarcodes.pdf");

// Loop through the results
foreach (BarcodeResult result in pagedResults)
{
    int pageNumber = result.PageNumber;
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}

// or from a multi-page  TIFF scan with image correction:
BarcodeResults multiFrameResults = BarcodeReader.Read(inputImage: "Multiframe.tiff", new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Detailed,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.Code128,
    Multithreaded = false,
    RemoveFalsePositive = false,
    ImageFilters = null
});
Imports IronBarCode

Private pagedResults As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.pdf")

' Loop through the results
For Each result As BarcodeResult In pagedResults
	Dim pageNumber As Integer = result.PageNumber
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result

' or from a multi-page  TIFF scan with image correction:
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read(inputImage:= "Multiframe.tiff", New BarcodeReaderOptions With {
	.Speed = ReadingSpeed.Detailed,
	.ExpectMultipleBarcodes = True,
	.ExpectBarcodeTypes = BarcodeEncoding.Code128,
	.Multithreaded = False,
	.RemoveFalsePositive = False,
	.ImageFilters = Nothing
})
$vbLabelText   $csharpLabel

Barcodes schreiben

Um Barcodes mit IronBarcode zu schreiben, verwenden wir die BarcodeWriter-Klasse.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
$vbLabelText   $csharpLabel

Barcodes stylen

IronBarcode bietet mehrere Optionen zur Manipulation der visuellen Darstellung eines Barcodes.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);

// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)

' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
$vbLabelText   $csharpLabel

Barcodes als HTML exportieren

IronBarcode kann Barcodes als HTML-Dokumente oder als Teil von HTML-Inhalten exportieren.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarCode;

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
Imports IronBarCode

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
$vbLabelText   $csharpLabel

QR-Codes generieren

Für QR-Codes verwenden Sie die QRCodeWriter-Klasse, die zusätzliche Konfigurationen für QR-spezifische Funktionen wie Fehlerkorrektur bietet.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;

QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
$vbLabelText   $csharpLabel

Unterstützte Barcode-Formate

IronBarcode unterstützt eine Vielzahl häufig verwendeter Barcode-Formate sowohl zum Lesen als auch zum Schreiben:

  • QR-, Micro QR- und Rechteckige Micro QR (rMQR)-Codes.
  • Andere zweidimensionale Barcodes wie Aztec, Data Matrix, MaxiCode und PDF417.
  • Gestapelte lineare Barcodes wie Databar.
  • Herkömmliche eindimensionale Barcodeformate wie UPC-A, UPC-E, EAN-8, EAN-13, Codabar, ITF, MSI und Plessey.

Warum IronBarcode wählen?

IronBarcode bietet eine benutzerfreundliche, einfach zu verwendende API für Entwickler, um Barcodes für .NET zu lesen und zu schreiben, die für Genauigkeit, Präzision und Geschwindigkeit in realen Anwendungsfällen optimiert ist.

Die BarcodeWriter-Klasse validiert und korrigiert beispielsweise automatisch 'Checksummen' auf UPCA- und UPCE-Barcodes und behandelt numerische Formatbeschränkungen. IronBarcode hilft Entwicklern, das am besten geeignete Barcode-Format für ihre Daten auszuwählen.

Die Bibliothek ist robust, mit Bildvorverarbeitungstechniken wie automatischer Drehung und Bildentrauschung, um Erfolgsraten bei der Barcodelokalisierung zu maximieren.

Vorgehen

Um das Beste aus IronBarcode herauszuholen, empfehlen wir Ihnen, die Tutorials in diesem Dokumentationsabschnitt zu lesen und uns auf GitHub zu besuchen.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen
Bereit anzufangen?
Nuget Downloads 1,935,276 | Version: 2025.11 gerade veröffentlicht