Click here to Skip to main content
11,707,254 members (54,259 online)
Click here to Skip to main content

Bar Code Drawing

, 9 Aug 2007 CPOL 20.6K 21
Rate this:
Please Sign up or sign in to vote.
Bar Code drawing with 2 of 5 Interleaved format

Introduction

I was looking for a C# sample on how to draw barcodes in various formats and I could not find a sample for "2 of 5 interleaved" format, so I wrote my own using some guidance from here.

Points of Interest

To use this code, simply paste the code into the default form of a new C# Windows project in Visual Studio 2005. Drop a picturebox on the form and set the "Image" for this control to the output from the print2of5Interleaved() method, pictureBox1.Image=print2of5Interleaved();.

public Bitmap Print2of5Interleaved()
{
string Content = "12345678";
string CheckSum = CalcCheckSum(Content);
string startcode = "1010";
string stopcode = "1101";
int startX = 0;
int startY = 0;
int endY = startY + 40;
int curX;
int sectionIndex = 0;
int pairIndex = 0;
int barIndex = 0;
int spaceIndex = 0;

Graphics g;
Bitmap bmp = new Bitmap(100, 80);
g = Graphics.FromImage(bmp);

curX = startX;
Content = Content + CheckSum;
if ((Content.Length % 2) != 0)
{
//odd number, fill in a leading zero
Content = "0" + Content;
}
//draw the start marker
foreach (char digit in startcode)
{
if (digit == '1')
{
g.DrawLine(Pens.Black, curX, startY, curX, endY);
curX += 1;
}
else
{
curX += 1;
}
}
//draw the content
for (int i = 0; i < Content.Length; i += 2)
{
string pair = Content.Substring(i, 2);
string barPattern = Get2of5Pattern(pair.Substring(0, 1));
string spacePattern = Get2of5Pattern(pair.Substring(1, 1));
barIndex = 0;
spaceIndex = 0;
sectionIndex = 0;
while (sectionIndex < 10)
{
if ((sectionIndex % 2) == 0)
{
//bar 0,2,4,6,8 positions
pairIndex = 0;
if (barPattern.Substring(barIndex, 1) == "W")
{
//draw wide bar
while (pairIndex < 2)
{
g.DrawLine(Pens.Black, curX + pairIndex, startY, curX + pairIndex, endY);
pairIndex++;
}
curX = curX + 2;
}
else
{
//draw narrow bar
g.DrawLine(Pens.Black, curX + pairIndex, startY, curX + pairIndex, endY);
curX = curX + 1;
}
barIndex++;
}
else
{
//space 1,3,5,7,8 positions
if (spacePattern.Substring(spaceIndex, 1) == "W")
{
//simulate drawing a wide white space
curX = curX + 2;
}
else
{
//simulate drawing a narrow white space
curX = curX + 1;
}
spaceIndex++;
}
sectionIndex += 1;
}
}
//draw the stop marker
foreach (char digit in stopcode)
{
if (digit == '1')
{
g.DrawLine(Pens.Black, curX, startY, curX, endY);
curX += 1;
}
else
{
curX += 1;
}
}
return bmp;
} 

Here is the code for the two helper methods:

public string CalcCheckSum(string CheckNum)
{
int i;
int j;
int checkval = 0;
j = 3;
i = CheckNum.Length - 1;
while (i > 0)
{
checkval += Convert.ToInt32(CheckNum.Substring(i, 1)) * j;
j = j ^ 2;
i -= 1;
}
checkval = (10 - (checkval % 10)) % 10;
return checkval.ToString();
}

public string Get2of5Pattern(string letter)
{
string tmpPattern = "";
switch (letter)
{
case "0":
tmpPattern = "NNWWN";
break;
case "1":
tmpPattern = "WNNNW";
break;
case "2":
tmpPattern = "NWNNW";
break;
case "3":
tmpPattern = "WWNNN";
break;
case "4":
tmpPattern = "NNWNW";
break;
case "5":
tmpPattern = "WNWNN";
break;
case "6":
tmpPattern = "NWWNN";
break;
case "7":
tmpPattern = "NNNWW";
break;
case "8":
tmpPattern = "WNNWN";
break;
case "9":
tmpPattern = "NWNWN";
break;
}
return tmpPattern;
} 

Points of Interest

If anyone tests this with a barcode scanner and encounters any problems, let me know!

History

  • 9th August, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RedneckProgrammer
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThe image never change Pin
Member 1176213412-Jun-15 9:01
memberMember 1176213412-Jun-15 9:01 
QuestionBarcode scanner give wrong result. Add one more number at the end of the string Pin
Member 87794357-Nov-14 1:41
memberMember 87794357-Nov-14 1:41 
GeneralMy vote of 4 Pin
nfrench22-Oct-13 1:37
membernfrench22-Oct-13 1:37 
Questiondrawing Interleaved 2of5 in C# Pin
Zacbr24-Sep-12 4:20
memberZacbr24-Sep-12 4:20 
GeneralRe: Format Pin
fotr21-Jun-10 0:44
memberfotr21-Jun-10 0:44 
GeneralFormat Pin
NotFromIndia18-Jun-10 5:53
memberNotFromIndia18-Jun-10 5:53 
GeneralWebForm Pin
fotr18-Jun-10 0:30
memberfotr18-Jun-10 0:30 
GeneralGood work! One flaw though... Pin
Thomas-H.8-May-08 6:09
memberThomas-H.8-May-08 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 9 Aug 2007
Article Copyright 2007 by RedneckProgrammer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid