Introduction
This application will allow you to generate a UPC-A barcode. It includes the checksum feature.
Background
I originally built this application in VB.NET. While I was learning C#.NET, I decided to re-write it in C#.NET because at the time I was learning that application. I built this application about a year ago, and have not looked at the code since then. I was just hoping someone could really benefit from it. All I ask is if you do use it, please email me, and let me know how you used it, and proper credit (my name) would be cool, but not necessary.
Using the Code
- First create a new instance of the
cUPCA
class.
- Checksum to correct the input value with the correct last number, or to check to see if it is a valid UPC-A barcode.
- Create a new instance of an image.
- Set the back color to white.
- Pull back from the class to the image (this contains the barcode and numbers).
- Set your image to equal the image of the barcode.
cUPCA upca = new cUPCA();
if(this.txtTextToAdd.Text.Length == 12)
{
this.txtTextToAdd.Text = this.txtTextToAdd.Text.Substring(0, 11) +
upca.GetCheckSum(this.txtTextToAdd.Text).ToString();
Image img;
img = upca.CreateBarCode(this.txtTextToAdd.Text, 2);
this.pctBarCode.Left = System.Convert.ToInt32(
(this.pnlWhiteBack.Width / 2) - (img.Width / 2));
this.pctBarCode.Image = img;
this.txtTextToAdd.SelectAll();
}
else
{
this.pctBarCode.Image = null;
}
Points of Interest
To test this application, I bought a barcode scanner from Ebay for 5 dollars, and it works. I printed the barcodes out on paper with my laser printer, and they scan perfectly. I am also at times able to scan the barcode from the actual screen.
It was just a fun project that allowed me to learn a little bit more about the graphics class.
For the checksum class, I used the same code that was used in this article.
History
- 7th July, 2007: Initial post