// OK button pressed
        private void OnOK(object sender, EventArgs e)
        {
            switch (CheckedField)
            {
            case 0:
                if (!double.TryParse(WidthHeightTextBox.Text.Trim(), out double WidthHeight) ||
                    WidthHeight < 0.1 || WidthHeight > 10.0 || !Encoder.WidthToHeightRatio(WidthHeight))
                {
                    MessageBox.Show("Invalid width to height ratio");
                    return;
                }
                break;

            case 1:
                if (!int.TryParse(DataRowsTextBox.Text.Trim(), out int DataRows) || !Encoder.SetDataRows(DataRows))
                {
                    MessageBox.Show("Invalid number of data rows");
                    return;
                }
                break;

            case 2:
                if (!int.TryParse(DataColumnsTextBox.Text.Trim(), out int DataColumns) || !Encoder.SetDataColumns(DataColumns))
                {
                    MessageBox.Show("Invalid number of data columns");
                    return;
                }
                break;
            }

            DialogResult = DialogResult.OK;
            return;
        }
Ejemplo n.º 2
0
        public static void Encode
        (
            string[] Args
        )
        {
            // help
            if (Args == null || Args.Length < 2)
            {
                throw new ArgumentException("help");
            }

            int    DataColumns    = 0;
            int    DataRows       = 0;
            double Ratio          = 0;
            bool   TextFile       = false;
            string CharacterSet   = null;
            string InputFileName  = null;
            string OutputFileName = null;
            string Code;
            string Value;

            Pdf417Encoder Encoder = new Pdf417Encoder();

            for (int ArgPtr = 0; ArgPtr < Args.Length; ArgPtr++)
            {
                string Arg = Args[ArgPtr];

                // file name
                if (Arg[0] != '/' && Arg[0] != '-')
                {
                    if (InputFileName == null)
                    {
                        InputFileName = Arg;
                        continue;
                    }
                    if (OutputFileName == null)
                    {
                        OutputFileName = Arg;
                        continue;
                    }
                    throw new ArgumentException(string.Format("Invalid option. Argument={0}", ArgPtr + 1));
                }

                // search for colon
                int Ptr = Arg.IndexOf(':');
                if (Ptr < 0)
                {
                    Ptr = Arg.IndexOf('=');
                }
                if (Ptr > 0)
                {
                    Code  = Arg.Substring(1, Ptr - 1);
                    Value = Arg.Substring(Ptr + 1);
                }
                else
                {
                    Code  = Arg.Substring(1);
                    Value = string.Empty;
                }

                Code  = Code.ToLower();
                Value = Value.ToLower();

                switch (Code)
                {
                case "encode":
                case "n":
                    EncodingControl EC;
                    switch (Value)
                    {
                    case "auto":
                    case "a":
                        EC = EncodingControl.Auto;
                        break;

                    case "byte":
                    case "b":
                        EC = EncodingControl.ByteOnly;
                        break;

                    case "text":
                    case "t":
                        EC = EncodingControl.TextAndByte;
                        break;

                    default:
                        throw new ArgumentException("Encoding control option in error");
                    }
                    Encoder.EncodingControl = EC;
                    break;

                case "error":
                case "e":
                    ErrorCorrectionLevel ECL;
                    switch (Value)
                    {
                    case "0":
                        ECL = ErrorCorrectionLevel.Level_0;
                        break;

                    case "1":
                        ECL = ErrorCorrectionLevel.Level_1;
                        break;

                    case "2":
                        ECL = ErrorCorrectionLevel.Level_2;
                        break;

                    case "3":
                        ECL = ErrorCorrectionLevel.Level_3;
                        break;

                    case "4":
                        ECL = ErrorCorrectionLevel.Level_4;
                        break;

                    case "5":
                        ECL = ErrorCorrectionLevel.Level_5;
                        break;

                    case "6":
                        ECL = ErrorCorrectionLevel.Level_6;
                        break;

                    case "7":
                        ECL = ErrorCorrectionLevel.Level_7;
                        break;

                    case "8":
                        ECL = ErrorCorrectionLevel.Level_8;
                        break;

                    case "low":
                    case "l":
                        ECL = ErrorCorrectionLevel.AutoLow;
                        break;

                    case "normal":
                    case "n":
                        ECL = ErrorCorrectionLevel.AutoNormal;
                        break;

                    case "medium":
                    case "m":
                        ECL = ErrorCorrectionLevel.AutoMedium;
                        break;

                    case "high":
                    case "h":
                        ECL = ErrorCorrectionLevel.AutoHigh;
                        break;

                    default:
                        throw new ArgumentException("Error correction level option in error");
                    }
                    Encoder.ErrorCorrection = ECL;
                    break;

                case "width":
                case "w":
                    if (!int.TryParse(Value, out int BarWidth))
                    {
                        BarWidth = -1;
                    }
                    Encoder.NarrowBarWidth = BarWidth;
                    break;

                case "height":
                case "h":
                    if (!int.TryParse(Value, out int RowHeight))
                    {
                        RowHeight = -1;
                    }
                    Encoder.RowHeight = RowHeight;
                    break;

                case "quiet":
                case "q":
                    if (!int.TryParse(Value, out int QuietZone))
                    {
                        QuietZone = -1;
                    }
                    Encoder.QuietZone = QuietZone;
                    break;

                case "col":
                case "c":
                    if (DataRows != 0 || Ratio != 0)
                    {
                        throw new ArgumentException("Only one value is allowed for Data Rows, Data Columns and Ratio");
                    }
                    if (!int.TryParse(Value, out DataColumns) || DataColumns < 1 || DataColumns > 30)
                    {
                        throw new ApplicationException("Data columns in error");
                    }
                    break;

                case "row":
                case "r":
                    if (DataColumns != 0 || Ratio != 0)
                    {
                        throw new ArgumentException("Only one value is allowed for Data Rows, Data Columns and Ratio");
                    }
                    if (!int.TryParse(Value, out DataRows) || DataRows < 3 || DataRows > 90)
                    {
                        throw new ApplicationException("Data Rows in error");
                    }
                    break;

                case "ratio":
                case "o":
                    if (DataRows != 0 || DataColumns != 0)
                    {
                        throw new ArgumentException("Only one value is allowed for Data Rows, Data Columns and Ratio");
                    }
                    if (!double.TryParse(Value, out Ratio) || Ratio < 0.1 || Ratio > 100)
                    {
                        throw new ApplicationException("Image aspect ratio (width/height) in error");
                    }
                    break;

                case "text":
                case "t":
                    TextFile = true;
                    if (Value != string.Empty)
                    {
                        CharacterSet = Value;
                    }
                    break;

                default:
                    throw new ApplicationException(string.Format("Invalid argument no {0}, code {1}", ArgPtr + 1, Code));
                }
            }

            if (TextFile)
            {
                string InputText = File.ReadAllText(InputFileName);
                Encoder.Encode(InputText);
            }
            else
            {
                byte[] InputBytes = File.ReadAllBytes(InputFileName);
                Encoder.Encode(InputBytes);
            }

            if (DataColumns != 0)
            {
                if (!Encoder.SetDataColumns(DataColumns))
                {
                    throw new ApplicationException("Set data columns failed");
                }
            }
            else if (DataRows != 0)
            {
                if (!Encoder.SetDataRows(DataRows))
                {
                    throw new ApplicationException("Set data rows failed");
                }
            }
            else if (Ratio != 0)
            {
                if (!Encoder.WidthToHeightRatio(Ratio))
                {
                    throw new ApplicationException("Set width to height aspect ratio failed");
                }
            }

            Encoder.SaveBarcodeToPngFile(OutputFileName);
            return;
        }