Jak odczytać kod QR z obrazu w C#
QR codes (Quick Response Codes) are everywhere—on product packaging, event tickets, menus, and even business cards. As a .NET developer, being able to quickly and reliably read QR codes from images can open the door to powerful automation and user interaction features. In this guide, we'll walk you through how to use IronQR, a high-performance QR code library built specifically for .NET, to read QR codes from images with just a few lines of C# code.
Whether you're building inventory management software, integrating two-factor authentication, or simply decoding URLs from screenshots, IronQR makes it easy.
What is IronQR?
IronQR is a powerful C# QR code library developed to create a powerful QR code reader and writer for .NET developers. IronQR is designed for both QR code generation and scanning, and it supports reading from a variety of image formats, making it ideal for use in desktop, web, or server applications. Through this library, you can create accurate QR code reader tools to automate the entire process of QR code recognition and reading.
Najważniejsze cechy
- Read and generate QR codes with ease.
- Support for JPEG, PNG, BMP, and other image formats.
- High-speed performance and accurate detection for easy extraction of QR code data.
- Works across .NET Framework, .NET Core, .NET Standard (2.0+), and .NET 6/7+ projects.
- Offers cross-platform support, so you can work in your preferred app environment and operating system, whether that's Windows, Linux, or any other supported environment.
Unlike open-source alternatives, IronQR focuses on enterprise-grade stability, commercial licensing, and professional support—making it an excellent fit for business-critical applications.
Setting Up IronQR in Your C# Project
Before you can start scanning QR codes, let's walk through how to set up IronQR in your .NET application.
Zainstaluj przez NuGet
You can install IronQR directly from the NuGet Package Manager Console:
Install-Package IronQR
Install-Package IronQR
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronQR
Alternatively, use the NuGet GUI in Visual Studio by searching for IronQR, and click "Install":

Add Namespaces and Basic Setup
Once installed, include the following namespace in your C# file:
using IronSoftware.Drawing;
using IronQR;
using IronSoftware.Drawing;
using IronQR;
Imports IronSoftware.Drawing
Imports IronQR
Note: IronSoftware.Drawing is used for handling image formats in a cross-platform manner.
Read QR Code from an Image
Let's dive into how to actually read a QR code from a file using IronQR.
Supported Image Formats
IronQR supports multiple types of image formats, including:
- PNG
- JPG/JPEG
- BMP
- GIF
- TIFF
This flexibility allows you to work with virtually any image source, from camera snapshots to scanned documents.
Podstawowy przykład kodu
Let's take a closer look at how you can use this library to decode QR codes with ease. Here's a minimal example that reads a single QR code with the text value "Hello World!" from an image file, using the Bitmap class and a file stream to load the image:
using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image using a file stream
using var stream = File.OpenRead("sample-qr.png");
var bitmapImage = AnyBitmap.FromStream(stream);
QrImageInput qrImageInput = new QrImageInput(bitmapImage );
// Read the QR code
QrReader qrReader = new QrReader();
try
{
// Use the QR read method to read the values within your QR code(s)
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}
}
}
using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image using a file stream
using var stream = File.OpenRead("sample-qr.png");
var bitmapImage = AnyBitmap.FromStream(stream);
QrImageInput qrImageInput = new QrImageInput(bitmapImage );
// Read the QR code
QrReader qrReader = new QrReader();
try
{
// Use the QR read method to read the values within your QR code(s)
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System
Friend Class Program
Shared Sub Main()
' Load the image using a file stream
Dim stream = File.OpenRead("sample-qr.png")
Dim bitmapImage = AnyBitmap.FromStream(stream)
Dim qrImageInput As New QrImageInput(bitmapImage)
' Read the QR code
Dim qrReader As New QrReader()
Try
' Use the QR read method to read the values within your QR code(s)
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
Catch ex As Exception
Console.WriteLine("Error reading QR code: " & ex.Message)
End Try
End Sub
End Class
Wynik konsoli

This code loads the QR code image, reads the first detected QR code, and prints the decoded content. Simple and effective. From here, you can save the QR codes value's for further use.
Handling Multiple QR Codes
If your image contains multiple QR codes (e.g., a sheet of product labels), you can extract all of them. For this example, we'll run the following QR Code image through our program:

using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image from file
var inputImage = AnyBitmap.FromFile("SampleCodes.png");
QrImageInput qrImageInput = new QrImageInput(inputImage);
// Read the QR code
QrReader qrReader = new QrReader();
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
}
using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image from file
var inputImage = AnyBitmap.FromFile("SampleCodes.png");
QrImageInput qrImageInput = new QrImageInput(inputImage);
// Read the QR code
QrReader qrReader = new QrReader();
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System
Friend Class Program
Shared Sub Main()
' Load the image from file
Dim inputImage = AnyBitmap.FromFile("SampleCodes.png")
Dim qrImageInput As New QrImageInput(inputImage)
' Read the QR code
Dim qrReader As New QrReader()
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
End Sub
End Class
Wynik

Common Use Cases for QR Code Scanning
Here are a few real-world scenarios where reading QR codes from images becomes valuable:
- Inventory and Asset Management: Automate item identification by scanning QR codes from package images.
- Two-Factor Authentication (2FA): Parse QR codes from 2FA setup screens to assist in secure configuration.
- Mobile App Integration: Launch mobile URLs or app-specific deep links by scanning shared QR screenshots.
- Event Ticketing: Validate ticket QR codes sent via email or displayed on a screen.
- Payment Gateways: Extract payment data embedded in QR codes for fintech applications.
In all these use cases, fast and accurate recognition is key—something IronQR handles with ease.
Troubleshooting Tips
If you run into issues reading QR codes, consider the following:
Poor Image Quality
Blurry or low-resolution images can make it difficult to detect a QR code. Use high-quality inputs when possible.
QR Code Not Detected
Upewnij się, że obraz nie jest zbyt ciemny, ma silny kontrast, a kod QR nie jest zasłonięty. Spróbuj przyciąć obraz, aby skupić się na obszarze kodu QR.
Obsługa wyjątków
Zawsze umieszczaj logikę odczytu kodów QR w blokach try-catch, aby płynnie obsługiwać uszkodzone pliki lub nieoczekiwane formaty:
try
{
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}
try
{
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}
Try
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
Catch ex As Exception
Console.WriteLine("Error reading QR code: " & ex.Message)
End Try
Podsumowanie
Odczytywanie kodów QR z obrazów w języku C# nie musi być trudne. Dzięki IronQR możesz zintegrować skanowanie kodów QR ze swoimi aplikacjami .NET za pomocą zaledwie kilku linii kodu. Jego prostota, kompatybilność międzyplatformowa i doskonała wydajność sprawiają, że jest to narzędzie z wyboru dla programistów pracujących nad nowoczesnymi systemami obsługującymi kody QR.
Jeśli chcesz rozszerzyć tę funkcjonalność, rozważ zapoznanie się z:
- Generowanie kodów QR do tworzenia własnych kodów do skanowania.
- Integracja z IronBarcode lub IronOCR zapewnia jeszcze szersze możliwości skanowania. Gotowi, aby rozpocząć?
Zainstaluj bezpłatną wersję próbną IronQR, aby rozpocząć pracę już dziś i dowiedzieć się, w jaki sposób ta biblioteka może usprawnić Twoje zadania związane z kodami QR.
Często Zadawane Pytania
Jak odczytać kody QR z obrazów w języku C#?
Możesz użyć IronQR do odczytu kodów QR z obrazów w języku C#. IronQR udostępnia metody skanowania i wyodrębniania danych z kodów QR z różnych formatów obrazów, takich jak PNG, JPG i TIFF, przy minimalnym nakładzie kodu.
Jakie kroki należy wykonać, aby skonfigurować IronQR w projekcie .NET?
Aby skonfigurować IronQR w projekcie .NET, należy użyć konsoli NuGet Package Manager Console z poleceniem Install-Package IronQR lub znaleźć IronQR w graficznym interfejsie NuGet Package Manager w programie Visual Studio i kliknąć „Install”.
W jaki sposób IronQR może pomóc w odczytywaniu kodów QR z obrazów o niskiej jakości?
IronQR jest zaprojektowany do odczytu kodów QR z obrazów o różnej jakości. Jednak aby uzyskać najlepsze wyniki, upewnij się, że obraz ma dobry kontrast i skup się na obszarze kodu QR. Jeśli wykrywanie nie powiedzie się, spróbuj poprawić jakość obrazu lub go przyciąć.
Czy IronQR może odczytać wiele kodów QR z jednego obrazu?
Tak, IronQR może wykrywać i odczytywać wiele kodów QR z jednego obrazu. Jest to przydatne podczas przetwarzania dokumentów lub etykiet zawierających wiele kodów QR.
Jakie są typowe zastosowania odczytu kodów QR w aplikacjach .NET?
Typowe zastosowania obejmują zarządzanie zapasami, uwierzytelnianie dwuskładnikowe, integrację aplikacji mobilnych, sprzedaż biletów na wydarzenia oraz bramki płatnicze. IronQR ułatwia korzystanie z tych aplikacji, zapewniając szybkie i niezawodne odczytywanie kodów QR.
W jaki sposób IronQR zapewnia kompatybilność między platformami?
IronQR obsługuje .NET Framework, .NET Core, .NET Standard oraz .NET 6/7+, zapewniając kompatybilność międzyplatformową dla systemów Windows, Linux i innych środowisk.
Co należy zrobić, jeśli IronQR nie odczyta kodu QR?
Jeśli IronQR nie odczyta kodu QR, sprawdź jakość obrazu pod kątem ostrości i kontrastu. Upewnij się, że na kodzie QR nie ma żadnych przeszkód i rozważ przycięcie obrazu, aby skupić się na obszarze kodu QR. Jeśli problemy nadal występują, sprawdź, czy format pliku jest obsługiwany.
Jak radzić sobie z wyjątkami podczas korzystania z IronQR do odczytu kodów QR?
Aby obsłużyć wyjątki, należy umieścić kod IronQR w blokach try-catch. Takie podejście pomaga w płynnym zarządzaniu problemami, takimi jak uszkodzone pliki lub nieobsługiwane formaty.
Jakie są zalety korzystania z IronQR do przetwarzania kodów QR?
IronQR oferuje wysoką wydajność, obsługuje wiele formatów obrazów i zapewnia stabilność na poziomie Enterprise. Łatwo go zintegrować z różnymi aplikacjami .NET, co czyni go idealnym rozwiązaniem dla programistów poszukujących wydajnych rozwiązań do przetwarzania kodów QR.
Jak mogę poprawić dokładność wykrywania kodów QR za pomocą IronQR?
Popraw dokładność wykrywania, używając wysokiej jakości obrazów o dobrym kontraście i ostrości. Upewnij się, że kody QR nie są zasłonięte, i w razie potrzeby rozważ zastosowanie technik przetwarzania wstępnego obrazów.




