VintaSoft Barcode .NET SDK 16.0: Documentation for .NET developer
In This Topic
How to read barcodes from an image if image data is stored in a byte array?
In This Topic
Here is C#/VB.NET code that demonstrates how to read barcodes from image if image data are stored in a byte array:
/// <summary>
/// Reads barcodes from an image. Image data are stored in a byte array.
/// </summary>
/// <param name="reader">The barcode reader.</param>
/// <param name="width">The image width.</param>
/// <param name="height">The image height.</param>
/// <param name="stride">The image stride.</param>
/// <param name="format">The image format.</param>
/// <param name="pixelData">The image pixel data.</param>
public static Vintasoft.Barcode.IBarcodeInfo[] ReadBarcodes(
    Vintasoft.Barcode.BarcodeReader reader, 
    int width, 
    int height, 
    int stride, 
    System.Drawing.Imaging.PixelFormat format, 
    byte[] pixelData)
{
    // lock managed data
    System.Runtime.InteropServices.GCHandle bitmapDataHandle =
        System.Runtime.InteropServices.GCHandle.Alloc(pixelData, GCHandleType.Pinned);
    if (!bitmapDataHandle.IsAllocated)
        throw new InvalidOperationException();

    // get IntPtr to managed data
    IntPtr pixelDataPtr = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(pixelData, 0);

    try
    {
        // read barcodes
        return ReadBarcodes(reader, width, height, stride, format, pixelDataPtr);
    }
    finally
    {
        // free GCHandle
        bitmapDataHandle.Free();
    }
}

/// <summary>
/// Reads barcodes from an image. Image data are stored in an unmanaged memory.
/// </summary>
/// <param name="reader">The barcode reader.</param>
/// <param name="width">The image width.</param>
/// <param name="height">The image height.</param>
/// <param name="stride">The image stride.</param>
/// <param name="format">The image format.</param>
/// <param name="pixelDataScan0">The Scan0 pointer to an image pixel data.</param>
public static Vintasoft.Barcode.IBarcodeInfo[] ReadBarcodes(Vintasoft.Barcode.BarcodeReader reader, int width,
    int height, int stride, System.Drawing.Imaging.PixelFormat format, IntPtr pixelDataScan0)
{
    using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, stride, format, pixelDataScan0))
    {
        // read barcodes
        return reader.ReadBarcodes(bitmap);
    }
}
''' <summary>
''' Reads barcodes from an image. Image data are stored in a byte array.
''' </summary>
''' <param name="reader">The barcode reader.</param>
''' <param name="width">The image width.</param>
''' <param name="height">The image height.</param>
''' <param name="stride">The image stride.</param>
''' <param name="format">The image format.</param>
''' <param name="pixelData">The image pixel data.</param>
Public Shared Function ReadBarcodes(reader As Vintasoft.Barcode.BarcodeReader, width As Integer, _
    height As Integer, stride As Integer, format As System.Drawing.Imaging.PixelFormat, _
    pixelData As Byte()) As Vintasoft.Barcode.IBarcodeInfo()

    ' lock managed data
    Dim bitmapDataHandle As System.Runtime.InteropServices.GCHandle = _
        System.Runtime.InteropServices.GCHandle.Alloc(pixelData, GCHandleType.Pinned)
    If Not bitmapDataHandle.IsAllocated Then
        Throw New InvalidOperationException()
    End If

    ' get IntPtr to managed data
    Dim pixelDataPtr As IntPtr = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(pixelData, 0)

    Try
        ' read barcodes
        Return ReadBarcodes(reader, width, height, stride, format, pixelDataPtr)
    Finally
        ' free GCHandle
        bitmapDataHandle.Free()
    End Try
End Function

''' <summary>
''' Reads barcodes from an image. Image data are stored in an unmanaged memory.
''' </summary>
''' <param name="reader">The barcode reader.</param>
''' <param name="width">The image width.</param>
''' <param name="height">The image height.</param>
''' <param name="stride">The image stride.</param>
''' <param name="format">The image format.</param>
''' <param name="pixelDataScan0">The Scan0 pointer to an image pixel data.</param>
Public Shared Function ReadBarcodes(reader As Vintasoft.Barcode.BarcodeReader, width As Integer, _
    height As Integer, stride As Integer, format As System.Drawing.Imaging.PixelFormat, _
    pixelDataScan0 As IntPtr) As Vintasoft.Barcode.IBarcodeInfo()

    Using bitmap As New System.Drawing.Bitmap(width, height, stride, format, pixelDataScan0)
        ' read barcodes
        Return reader.ReadBarcodes(bitmap)
    End Using
End Function