Click here to Skip to main content
12,559,073 members (41,337 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked

Drawing an EAN13 barcode

, 18 Oct 2005
Rate this:
Please Sign up or sign in to vote.
Drawing an EAN13 barcode based on Neil Van Eps' and Rainman_63's articles.


I am very interested in Neil Van Eps' series of articles that shows how to draw barcodes. And I have been waiting for his next article since I had found his stuff on CodeProject. Actually, I need the articles for EAN8/13, UPCA/B. I searched in CodeProject, and found "Creating EAN-13 Barcodes with C#", by rainman_63. It's really helpful for me to understand clearly how to decode EAN barcodes.


I am not familiar with C#, and C/C++ is a unique language that I had known. So I was very confused to research rainman_63's C# project. However, his detailed guiding was enough for me to implement an EAN13 project with Visual C++. In this article, I shall not explain how the EAN13 encoding works, for further information, you can read rainman_63's article "Creating EAN-13 Barcodes with C#".

CEAN13 Class

My CEAN13 is based on the CBarcode class, that has been introduced in Neil Van Eps' series of articles. See the class declaration below.

class CBarcode
        void LoadData(CString csMessage, double dNarrowBar, double dFinalHeight, 
                      HDC pDC, int nStartingXPixel, int nStartingYPixel, 
                      double dRatio = 1.0);
        virtual void DrawBitmap() = 0;
        virtual void BitmapToClipboard() = 0;
        virtual ~CBarcode();
        long GetBarcodePixelWidth();
        long GetBarcodePixelHeight();
        CString m_csMessage;
        HDC m_hDC;
        long m_nFinalBarcodePixelWidth;
        long m_nNarrowBarPixelWidth;
        long m_nPixelHeight;
        long m_nStartingXPixel;
        long m_nStartingYPixel;
        long m_nSymbology;
        long m_nWideBarPixelWidth;
        virtual void DrawPattern(CString csPattern) = 0;

And here is the CEAN13 class declaration. It's implemented to draw an EAN13 barcode.

class CEAN13 : public CBarcode  
    virtual ~CEAN13();
    void LoadData(CString csMessage, double dNarrowBar, 
         double dFinalHeight, long nGuardbarHeight, HDC hDC, int 
         nStartingXPixel, int nStartingYPixel, double dRatio);
    void DrawBitmap();
    void BitmapToClipboard();
    long CalculateCheckSumDigit();

    long  m_nGuardbarHeight;
    CString RetrieveLeftOddParityPattern(int iNumber);
    CString RetrieveLeftEvenParityPattern(int iNumber);
    CString RetrieveRightPattern(int iNumber);
    CString RetrieveCountryCodePattern(int iNumber);

    void DrawPattern(CString csPattern);

Guard Bar

The UPC/EAN/JAN standards include specifications for some of the bars to extend below the main body of the bar code. These are called "guard" or "security" bars.

Using the CEAN13 Class

HDC hDC = GetDC()->m_hDC;
double dHeight = 1.5;
nGuardbarHeight = 20;
double dRatio = 3.0;
CEAN24 CodeEAN13;

CEAN13::DrawBitmap() Details

The DrawBitmap() function draws all the characters of the message. It divides a message into five parts and draws each part in turn.

  • Part 1: left quite zone and lead.
  • Part 2: country code and manufacture code.
  • Part 3: separator bars.
  • Part 4: product code.
  • Part 5: trailer and right quite zone.
void CEAN13::DrawBitmap()
    int i, tmpGuardBarHeight;
    DrawPattern("sssssssss"); // draw quite zone
    DrawPattern("bsb"); // draw lead

    CString strCountryCodePattern;
    strCountryCodePattern = RetrieveCountryCodePattern((int)m_csMessage.GetAt(0)-48);

    tmpGuardBarHeight = m_nGuardbarHeight;
    m_nGuardbarHeight = 0;
    for (i = 2 ; i < 7 ; i ++){
        if (strCountryCodePattern[i-2] == 'O')

        if (strCountryCodePattern[i-2] == 'E')

    m_nGuardbarHeight = tmpGuardBarHeight;

    DrawPattern("sbsbs"); // draw separator bars

    tmpGuardBarHeight = m_nGuardbarHeight;
    m_nGuardbarHeight = 0;
    for (i = 7 ; i < 12 ; i ++)

    m_nGuardbarHeight = tmpGuardBarHeight;
    DrawPattern("bsb"); // draw trailer bars
    DrawPattern("sssssssss"); // draw quite zone 


My coding could not be nice ... ;=) but I tried my best to inherit from Neil Van Eps' and rainman_63's articles. I joined CodeProject 2 years ago, but have not contributed anything. It's time to do something for my favorite website. I would like to thank Neil Van Eps and rainman_63, your contributions were very helpful for me, and for everybody who would be interested in barcode topics. Thanks for any feedback.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Lam Do
Software Developer (Senior)
Vietnam Vietnam
I joined CodeProject since working in MFC/C++ applications. For some personal reasons, I switched to Flash/Flex web applications and joined Pyramid Consulting Vietnam. I left Pyramid Consulting Vietnam in 2009 and started my free-lance jobs. Now I am a professional multimedia developer with specialized in Flash/Flex technologies and intend to turn back this community as a chance to contribute my knowledge in Flash/Flex technologies.

You may also be interested in...

Comments and Discussions

QuestionDetailed and helpful information Pin
jokefan23-Jul-14 20:27
memberjokefan23-Jul-14 20:27 - our experience Pin
null19778-May-13 4:15
membernull19778-May-13 4:15 
QuestionC# EAN13 Pin
Zacbr15-Feb-12 3:56
memberZacbr15-Feb-12 3:56 
Questionbarcode in code 39 C# Pin
Member 79252208-Jul-11 22:31
memberMember 79252208-Jul-11 22:31 
JokeThanks Pin
wizard_y1-Apr-07 20:53
memberwizard_y1-Apr-07 20:53 
GeneralThanks Pin
dannymis2-Nov-06 19:23
memberdannymis2-Nov-06 19:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.161026.1 | Last Updated 18 Oct 2005
Article Copyright 2005 by Lam Do
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid