Esempio n. 1
0
        private void btnEncode_Click(object sender, EventArgs e)
        {
            barcodeCreator bc = new barcodeCreator();

            switch (cmbOperation.Text)
            {
            case OPERATION_TYPE_SINGLE:
                BarcodeFormat fm;
                if (barcodeFormat.TryGetValue(cmbBarcodeType.Text, out fm))
                {
                    bitmap = bc.createBarcode(fldBarcodeText.Text, fm, (int)numWidth.Value, (int)numHeight.Value);
                }
                if (!(bitmap == null))
                {
                    picEncode.Image = bitmap;
                }

                break;

            case OPERATION_TYPE_FILE:
                break;
            }
        }
        private void processListFile(String listFilePath,
            String outputFolder, String bcPrefix,
            String bcSuffix,
            int bcHeight,
            int bcWidth,
            BarcodeFormat fm,
            RichTextBox log)
        {
            string input = null;
            bool skip = false;
            Bitmap bm = null;
            UInt64 cnt = 1;
            barcodeCreator bc = new barcodeCreator();
            StringBuilder logB = new StringBuilder();

            if (!File.Exists(listFilePath))
            {
                String message = "File '" + listFilePath + "' does not exist.\nAborting...";

                log.AppendText("!! " + message + Environment.NewLine);
                MessageBox.Show(message, "Could not open file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                StreamReader re = File.OpenText(listFilePath);

                while ((input = re.ReadLine()) != null)
                {

                    switch (cnt)
                    {
                        case 1:
                            if (!(input.Trim().ToLower().Equals(FILE_MARKER.ToLower())))
                            {
                                String message = "File '" + listFilePath + "' does not seem to be a valid barcode list file.\nIf it should be, make sure that the first line is '" + FILE_MARKER + "'";
                                log.AppendText("!! " + message + Environment.NewLine);

                                MessageBox.Show(message, "Invalid list file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                re.Close();
                                return;
                            }
                            break;
                        default:
                            try
                            {
                                if (input.Trim().Length == 0)
                                {
                                    skip = true;
                                }
                                else if (input.Trim().Substring(0, 1).Equals(COMMENT_INDICATOR))
                                {
                                    skip = true;
                                }
                                else
                                {
                                    skip = false;
                                }

                                if (!skip)
                                {
                                    //log.AppendText("^ Parsing line: " + input + Environment.NewLine);
                                    String[] lineArr = input.Split('\t');

                                    switch (lineArr.Length)
                                    {
                                        case 0:
                                            break;
                                        case 2:
                                            StringBuilder sb = new StringBuilder();
                                            sb.Append(bcPrefix.Trim());
                                            sb.Append(lineArr[0]);
                                            sb.Append(bcSuffix.Trim());

                                            log.AppendText("^ Creating barcode: " + sb.ToString() + Environment.NewLine);

                                            bm = bc.createBarcode(sb.ToString(), fm, bcWidth, bcHeight);
                                            if (!(bm == null))
                                            {
                                                string savePath = outputFolder + "/" + lineArr[1];
                                                try
                                                {
                                                    bm.Save(savePath, ImageFormat.Png);
                                                }
                                                catch
                                                {
                                                    String message = "Could not save barcode'" + savePath + "'.\nAborting..";
                                                    log.AppendText("!! " + message + Environment.NewLine);

                                                    MessageBox.Show(message, "Could not save barcode", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                                    re.Close();
                                                    return;
                                                }
                                            }
                                            break;
                                        case 3:
                                            if (lineArr[0].Trim().ToLower().Equals(KEYWORD_OPTION))
                                            {
                                                try
                                                {
                                                    switch (lineArr[1])
                                                    {
                                                        case KEYWORD_WIDTH:
                                                            if (!setOptionInteger(lineArr[2].Trim(), ref bcWidth))
                                                            {
                                                                log.AppendText("! Not a valid integer: " + input + Environment.NewLine);
                                                            }
                                                            log.AppendText("^ BC Width=" + bcWidth + Environment.NewLine);
                                                            break;
                                                        case KEYWORD_HEIGHT:
                                                            if (!setOptionInteger(lineArr[2].Trim(), ref bcHeight))
                                                            {
                                                                log.AppendText("! Not a valid integer: " + input + Environment.NewLine);
                                                            }
                                                            log.AppendText("^ BC Height=" + bcHeight + Environment.NewLine);
                                                            break;
                                                        case KEYWORD_PREFIX:
                                                            bcPrefix = lineArr[2];
                                                            log.AppendText("^ BC Prefix: '" + bcPrefix + "'" + Environment.NewLine);
                                                            break;
                                                        case KEYWORD_SUFFIX:
                                                            bcSuffix = lineArr[2];
                                                            log.AppendText("^ BC Suffix: '" + bcSuffix + "'" + Environment.NewLine);
                                                            break;
                                                        default:
                                                            log.AppendText("^ Unknown option: " + input + Environment.NewLine);
                                                            break;
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    log.AppendText("!! Error parsing option. Line: '" + input + "' Error: " + e.Message + Environment.NewLine);
                                                }
                                            }
                                            break;
                                        default:
                                            log.AppendText("^ Could not process line: " + input + Environment.NewLine);
                                            break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                String message = "There was an error while reading the file.\nError: " + e.Message + "\nAborting..";
                                log.AppendText("!! " + message + Environment.NewLine);

                                MessageBox.Show(message, "Error reading file.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                re.Close();
                                return;
                            }
                            break;
                    }
                    cnt += 1;
                }

                re.Close();

            }
            catch (Exception e)
            {
                MessageBox.Show("An error was encountered while opening file '" + listFilePath + "'.\n Error: " + e.Message, "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        private void btnEncode_Click(object sender, EventArgs e)
        {
            barcodeCreator bc = new barcodeCreator();

            switch (cmbOperation.Text)
            {
                case OPERATION_TYPE_SINGLE:
                    BarcodeFormat fm;
                    if (barcodeFormat.TryGetValue(cmbBarcodeType.Text, out fm))
                    {
                        bitmap = bc.createBarcode(fldBarcodeText.Text, fm, (int)numWidth.Value, (int)numHeight.Value);
                    }
                    if (!(bitmap == null))
                    {
                        picEncode.Image = bitmap;
                    }

                    break;
                case OPERATION_TYPE_FILE:
                    break;
            }
        }
Esempio n. 4
0
        private void processListFile(String listFilePath,
                                     String outputFolder, String bcPrefix,
                                     String bcSuffix,
                                     int bcHeight,
                                     int bcWidth,
                                     BarcodeFormat fm,
                                     RichTextBox log)
        {
            string         input = null;
            bool           skip  = false;
            Bitmap         bm    = null;
            UInt64         cnt   = 1;
            barcodeCreator bc    = new barcodeCreator();
            StringBuilder  logB  = new StringBuilder();



            if (!File.Exists(listFilePath))
            {
                String message = "File '" + listFilePath + "' does not exist.\nAborting...";

                log.AppendText("!! " + message + Environment.NewLine);
                MessageBox.Show(message, "Could not open file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                StreamReader re = File.OpenText(listFilePath);

                while ((input = re.ReadLine()) != null)
                {
                    switch (cnt)
                    {
                    case 1:
                        if (!(input.Trim().ToLower().Equals(FILE_MARKER.ToLower())))
                        {
                            String message = "File '" + listFilePath + "' does not seem to be a valid barcode list file.\nIf it should be, make sure that the first line is '" + FILE_MARKER + "'";
                            log.AppendText("!! " + message + Environment.NewLine);

                            MessageBox.Show(message, "Invalid list file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            re.Close();
                            return;
                        }
                        break;

                    default:
                        try
                        {
                            if (input.Trim().Length == 0)
                            {
                                skip = true;
                            }
                            else if (input.Trim().Substring(0, 1).Equals(COMMENT_INDICATOR))
                            {
                                skip = true;
                            }
                            else
                            {
                                skip = false;
                            }

                            if (!skip)
                            {
                                //log.AppendText("^ Parsing line: " + input + Environment.NewLine);
                                String[] lineArr = input.Split('\t');

                                switch (lineArr.Length)
                                {
                                case 0:
                                    break;

                                case 2:
                                    StringBuilder sb = new StringBuilder();
                                    sb.Append(bcPrefix.Trim());
                                    sb.Append(lineArr[0]);
                                    sb.Append(bcSuffix.Trim());

                                    log.AppendText("^ Creating barcode: " + sb.ToString() + Environment.NewLine);

                                    bm = bc.createBarcode(sb.ToString(), fm, bcWidth, bcHeight);
                                    if (!(bm == null))
                                    {
                                        string savePath = outputFolder + "/" + lineArr[1];
                                        try
                                        {
                                            bm.Save(savePath, ImageFormat.Png);
                                        }
                                        catch
                                        {
                                            String message = "Could not save barcode'" + savePath + "'.\nAborting..";
                                            log.AppendText("!! " + message + Environment.NewLine);

                                            MessageBox.Show(message, "Could not save barcode", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            re.Close();
                                            return;
                                        }
                                    }
                                    break;

                                case 3:
                                    if (lineArr[0].Trim().ToLower().Equals(KEYWORD_OPTION))
                                    {
                                        try
                                        {
                                            switch (lineArr[1])
                                            {
                                            case KEYWORD_WIDTH:
                                                if (!setOptionInteger(lineArr[2].Trim(), ref bcWidth))
                                                {
                                                    log.AppendText("! Not a valid integer: " + input + Environment.NewLine);
                                                }
                                                log.AppendText("^ BC Width=" + bcWidth + Environment.NewLine);
                                                break;

                                            case KEYWORD_HEIGHT:
                                                if (!setOptionInteger(lineArr[2].Trim(), ref bcHeight))
                                                {
                                                    log.AppendText("! Not a valid integer: " + input + Environment.NewLine);
                                                }
                                                log.AppendText("^ BC Height=" + bcHeight + Environment.NewLine);
                                                break;

                                            case KEYWORD_PREFIX:
                                                bcPrefix = lineArr[2];
                                                log.AppendText("^ BC Prefix: '" + bcPrefix + "'" + Environment.NewLine);
                                                break;

                                            case KEYWORD_SUFFIX:
                                                bcSuffix = lineArr[2];
                                                log.AppendText("^ BC Suffix: '" + bcSuffix + "'" + Environment.NewLine);
                                                break;

                                            default:
                                                log.AppendText("^ Unknown option: " + input + Environment.NewLine);
                                                break;
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            log.AppendText("!! Error parsing option. Line: '" + input + "' Error: " + e.Message + Environment.NewLine);
                                        }
                                    }
                                    break;

                                default:
                                    log.AppendText("^ Could not process line: " + input + Environment.NewLine);
                                    break;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            String message = "There was an error while reading the file.\nError: " + e.Message + "\nAborting..";
                            log.AppendText("!! " + message + Environment.NewLine);

                            MessageBox.Show(message, "Error reading file.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            re.Close();
                            return;
                        }
                        break;
                    }
                    cnt += 1;
                }

                re.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("An error was encountered while opening file '" + listFilePath + "'.\n Error: " + e.Message, "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }