Barcode drawing question

General discussions about GdPicture.NET.
Post Reply
Ezio
Posts: 16
Joined: Thu Apr 09, 2009 5:17 pm

Barcode drawing question

Post by Ezio » Thu Nov 03, 2011 7:23 pm

Hi Loic,
I need create a Bitmap with 1D barcode on it.

Follwing a code excerpt i write.

Code: Select all


Calling code excerpt

        private string Encoded_Value = "";
        private Barcode1DWriterType Encoded_Type = Barcode1DWriterType.Barcode1DWriterCode39;
        private Image _Encoded_Image = null;
        private Color _ForeColor = Color.Black;
        private Color _BackColor = Color.White;
        private int _Width = 400;
        private int _Height = 100;
        private bool _IncludeLabel = false;
        private Font _LabelFont = new Font("Microsoft Sans Serif", 10, System.Drawing.FontStyle.Bold);
        private BarcodeAlign _Alignment = BarcodeAlign.BarcodeAlignCenter;
        private LabelPositions _LabelPosition = LabelPositions.BOTTOMCENTER;
        private RotateFlipType _RotateFlipType = RotateFlipType.RotateNoneFlipNone;
        private GdPictureImaging GDImaging = new GdPictureImaging();
        private string LicenseNumber = "XXXX"; 
        private string LicenseNumberUpgrade = "YYYY";
        private string gdError;

                       .
                       .
                       .
                       .

            Encoded_Value = "0800853012780";   //"BCODE";
            Encoded_Type = Barcode1DWriterType.Barcode1DWriterCode39;
            IncludeLabel =  true;
            Alignment = BarcodeAlign.BarcodeAlignCenter;
            LabelPosition = LabelPositions.BOTTOMCENTER;
            RotateFlipType = RotateFlipType.RotateNoneFlipNone;
                topOffset = leftOffset = 0;

            //===== Encoding performed here =====
            bcw = 2 * leftOffset;
            bch = topOffset;
            if (IncludeLabel == true)
                bch += topOffset;

            if (BarcodeDraw(Encoded_Type, EncodeData, IncludeLabel, leftOffset, topOffset, Width - bcw, Height - bch, Color.Black, Alignment, (float)RotateFlipType) == false)
                MessageBox.Show(gdError);



// function to draw barcode e return a Bitmap
 private bool BarcodeDraw(Barcode1DWriterType bcType, string bcData,bool drawLabel, int left, int top, int width, int height, Color barColor,BarcodeAlign Alignment, float angle)
        {
            GdPictureStatus GdRet;

             GDImaging.SetLicenseNumberUpgrade(LicenseNumber, LicenseNumberUpgrade);

            int pImageID = GDImaging.CreateNewGdPictureImage(Width, Height, 24, BackColor);
            
            GdRet = GDImaging.Barcode1DWrite(pImageID, bcType, bcData, left, top, width, height,barColor, Alignment, angle);
            gdError = GdRet.ToString();

            if (GdRet != GdPictureStatus.OK)
                return false;
            
            Bitmap = GDImaging.GetBitmapFromGdPictureImage(pImageID);

            if (drawLabel == true)
                Add_Label(Bitmap);     // Add code string

            GDImaging.ReleaseGdPictureImage(pImageID);
            // rilascia le risorse allocate da GDPicture
            GDImaging.ClearGdPicture();
            return true;
        }

When I call the function BarcodeDraw() with this parameters:
width = 400, height = 100, top = 0, left = 0
I get expected image (see 400x100.bmp) :D

If I reduce width to 350 I get a small barcode centered in image (see 350x100.bmp) :(

Please can you explain logic used to calc width of bars??
Thank you in advance.
Last edited by Ezio on Thu Nov 03, 2011 7:28 pm, edited 1 time in total.

Ezio
Posts: 16
Joined: Thu Apr 09, 2009 5:17 pm

Re: Barcode drawing question

Post by Ezio » Thu Nov 03, 2011 7:26 pm

Images.zip
barcodes image file
(4.23 KiB) Downloaded 147 times
Ooops attchments

User avatar
Loïc
Site Admin
Posts: 5651
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Barcode drawing question

Post by Loïc » Thu Nov 03, 2011 7:57 pm

Hi Ezio,

Could you provide a code snippet that i can run as is ?

Thank you for your understanding.

Kind regards,

Loïc

Ezio
Posts: 16
Joined: Thu Apr 09, 2009 5:17 pm

Re: Barcode drawing question

Post by Ezio » Fri Nov 04, 2011 11:42 am

prova.zip
Test project
(35.14 KiB) Downloaded 152 times
Hi Loic,
in attached file you can find a sample code I use to create barcode.

I tried from 250x100 to 400x100 barcode size and it seems to change bars dimension starting at 390x100.
creating different barcode size, I get same barcode from 250 to 380 pixel width.

Kind Regards
Ezio

User avatar
Loïc
Site Admin
Posts: 5651
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Barcode drawing question

Post by Loïc » Fri Nov 04, 2011 12:31 pm

Ezio, thank you very much for the clear application.

I understand the issue now. Let me explain:

Based on the width parameter passed to the barcode drawing engine, a minimum bar width will be computed. This bar width must be a real value > 0. IE: 1, 2, 3, 4...

In situation 1, the min bar width is equal to 1.
In situation 2, the min bar width is equal to 2.

Therefore, in the situation 2 the engine will be able to use more space to render the barcode than in situation 1. This explains why the produced barcode is wider.

If you want to get the same barcode width in any situation you have to:
- Determines the minimum width needed to paint a barcode with a bar width of 1. See method: Barcode1DWriteGetMinWidth()
- Create a tmp bitmap of the computed width. For the height I suggest width /3, but you can use any value.
- And then, render the tmp bitmap on your bitmap in which you want to see your barcode.

But keep in mind: this method implies to interpolate your barcode bars which usually results in a harder barcode detection (if any).

I hope my explanations are clear enough. Let me know there is something you do not understand.

Kind regards,

Loïc

Ezio
Posts: 16
Joined: Thu Apr 09, 2009 5:17 pm

Re: Barcode drawing question

Post by Ezio » Fri Nov 04, 2011 1:22 pm

Loic, thank you for your explanation. This is what I imagined.
I need produce surely readable barcode at low resolution, usually images are scanned at 200DPI resolution.

To get this result I must create barcode where thin bar is always 3 pixel at least when is scanned at 200 DPI, or if you prefer about 4/10 mm.

if I understand I must proceed as follow:
1 - I get minimum width required to draw a barcode with thin bar dimension of 1 Pixel, using method: Barcode1DWriteGetMinWidth()
2 - I create a GdPictureImage where the width will be calculated as the result of previous call multiply by 3, at least.
3 - I create barcode on it and the thin bar will be about 4/10 mm.

Is this a correct approach to provide a solution to my problem?
can you comfirm this or I misunderstood your explanations?

Kind regards
Ezio

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests