Example #1
0
        private void DrawBarcode128(string encodeValue, Graphics g, Rectangle rect)
        {
            var image = _barcodeGenerator.GenerateBarcode(encodeValue, rect.Width, 25);

            image.SetResolution(g.DpiX, g.DpiY);
            g.DrawImageUnscaled(image, new Point(0, rect.Y));
        }
Example #2
0
        public IActionResult BarcodePart([FromQuery] GetPartRequest request)
        {
            var stream = new MemoryStream();
            var image  = _barcodeGenerator.GenerateBarcode(request.PartNumber, 300, 25);

            image.Save(stream, ImageFormat.Png);
            stream.Seek(0, SeekOrigin.Begin);
            return(new FileStreamResult(stream, "image/png"));
        }
        private void GenerateBarcode(string path)
        {
            IBarcodeEncoder enc = new Code128Encoder();

            enc.Text = "123456";

            BarcodeRenderMode mode = BarcodeRenderMode.None;

            mode |= BarcodeRenderMode.Numbered;

            enc.Sizer.Mode = mode;
            enc.Sizer.DPI  = 0;

            IBarcodeGenerator gen = enc.Generator;
            Bitmap            bmp = gen.GenerateBarcode(new Size(68, 40));

            bmp.Save(_path, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
Example #4
0
        private Bitmap GenerateBarcodeStandard()
        {
            Bitmap result      = null;
            Size   currentSize = new Size(0, 0);

            IBarcodeEncoder enc = GetEncoder(this.BarcodeType);

            if (enc == null)
            {
                throw new Exception("That encoder is not implemented yet.");
            }

            try
            {
                enc.Text = this.Data;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }

            BarcodeRenderMode mode = BarcodeRenderMode.None;

            if (this.Guarded)
            {
                mode |= BarcodeRenderMode.Guarded;
            }
            if (this.Notched)
            {
                mode |= BarcodeRenderMode.Notched;
            }
            if (this.Numbered)
            {
                mode |= BarcodeRenderMode.Numbered;
            }
            if (this.Braced)
            {
                mode |= BarcodeRenderMode.Braced;
            }
            if (this.Boxed)
            {
                mode |= BarcodeRenderMode.Boxed;
            }

            enc.Sizer.Mode = mode;
            enc.Sizer.DPI  = (float)this.Dpi.Value;

            bool calculResult = CalculateSize(ref currentSize);

            if (calculResult == false)
            {
                throw new Exception("Error calculating the size.");
            }

            if (!enc.Sizer.IsValidSize(currentSize))
            {
                throw new Exception("Invalid size.");
            }

            IBarcodeGenerator gen = enc.Generator;

            result = gen.GenerateBarcode(currentSize);

            return(result);
        }
Example #5
0
        private bool GenerateBarecode()
        {
            // --- STANDARD ---
            if (_rbStandard.Checked)
            {
                IBarcodeEncoder enc = GetEncoder();

                if (enc == null)
                {
                    MessageBox.Show("That encoder is not implemented yet.");
                    return(false);
                }

                try
                {
                    enc.Text = _tbData.Text;
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message);
                    return(false);
                }

                BarcodeRenderMode mode = BarcodeRenderMode.None;
                if (_cbGuarded.Checked)
                {
                    mode |= BarcodeRenderMode.Guarded;
                }
                if (_cbNotched.Checked)
                {
                    mode |= BarcodeRenderMode.Notched;
                }
                if (_cbNumbered.Checked)
                {
                    mode |= BarcodeRenderMode.Numbered;
                }
                if (_cbBraced.Checked)
                {
                    mode |= BarcodeRenderMode.Braced;
                }
                if (_cbBoxed.Checked)
                {
                    mode |= BarcodeRenderMode.Boxed;
                }

                enc.Sizer.Mode = mode;
                enc.Sizer.DPI  = (int)_nupDpi.Value;

                IBarcodeModularSizer mSizer = enc.Sizer as IBarcodeModularSizer;
                if (mSizer != null)
                {
                    if (_nudModuleWidth.Value != 0)
                    {
                        mSizer.Module = (float)_nudModuleWidth.Value;
                    }
                }

                bool calculResult = CalculateSize();
                if (calculResult == false)
                {
                    return(false);
                }

                if (!enc.Sizer.IsValidSize(_currentSize))
                {
                    MessageBox.Show("Invalid size.");
                    return(false);
                }

                IBarcodeGenerator gen = enc.Generator;
                _bmpPrimary      = gen.GenerateBarcode(_currentSize);
                _pbBarcode.Image = _bmpPrimary;
            }

            // --- HIBC ---
            else
            {
                ActiveUp.WebControls.SecondaryDataHibc secondData = new ActiveUp.WebControls.SecondaryDataHibc();
                if (_tbDate.Text != "")
                {
                    try
                    {
                        secondData.Date = DateTime.Parse(_tbDate.Text);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Invalid date format in the Date box.");
                        return(false);
                    }
                }

                if (_tbQuantity.Text != "")
                {
                    try
                    {
                        secondData.Quantity = int.Parse(_tbQuantity.Text);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("The value in the quantity box must be a number between 0 and 99999.");
                        return(false);
                    }
                }

                if (_tbSerial.Text != "")
                {
                    secondData.Lot = _tbSerial.Text;
                }

                if (_comboDateFormat.SelectedItem != null)
                {
                    try
                    {
                        secondData.DateFormat = (ActiveUp.WebControls.HibcDateFormat)Enum.Parse(typeof(ActiveUp.WebControls.HibcDateFormat), (string)_comboDateFormat.SelectedItem);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("An error occured in parsing the date format.");
                        return(false);
                    }
                }

                Regex licCheck = new Regex("[A-Z][A-Z0-9]{3}", RegexOptions.IgnoreCase);
                Regex pcnCheck = new Regex("[A-Z0-9]{1,13}", RegexOptions.IgnoreCase);
                Regex gs1Check = new Regex("[0-9]{12}");

                if (_rbHIBCLIC.Checked)
                {
                    //Check the LIC and PCN.
                    if (_tbLICorGS1.Text == "" || !licCheck.IsMatch(_tbLICorGS1.Text))
                    {
                        MessageBox.Show("The LIC is not formatted correctly.");
                        return(false);
                    }

                    if (_tbPCN.Text != "" && !pcnCheck.IsMatch(_tbPCN.Text))
                    {
                        MessageBox.Show("The PCN is not formatted correctly.");
                        return(false);
                    }
                }
                else
                {
                    //GS1
                    if (_tbLICorGS1.Text == "" || !gs1Check.IsMatch(_tbLICorGS1.Text))
                    {
                        MessageBox.Show("The GS1 is not formatted correctly.");
                        return(false);
                    }

                    if (_tbPCN.Text != "" && !pcnCheck.IsMatch(_tbPCN.Text))
                    {
                        MessageBox.Show("The PCN is not formatted correctly.");
                        return(false);
                    }
                }

                byte uom;
                try
                {
                    uom = byte.Parse((string)_comboUnitOfMeasurement.SelectedItem);
                }
                catch (Exception)
                {
                    MessageBox.Show("The Unit of Measurement is not set.");
                    return(false);
                }

                IBarcodeEncoder[] encoders;
                ActiveUp.WebControls.PrimaryEncodingMode   pMode;
                ActiveUp.WebControls.SecondaryEncodingMode sMode;


                try
                {
                    pMode = (ActiveUp.WebControls.PrimaryEncodingMode)Enum.Parse(typeof(ActiveUp.WebControls.PrimaryEncodingMode), (string)_comboPrimaryMode.SelectedItem);
                }
                catch (Exception)
                {
                    MessageBox.Show("An error occured in parsing the primary encoding mode.");
                    return(false);
                }

                try
                {
                    sMode = (ActiveUp.WebControls.SecondaryEncodingMode)Enum.Parse(typeof(ActiveUp.WebControls.SecondaryEncodingMode), (string)_comboSecondaryMode.SelectedItem);
                }
                catch (Exception)
                {
                    MessageBox.Show("An error occured in parsing the secondary encoding mode.");
                    return(false);
                }

                try
                {
                    if (_rbHIBCLIC.Checked)
                    {
                        encoders = ActiveUp.WebControls.HIBCEncoder.EncodeHIBC(_tbLICorGS1.Text, _tbPCN.Text, uom, secondData, pMode, sMode);
                    }
                    else
                    {
                        encoders = ActiveUp.WebControls.HIBCEncoder.EncodeGS1(uom.ToString() + _tbLICorGS1.Text, (_tbPCN.Text == "") ? null : _tbPCN.Text, secondData, pMode, sMode);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An exception occured:\n" + ex.Message);
                    return(false);
                }

                encoders[0].Sizer.DPI = (float)_nupDpi.Value;
                if (encoders.Length == 2)
                {
                    encoders[1].Sizer.DPI = (float)_nupDpi.Value;
                }

                encoders[0].Sizer.Mode = BarcodeRenderMode.Guarded | BarcodeRenderMode.Numbered;
                if (encoders.Length == 2)
                {
                    encoders[1].Sizer.Mode = BarcodeRenderMode.Guarded | BarcodeRenderMode.Numbered;
                }

                _bmpPrimary = encoders[0].Generator.GenerateBarcode(encoders[0].Sizer.Size);
                if (encoders.Length == 2)
                {
                    _bmpSecondary = encoders[1].Generator.GenerateBarcode(encoders[1].Sizer.Size);
                }

                if (_rbPrimary.Checked)
                {
                    _pbBarcode.Image = _bmpPrimary;
                }
                else
                {
                    _pbBarcode.Image = _bmpSecondary;
                }
            }

            return(true);
        }