WIA C# and VB.NET samples - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

WIA C# and VB.NET samples

WIA C# sample and WIA VB.NET sample will show you how to acquire images via WIA scanner to recognize barcodes. 

In the latest version of BarCode Reader SDK we have implemented new WIAImageScanner class that allows to acquire images and works on Windows XP, Vista, 7, Windows 8 for both x86 and x64 platforms. Find source code samples of using this class below.

C#

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Bytescout.BarCodeReader;

// This example demonstrates camera image acquiring using Windows Image Acquisition (WIA).
// using built-in <span data-scayt_word="WIAImageScanner" data-scaytid="10">WIAImageScanner</span> class from <span data-scayt_word="barCode" data-scaytid="12">barCode</span> Reader SDK

<span data-scayt_word="namespace" data-scaytid="13">namespace</span> <span data-scayt_word="BarcodeFromWebCam" data-scaytid="14">BarcodeFromWebCam</span>
{
 static class Program
 {
  static void Main()
  {
            // create <span data-scayt_word="WIA" data-scaytid="15">WIA</span> scanner object
            <span data-scayt_word="WIAImageScanner" data-scaytid="16">WIAImageScanner</span> <span data-scayt_word="wiaScanner" data-scaytid="21">wiaScanner</span> = new <span data-scayt_word="WIAImageScanner" data-scaytid="17">WIAImageScanner</span>();

            wiaScanner.OutputFileNameTemplate = "<span data-scayt_word="BarCodeReader-scanned" data-scaytid="22">BarCodeReader-scanned</span>";
            wiaScanner.OutputImageFormat = WiaImageFormatType.PNG;
            wiaScanner.ImageQuality = WiaImageBias.MaximizeQuality;
            wiaScanner.ImageIntent = WiaImageIntent.UnspecifiedIntent;
            wiaScanner.ShowDeviceSelectionDialog = true;

            try
            {
                // run acquire and exit if canceled or zero images
                if (!wiaScanner.Acquire())
                    return;

            }
            catch (Exception E)
            {
                string message = E.Message;
                if (E.InnerException != null)
                    message = message + "rnrn" + E.InnerException.Message;
                MessageBox.Show("Error while acquiring images:rnrn" + message);
                return;
            }

            // Read barcode:
            Reader <span data-scayt_word="barcodeReader" data-scaytid="25">barcodeReader</span> = new Reader();
            <span data-scayt_word="FoundBarcode" data-scaytid="26">FoundBarcode</span>[] barcodes = barcodeReader.ReadFrom(wiaScanner.OutputFiles[0]);

            if (barcodes.Length > 0)
            {
                <span data-scayt_word="StringBuilder" data-scaytid="28">StringBuilder</span> builder = new <span data-scayt_word="StringBuilder" data-scaytid="29">StringBuilder</span>();

                <span data-scayt_word="foreach" data-scaytid="30">foreach</span> (<span data-scayt_word="FoundBarcode" data-scaytid="27">FoundBarcode</span> barcode in barcodes)
                    builder.AppendLine(String.Format("Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value));

                MessageBox.Show(builder.ToString());
            }
            else
            {
                MessageBox.Show(wiaScanner.OutputFiles[0] + "rnr<span data-scayt_word="nCould" data-scaytid="32">nCould</span> not find any barcode.");
            }


   }
 }
}

Visual Basic .NET

Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports Bytescout.BarCodeReader

' This example demonstrates camera image acquiring using Windows Image Acquisition (WIA).
' using built-in <span data-scayt_word="WIAImageScanner" data-scaytid="35">WIAImageScanner</span> class from <span data-scayt_word="barCode" data-scaytid="36">barCode</span> Reader SDK

<span data-scayt_word="NotInheritable" data-scaytid="40">NotInheritable</span> Class Program
 Private Sub New()
 End Sub
    Friend Shared Sub Main()

        ' use <span data-scayt_word="WIA" data-scaytid="37">WIA</span> barcode reader scanner
        Dim <span data-scayt_word="wiaScanner" data-scaytid="43">wiaScanner</span> As <span data-scayt_word="WIAImageScanner" data-scaytid="44">WIAImageScanner</span> = New <span data-scayt_word="WIAImageScanner" data-scaytid="45">WIAImageScanner</span>

        wiaScanner.OutputFileNameTemplate = "<span data-scayt_word="BarCodeReader-scanned" data-scaytid="50">BarCodeReader-scanned</span>"
        wiaScanner.OutputImageFormat = WiaImageFormatType.PNG
        wiaScanner.ImageQuality = WiaImageBias.MaximizeQuality
        wiaScanner.ImageIntent = WiaImageIntent.UnspecifiedIntent
        wiaScanner.ShowDeviceSelectionDialog = True

        Try
            ' run acquire and exit if canceled or zero images
            If Not wiaScanner.Acquire() Then
                Return
            End If

        Catch E As Exception
            Dim message As String = E.Message
            If E.InnerException <span data-scayt_word="IsNot" data-scaytid="53">IsNot</span> Nothing Then
                message = message + "rnrn" + E.InnerException.Message
                MessageBox.Show("Error while acquiring images:rnrn" + message)
                Return
            End If
        End Try

        ' Read barcode:
        Dim <span data-scayt_word="barcodeReader" data-scaytid="52">barcodeReader</span> As New Reader()
        Dim barcodes As <span data-scayt_word="FoundBarcode" data-scaytid="56">FoundBarcode</span>() = barcodeReader.ReadFrom(wiaScanner.OutputFiles(0))

        If barcodes.Length > 0 Then
            Dim builder As New <span data-scayt_word="StringBuilder" data-scaytid="57">StringBuilder</span>()

            For Each barcode As <span data-scayt_word="FoundBarcode" data-scaytid="59">FoundBarcode</span> In barcodes
                builder.AppendLine([String].Format("Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value))
            Next

            MessageBox.Show(builder.ToString())
        Else
            MessageBox.Show(wiaScanner.OutputFiles(0) + "" & <span data-scayt_word="Chr" data-scaytid="62">Chr</span>(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "Could not find any barcode.")


        End If

    End Sub
End Class

The demo video shows how to read Code39 barcode with ByteScout Barcode Reader SDK:

Tutorials:

prev
next