Beispiel #1
0
        /// <summary>
        /// Estimate the barcode width
        /// </summary>
        /// <param name="InputData"></param>
        /// <param name="BarWeight"></param>
        /// <param name="AddQuietZone"></param>
        /// <returns></returns>
        public static int EstimateWidth(string InputData, int BarWeight, bool AddQuietZone)
        {
            // get the Code128 codes to represent the message
            Code128Content content = new Code128Content(InputData);

            int[] codes = content.Codes;

            int width;

            width = ((codes.Length - 3) * 11 + 35) * BarWeight;
            //height = Convert.ToInt32(System.Math.Ceiling(Convert.ToSingle(width) * .15F));

            if (AddQuietZone)
            {
                width += 2 * cQuietWidth * BarWeight;  // on both sides
            }

            // first we need to get the total width of barcode, to calculate scale value
            float barcode_width = 0;

            if (AddQuietZone)
            {
                barcode_width += 2 * cQuietWidth * BarWeight;  // on both sides
            }
            for (int codeidx = 0; codeidx < codes.Length; codeidx++)
            {
                int code = codes[codeidx];

                // take the bars two at a time: a black and a white
                for (int bar = 0; bar < 8; bar += 2)
                {
                    float barwidth = cPatterns[code, bar] * BarWeight;
                    float spcwidth = cPatterns[code, bar + 1] * BarWeight;

                    // advance cursor beyond this pair
                    barcode_width += (barwidth + spcwidth);
                }
            }
            return((int)Math.Ceiling(barcode_width));
        }
Beispiel #2
0
        /// <summary>
        /// Make an image of a Code128 barcode for a given string
        /// </summary>
        /// <param name="InputData">Message to be encoded</param>
        /// <param name="BarWeight">Base thickness for bar width (1 or 2 works well)</param>
        /// <param name="AddQuietZone">Add required horiz margins (use if output is tight)</param>
        /// <returns>An Image of the Code128 barcode representing the message</returns>
        public static int MakeBarcodeImage(string InputData, int BarWeight, bool AddQuietZone, ref Graphics gr, float y, float max_width)
        {
            float scale = 1f;
            // get the Code128 codes to represent the message
            Code128Content content = new Code128Content(InputData);

            int[] codes = content.Codes;

            int width, height;

            width = ((codes.Length - 3) * 11 + 35) * BarWeight;
            //height = Convert.ToInt32(System.Math.Ceiling(Convert.ToSingle(width) * .15F));
            height = 40;

            if (AddQuietZone)
            {
                width += 2 * cQuietWidth * BarWeight;  // on both sides
            }

            // first we need to get the total width of barcode, to calculate scale value
            float barcode_width = 0;

            if (AddQuietZone)
            {
                barcode_width += 2 * cQuietWidth * BarWeight;  // on both sides
            }
            for (int codeidx = 0; codeidx < codes.Length; codeidx++)
            {
                int code = codes[codeidx];

                // take the bars two at a time: a black and a white
                for (int bar = 0; bar < 8; bar += 2)
                {
                    float barwidth = cPatterns[code, bar] * BarWeight;
                    float spcwidth = cPatterns[code, bar + 1] * BarWeight;

                    // advance cursor beyond this pair
                    barcode_width += (barwidth + spcwidth);
                }
            }

            if (barcode_width > max_width)
            {
                //need to scale
                scale = max_width / barcode_width;
            }

            // skip quiet zone
            float cursor = 0;

            cursor += gr.VisibleClipBounds.Width / 2 - barcode_width * scale / 2;

            if (AddQuietZone)
            {
                cursor += cQuietWidth * BarWeight * scale;
            }

            for (int codeidx = 0; codeidx < codes.Length; codeidx++)
            {
                int code = codes[codeidx];

                // take the bars two at a time: a black and a white
                for (int bar = 0; bar < 8; bar += 2)
                {
                    float barwidth = cPatterns[code, bar] * BarWeight * scale;
                    float spcwidth = cPatterns[code, bar + 1] * BarWeight * scale;

                    // if width is zero, don't try to draw it
                    if (barwidth > 0)
                    {
                        gr.FillRectangle(System.Drawing.Brushes.Black, cursor, y, barwidth, height);
                    }

                    // note that we never need to draw the space, since we
                    // initialized the graphics to all white

                    // advance cursor beyond this pair
                    cursor += (barwidth + spcwidth);
                }
            }

            return(height);
        }