Learn how to read barcodes from image in ASP.NET MVC C# with this source code sample. ByteScout BarCode Reader SDK is the barcode decoder with support for code 39, code 128, QR Code, Datamatrix, GS1, PDF417 and all other popular barcodes. Can read barcodes from images, pdf, tiff documents and live web camera. Supports noisy and damaged documents, can split and merge pdf and tiff documents based on barcodes. Can export barcode decoder results to XML, JSON, CSV and into custom data structures. It can be used to read barcodes from image using ASP.NET MVC C#.
This code snippet below for ByteScout BarCode Reader SDK works best when you need to quickly read barcodes from image in your ASP.NET MVC C# application. In your ASP.NET MVC C# project or application you may simply copy & paste the code and then run your app! Further enhancement of the code will make it more vigorous.
Our website provides trial version of ByteScout BarCode Reader SDK for free. It also includes documentation and source code samples.
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
<%@ 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" Height="74px" Width="71px" /></div> </form> </body> </html>
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
using System; using System.Drawing; using System.IO; using Bytescout.BarCodeReader; namespace WebTestSharp { /* IF YOU SEE TEMPORARY FOLDER ACCESS ERRORS: Temporary folder access is required for web application when you use ByteScout SDK in it. If you are getting errors related to the access to temporary folder like "Access to the path 'C:\Windows\TEMP\... is denied" then you need to add permission for this temporary folder to make ByteScout SDK working on that machine and IIS configuration because ByteScout SDK requires access to temp folder to cache some of its data for more efficient work. SOLUTION: If your IIS Application Pool has "Load User Profile" option enabled the IIS provides access to user's temp folder. Check user's temporary folder If you are running Web Application under an impersonated account or IIS_IUSRS group, IIS may redirect all requests into separate temp folder like "c:\temp\". In this case - check the User or User Group your web application is running under - then add permissions for this User or User Group to read and write into that temp folder (c:\temp or c:\windows\temp\ folder) - restart your web application and try again */ 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)); } } } }
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace WebTestSharp { public partial class _Default { /// <summary> /// form1 control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.HtmlControls.HtmlForm form1; /// <summary> /// FileUpload1 control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.FileUpload FileUpload1; /// <summary> /// UploadButton control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.Button UploadButton; /// <summary> /// UploadStatusLabel control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.Label UploadStatusLabel; /// <summary> /// ListBox1 control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.ListBox ListBox1; /// <summary> /// Image1 control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.Image Image1; } }
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace Barcodes_From_Image { public class Global : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { // Code that runs on application startup } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. } } }
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Barcodes_From_Image { public partial class SiteMaster : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { } } }
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Barcodes_From_Image { public partial class SiteMaster { /// <summary> /// HeadContent control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.ContentPlaceHolder HeadContent; /// <summary> /// MasterPageScriptManager control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.ScriptManager MasterPageScriptManager; /// <summary> /// HeadLoginView control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.LoginView HeadLoginView; /// <summary> /// HeadHomeLink control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.HtmlControls.HtmlAnchor HeadHomeLink; /// <summary> /// HeadAboutLink control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.HtmlControls.HtmlAnchor HeadAboutLink; /// <summary> /// MainContent control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.WebControls.ContentPlaceHolder MainContent; } }
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
<?xml version="1.0"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <!-- In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an atrribute "name" that has a value of "MyDB". <connectionStrings> <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> --> <system.web> <!-- In the example below, the "Replace" transform will replace the entire <customErrors> section of your web.config file. Note that because there is only one customErrors section under the <system.web> node, there is no need to use the "xdt:Locator" attribute. <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors> --> </system.web> </configuration>
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
<?xml version="1.0"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <!-- In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an atrribute "name" that has a value of "MyDB". <connectionStrings> <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> --> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> <!-- In the example below, the "Replace" transform will replace the entire <customErrors> section of your web.config file. Note that because there is only one customErrors section under the <system.web> node, there is no need to use the "xdt:Locator" attribute. <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors> --> </system.web> </configuration>
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
<?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>
Try ByteScout BarCode Reader SDK today: 60 Day Free Trial (on-premise) or Web API (on-demand version)
Get 60 Day Free Trial or Visit ByteScout BarCode Reader SDK Home Page
Explore ByteScout BarCode Reader SDK Documentation
Get ByteScout BarCode Reader SDK Free Training