In this article, we will see how to generate BarCode and after that how to read the bar code. Let us see.
First, we have to download the Free BarCode Font from this link
After that, we will generate an event of the button and right-click select view code and write below code
protected void btnGenerate_Click(object sender, EventArgs e)
Now we will run the project and see the output
And generate the event of the button and right-click and select view code and write below code
protected void UploadButton_Click(object sender, EventArgs e)
First, we have to download the Free BarCode Font from this link
- After downloading the file we extract the ZIP file.
- Click and Execute INSTALL.exe file.
- After installation is completed restart your machine.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<h2>Generate Bar Code</h2>
<asp:TextBox CssClass="form-control col-4" ID="textCode" runat="server"></asp:TextBox>
<br /><br />
<asp:Button CssClass="btn btn-primary" ID="btnGenerate" runat="server" Text="Generat" OnClick="btnGenerate_Click" />
<hr />
<asp:PlaceHolder ID="plBarCode" runat="server"></asp:PlaceHolder>
</div>
</div>
</form>
</body>
After that, we will generate an event of the button and right-click select view code and write below code
protected void btnGenerate_Click(object sender, EventArgs e)
{
string barCode =
textCode.Text;
System.Web.UI.WebControls.Image imageBarCode = new
System.Web.UI.WebControls.Image();
using (Bitmap bitMaps = new Bitmap(barCode.Length
* 40, 180))
{
using (Graphics graphics =
Graphics.FromImage(bitMaps))
{
Font fnt = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new
SolidBrush(Color.Black);
SolidBrush whiteBrush = new
SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush,
0, 0, bitMaps.Width, bitMaps.Height);
graphics.DrawString("*" + barCode + "*", fnt, blackBrush,
point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMaps.Save(ms,
System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imageBarCode.ImageUrl = ("data:image/png;base64," +
Convert.ToBase64String(byteImage));
}
plBarCode.Controls.Add(imageBarCode);
}
}
Now we will see how to read the bar code and display the original code
we are going to write the code to read the bar code but before that, we have to install Bytescout.BarCode.Reader dll So for this just copy and below code
Install-Package Bytescout.BarCode.Reader -Version 10.1.0.1788
Go tool option and go new get package manager -> Package Manager console and press enter.
So for that again add aspx page and write below code
<body>
<form id="form1" runat="server">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-3">
<div align="center">
<br />
<asp:FileUpload AllowMultiple="true" CssClass="form-control" ID="barCodeUpload" runat="server" /><br />
<br />
<asp:Button CssClass="btn
btn-primary" ID="UploadButton" Text="Upload file" OnClick="UploadButton_Click" runat="server"></asp:Button>
<br />
<br />
<asp:Label CssClass="btn
alert-danger" ID="UploadStatusLabel" Text="" runat="server"></asp:Label>
<br />
<asp:ListBox CssClass="col-md-8" ID="lstDisplayBarCode" runat="server" Visible="False"></asp:ListBox><br />
<br />
<asp:Image CssClass="form-control" ID="BarCodeImage" runat="server" Visible="False" />
</div>
</div>
</div>
</form>
</body>
And generate the event of the button and right-click and select view code and write below code
protected void UploadButton_Click(object sender, EventArgs e)
{
String localSavePath = "~/UploadFiles/";
if
(barCodeUpload.HasFile)
{
String fileName = barCodeUpload.FileName;
localSavePath += fileName;
barCodeUpload.SaveAs(Server.MapPath(localSavePath));
Bitmap bitmap = null;
try
{
bitmap = new
Bitmap(barCodeUpload.FileContent);
}
catch (Exception ex)
{
ex.ToString();
}
if (bitmap == null)
{
UploadStatusLabel.Visible = true;
UploadStatusLabel.Text = "Your file is
not an image";
BarCodeImage.Visible = false;
lstDisplayBarCode.Visible = false;
}
else
{
UploadStatusLabel.Visible = false;
BarCodeImage.ImageUrl =
localSavePath;
BarCodeImage.Visible = true;
lstDisplayBarCode.Items.Clear();
lstDisplayBarCode.Visible = true;
findBarcodes(Server.MapPath(localSavePath));
if (lstDisplayBarCode.Items.Count == 0)
{
lstDisplayBarCode.Items.Add("No barcodes
found");
}
}
}
else
{
UploadStatusLabel.Text = "Please upload the bar code Image.";
}
}
private void findBarcodes(string fileName)
{
Reader reader = new Reader();
reader.BarcodeTypesToFind.SetAll1D();
reader.ReadFromFile(fileName);
foreach (FoundBarcode barcode
in reader.FoundBarcodes)
{
string[] strBar =
barcode.Value.Split(new[] { ">" }, StringSplitOptions.None);
lstDisplayBarCode.Items.Add(String.Format("{0} : {1}", barcode.Type,
strBar[0] + " "));
}
}
Now we will see the output
No comments:
Post a Comment