QUESTIONS OR COMMENTS ABOUT PRODUCTS? WRITE US AT SUPPORT@BYTESCOUT.COM OR USE THIS FORM

How to upload image in ASP.NET and read barcode value from this image using Bytescout.BarCode Reader SDK for .NET 2.00 or higher

The tutorial demonstrates how to upload photo image with barcode and then recognize barcode value using BarCode Reader SDK in ASP.NET application

The following sample demonstrates how to upload image with barcode and then read barcode from it using Bytescout BarCode Reader SDK for .NET

You can find full source code of this sample in \Examples\Advanced Examples\ folder of Bytescout BarCode Reader SDK installation.

ASP.NET application (design mode):

ASP.NET design mode application screenshot

Running barcode recognition ASP.NET application:

net barcode reader

1) Visual Basic in ASP.NET

Default.aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebTestVB._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Web Barcode Reader Tester (VB)</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<div>

Click browse button to upload an image<br />

<br />

<asp:FileUpload ID="FileUpload1" runat="server" /><br />

<br />

<asp:Button ID="UploadButton" runat="server" OnClick="UploadButton_Click" Text="Upload file" />

<br />

<br />

<asp:Label ID="UploadStatusLabel" runat="server" Text=""></asp:Label>

<br />

<asp:ListBox ID="ListBox1" runat="server" Visible="False"></asp:ListBox><br />

<br />

<asp:Image ID="Image1" runat="server" Visible="False" /></div>



</div>

</form>

</body>

</html>

Default.aspx.vb:

Imports System

Imports System.Data

Imports System.Configuration

Imports System.Collections

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

Imports System.Drawing

Imports System.Collections.Generic

Imports Bytescout.BarCodeReader

Partial Public Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim savePath As String = "\uploads\"

If FileUpload1.HasFile Then

Dim fileName As String = FileUpload1.FileName

savePath += fileName

FileUpload1.SaveAs(Server.MapPath(savePath))

Dim bitmap As Bitmap = Nothing

Try

bitmap = New Bitmap(FileUpload1.FileContent)

Catch generatedExceptionName As System.Exception

End Try

If bitmap Is Nothing Then

UploadStatusLabel.Visible = True

UploadStatusLabel.Text = "Your file is not an image"

Image1.Visible = False

ListBox1.Visible = False

Else

UploadStatusLabel.Visible = False

Image1.ImageUrl = savePath

Image1.Visible = True

ListBox1.Items.Clear()

ListBox1.Visible = True

findBarcodes(Server.MapPath(savePath))

If ListBox1.Items.Count = 0 Then

ListBox1.Items.Add("No barcodes found")

End If

End If

Else

' Notify the user that a file was not uploaded.

UploadStatusLabel.Text = "You did not specify a file to upload."

End If

End Sub

Private Sub findBarcodes(ByVal fileName As String)

Dim reader As New Reader(fileName)

For Each barcode As FoundBarcode In reader.FoundBarcodes

ListBox1.Items.Add([String].Format("{0} : {1}", barcode.Type, barcode.Value))

Next

End Sub

End Class

 

2) Visual C# in ASP.NET

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTestSharp._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Web Barcode Reader Tester (C#)</title>

</head>

<body>

<form id="form1" runat="server">

<div>

Click browse button to upload an image<br />

<br />

<asp:FileUpload ID="FileUpload1" runat="server" /><br />

<br />

<asp:Button id="UploadButton"

Text="Upload file"

OnClick="UploadButton_Click"

runat="server">

</asp:Button>

<br />

<br />

<asp:Label id="UploadStatusLabel" Text="" runat="server"></asp:Label>

<br />

<asp:ListBox ID="ListBox1" runat="server" Visible="False"></asp:ListBox><br />

<br />

<asp:Image ID="Image1" runat="server" Visible="False" /></div>

</form>

</body>

</html>

Default.aspx.cs:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Drawing;

using System.Collections.Generic;

using Bytescout.BarCodeReader;

namespace WebTestSharp

{

public partial class _Default : System.Web.UI.Page

{

protected void UploadButton_Click(object sender, EventArgs e)

{

String savePath = @"\uploads\";

if (FileUpload1.HasFile)

{

String fileName = FileUpload1.FileName;

savePath += fileName;

FileUpload1.SaveAs(Server.MapPath(savePath));



Bitmap bitmap = null;

try

{

bitmap = new Bitmap(FileUpload1.FileContent);

}

catch (System.Exception)

{

}

if (bitmap == null)

{

UploadStatusLabel.Visible = true;

UploadStatusLabel.Text = "Your file is not an image";

Image1.Visible = false;

ListBox1.Visible = false;

}

else

{

UploadStatusLabel.Visible = false;

Image1.ImageUrl = savePath;

Image1.Visible = true;

ListBox1.Items.Clear();

ListBox1.Visible = true;

findBarcodes(Server.MapPath(savePath));

if (ListBox1.Items.Count == 0)

ListBox1.Items.Add("No barcodes found");

}

}

else

{

// Notify the user that a file was not uploaded.

UploadStatusLabel.Text = "You did not specify a file to upload.";

}

}

private void findBarcodes(string fileName)

{

Reader reader = new Reader(fileName);

foreach (FoundBarcode barcode in reader.FoundBarcodes)

ListBox1.Items.Add(String.Format("{0} : {1}", barcode.Type, barcode.Value));

}

}

}