ByteScout Barcode Reader SDK – ASP.NET – Barcodes From Image (C#)

  • Home
  • /
  • Articles
  • /
  • ByteScout Barcode Reader SDK – ASP.NET – Barcodes From Image (C#)

ByteScout Barcode Reader SDK – ASP.NET – Barcodes From Image (C#)

Default.aspx.cs

using System;
using System.Drawing;
using System.IO;
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 virtualFilePath = Path.Combine(savePath, FileUpload1.FileName);

                string serverFilePath = Server.MapPath(virtualFilePath);
                if (!Directory.Exists(Path.GetDirectoryName(serverFilePath)))
                    Directory.CreateDirectory(Path.GetDirectoryName(serverFilePath));

                FileUpload1.SaveAs(serverFilePath);

				Image image = null;

				try
				{
                    using (Stream fileStream = File.OpenRead(serverFilePath))
				    {
				        image = Image.FromStream(fileStream);
				    }
				}
				catch (Exception)
				{
				}

				if (image == null)
				{
					UploadStatusLabel.Visible = true;
					UploadStatusLabel.Text = "Your file is not an image.";
					Image1.Visible = false;
					ListBox1.Visible = false;
				}
				else
				{
					UploadStatusLabel.Visible = false;
                    Image1.ImageUrl = virtualFilePath;
					Image1.Visible = true;
				    Image1.Width = image.Width;
				    Image1.Height = image.Height;
					ListBox1.Items.Clear();
					ListBox1.Visible = true;

                    FindBarcodes(serverFilePath);
					
					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();

			// Limit search to 1D barcodes only (exclude 2D barcodes to speed up the search).
			// Change to reader.BarcodeTypesToFind.SetAll() to scan for all supported 1D and 2D barcodes 
			// or select specific type, e.g. reader.BarcodeTypesToFind.PDF417 = True
			reader.BarcodeTypesToFind.SetAll1D();

			// reader.MediumTrustLevelCompatible = true; // uncomment this line to enable Medium Trust compatible mode (slows down the recognition process as direct image data access is disabled in Medium Trust mode)

			reader.ReadFromFile(fileName);

			foreach (FoundBarcode barcode in reader.FoundBarcodes)
			{
				ListBox1.Items.Add(String.Format("{0} : {1}", barcode.Type, barcode.Value));
			}
		}
	}
}

Web.config

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>


  Click here to get your Free Trial version of the SDK

prev
next