Scanning QR codes with a .NET application is straightforward using libraries like ZXing.Net or built-in .NET MAUI or WPF controls, allowing you to decode QR codes from images, camera feeds, or files with minimal setup.

What You'll Need

  • A development environment such as Visual Studio 2022 or later
  • The .NET 6.0 SDK or newer installed
  • A project type that supports UI and imaging (e.g., WPF, WinForms, .NET MAUI, or Console with image input)
  • The ZXing.Net NuGet package (for QR code decoding)
  • Optionally, a webcam or sample QR code images for testing

Step-by-Step

  1. Create a new .NET project in Visual Studio: choose WPF App (.NET) for desktop scanning or .NET MAUI for cross-platform mobile/desktop support.
  2. Open the NuGet Package Manager (Tools > NuGet Package Manager > Manage NuGet Packages for Solution) and install the ZXing.Net package. Ensure you select the latest stable version.
  3. For WPF applications, add a PictureBox or Image control to your MainWindow.xaml to display the camera feed or loaded image, and a Button to trigger scanning.
  4. In the code-behind (MainWindow.xaml.cs), initialize a BarcodeReader instance from ZXing.Net: var reader = new BarcodeReader();
  5. To scan from an image file, use OpenFileDialog to select a PNG, JPG, or BMP containing a QR code, then load it into a Bitmap and pass it to reader.Decode(bitmap). The result’s Text property holds the decoded data.
  6. For live camera scanning in WPF, use VideoCaptureDevice from the AForge.Video.DirectShow library (install via NuGet) to access the webcam. Handle the NewFrame event, convert each frame to a Bitmap, and run reader.Decode() on it. Display the frame in an Image control using BitmapImage.
  7. In .NET MAUI, use the CameraView or third-party controls like ZXing.Net.Maui (install via NuGet) to simplify camera integration. Set up the scanner view in XAML and handle the OnDetected event to process the scanned QR code.
  8. Always dispose of camera resources and unsubscribe from events when the page closes or the app exits to prevent memory leaks.
  9. Test with various QR codes: URLs, plain text, Wi-Fi credentials (format: WIFI:T:WPA;S:mynetwork;P:mypass;;), and vCard data to ensure proper parsing.

Common Mistakes

  • Forgetting to set the camera resolution to a supported value, causing the video feed to fail or freeze.
  • Not handling threading properly—running image processing on the UI thread can freeze the UI; use Task.Run or dispatch to the UI thread when updating controls.
  • Assuming all QR codes are valid; always check if reader.Decode() returns null before accessing the Text property to avoid null reference exceptions.
  • Using outdated versions of ZXing.Net that lack support for newer QR code formats or .NET versions.
  • Neglecting to request camera permissions on mobile platforms (.NET MAUI on Android/iOS), which results in no video feed.

Tips

  • Enable try-harder mode in ZXing.Net by setting reader.Options.TryHarder = true to improve detection of damaged or low-contrast QR codes.
  • Use a preview layer with a scanning rectangle overlay to guide users to align the QR code properly.
  • For batch processing, scan multiple images in a loop and log results to a file or database.
  • Combine QR scanning with validation—for example, check if the decoded text is a valid URL before navigating.
  • If using AForge, ensure you’re on a Windows desktop with DirectShow support; it does not work on .NET MAUI or Linux without additional setup.

Troubleshooting

  • No camera feed: Verify the webcam is connected and not used by another app. Try switching to a different video capture device index (e.g., 0, 1, 2) in VideoCaptureDevice constructor.
  • QR code not detected: Ensure the code is well-lit, in focus, and fully within the frame. Increase the scanning area or enable image preprocessing (grayscale, contrast enhancement) before decoding.
  • Application crashes on scan: Check for null results from Decode() and handle exceptions. Ensure all UI updates happen on the main thread using Dispatcher.Invoke (WPF) or MainThread.BeginInvokeOnMainThread (.NET MAUI).
  • NuGet package fails to install: Confirm your .NET version is compatible with the ZXing.Net package version. Update the SDK or try a earlier package version if needed.
  • Blank or black video frames: This often indicates a missing codec or DirectShow issue. Test the webcam in another app (like Camera app in Windows) to confirm it works.

Frequently Asked Questions

Can I scan QR codes without a camera?

Yes, you can load and decode QR codes from image files (PNG, JPG, BMP) using ZXing.Net’s BarcodeReader.Decode() method on a Bitmap object.

Do I need to pay for ZXing.Net?

No, ZXing.Net is free and open-source, licensed under the Apache License 2.0, and available via NuGet.

Will this work on Mac or Linux?

Yes, with .NET MAUI or Console apps using ZXing.Net, though camera access on Linux may require additional setup; image-based scanning works cross-platform.

How do I scan a QR code that’s upside down or rotated?

ZXing.Net automatically detects QR codes at any rotation angle (0°, 90°, 180°, 270°), so no manual rotation is needed.

Can I generate QR codes in the same app?

Yes, use the BarcodeWriter class from ZXing.Net to encode text into a QR code bitmap, which you can save or display.

Is it safe to scan unknown QR codes?

Exercise caution—malicious QR codes can trigger unwanted actions. Always validate and preview the decoded content (e.g., show a URL before opening it).