Ejemplo n.º 1
0
        private void btnSave_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif";
            //sfd.AddExtension = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                BarcodeLib.SaveTypes savetype = BarcodeLib.SaveTypes.UNSPECIFIED;
                switch (sfd.FilterIndex)
                {
                case 1: /* BMP */ savetype = BarcodeLib.SaveTypes.BMP; break;

                case 2: /* GIF */ savetype = BarcodeLib.SaveTypes.GIF; break;

                case 3: /* JPG */ savetype = BarcodeLib.SaveTypes.JPG; break;

                case 4: /* PNG */ savetype = BarcodeLib.SaveTypes.PNG; break;

                case 5: /* TIFF */ savetype = BarcodeLib.SaveTypes.TIFF; break;

                default: break;
                } //switch
                b.SaveImage(sfd.FileName, savetype);
            }     //if
        }
Ejemplo n.º 2
0
        public static bool GenerateBarcode(
            int barcodeEncoding
            , int width
            , int height
            , BarcodeLib.AlignmentPositions alignmentPosition
            , System.Drawing.RotateFlipType rotateFlipType
            , BarcodeLib.LabelPositions labelPosition
            , string stringToEncode
            , string imagePathAndName
            , BarcodeLib.SaveTypes saveType
            , System.Windows.Forms.GroupBox barcode
            , ref string errorMessage
            )
        {
            errorMessage = string.Empty;

            BarcodeLib.Barcode b = new BarcodeLib.Barcode();
            b.Alignment = alignmentPosition;

            BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;

            if (barcodeEncoding > 1)
            {
                barcodeEncoding = 1;
            }

            switch (barcodeEncoding)
            {
            ////case 0: type = BarcodeLib.TYPE.UPCA; break;                             //"UPC-A"
            ////case 1: type = BarcodeLib.TYPE.UPCE; break;                             //"UPC-E"
            ////case 2: type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;          //"UPC 2 Digit Ext."
            ////case 3: type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;          //"UPC 5 Digit Ext".
            ////case 4: type = BarcodeLib.TYPE.EAN13; break;                            //"EAN-13"
            ////case 5: type = BarcodeLib.TYPE.JAN13; break;                            //"JAN-13"
            ////case 6: type = BarcodeLib.TYPE.EAN8; break;                             //"EAN-8"
            ////case 7: type = BarcodeLib.TYPE.ITF14; break;                            //"ITF-14"
            //case 0: type = BarcodeLib.TYPE.Interleaved2of5; break;                  //"Interleaved 2 of 5"
            //case 1: type = BarcodeLib.TYPE.Standard2of5; break;                     //"Standard 2 of 5"
            ////case 10: type = BarcodeLib.TYPE.Codabar; break;                          //"Codabar"
            ////case 11: type = BarcodeLib.TYPE.PostNet; break;                          //"PostNet"
            //case 2: type = BarcodeLib.TYPE.BOOKLAND; break;                        //"Bookland/ISBN"
            //case 3: type = BarcodeLib.TYPE.CODE11; break;                          //"Code 11"
            case 0: type = BarcodeLib.TYPE.CODE39; break;                              //"Code 39"

            //case 5: type = BarcodeLib.TYPE.CODE39Extended; break;                  //"Code 39 Extended"
            //case 6: type = BarcodeLib.TYPE.CODE39_Mod43; break;                    //"Code 39 Mod 43"
            //case 7: type = BarcodeLib.TYPE.CODE93; break;                          //"Code 93"
            case 1: type = BarcodeLib.TYPE.CODE128; break;                             //"Code 128"

            //case 9: type = BarcodeLib.TYPE.CODE128A; break;                        //"Code 128-A"
            //case 10: type = BarcodeLib.TYPE.CODE128B; break;                        //"Code 128-B"
            //case 11: type = BarcodeLib.TYPE.CODE128C; break;                        //"Code 128-C"
            //case 12: type = BarcodeLib.TYPE.LOGMARS; break;                         //"LOGMARS"
            //case 13: type = BarcodeLib.TYPE.MSI_Mod10; break;                       //"MSI"
            //case 14: type = BarcodeLib.TYPE.TELEPEN; break;                         //"Telepen"
            ////case 25: type = BarcodeLib.TYPE.FIM; break;                             //"FIM"
            ////case 26: type = BarcodeLib.TYPE.PHARMACODE; break;                      //"Pharmacode"
            default: type = BarcodeLib.TYPE.CODE128; break;                             //"Code 128"
            }//switch

            try
            {
                if (type != BarcodeLib.TYPE.UNSPECIFIED)
                {
                    b.IncludeLabel = true;

                    b.RotateFlipType = rotateFlipType;
                    b.LabelPosition  = labelPosition;
                    if (barcode == null)
                    {
                        barcode = new System.Windows.Forms.GroupBox();
                        //
                        // barcode
                        //
                        barcode.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                        barcode.Dock     = System.Windows.Forms.DockStyle.Fill;
                        barcode.Location = new System.Drawing.Point(0, 0);
                        barcode.Margin   = new System.Windows.Forms.Padding(4, 4, 4, 4);
                        barcode.Name     = "barcode";
                        barcode.Padding  = new System.Windows.Forms.Padding(4, 4, 4, 4);
                        barcode.Size     = new System.Drawing.Size(754, 414);
                        barcode.TabIndex = 36;
                        barcode.TabStop  = false;
                        barcode.Text     = "Barcode Image";
                        barcode.Visible  = false;
                    }

                    //===== Encoding performed here =====
                    barcode.BackgroundImage = b.Encode(type, stringToEncode, System.Drawing.Color.Black, System.Drawing.Color.White, width, height);
                    //===================================
                    //reposition the barcode image to the middle
                    barcode.Location = new System.Drawing.Point((barcode.Location.X + barcode.Width / 2) - barcode.Width / 2, (barcode.Location.Y + barcode.Height / 2) - barcode.Height / 2);

                    if (!string.IsNullOrEmpty(imagePathAndName))
                    {
                        b.SaveImage(imagePathAndName, saveType);
                    }
                } //if
            }     //try
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return(false);
            }//catch

            return(true);
        }