Example #1
0
        //-------------------------------------------------------------------------
        public void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                int x = (int)Math.Round(e.X / Zoom);
                int y = (int)Math.Round(e.Y / Zoom);
                Point begPointZoom = new Point((int)Math.Round(begPoint.X / Zoom), (int)Math.Round(begPoint.Y / Zoom));
                if (rec != null && rec.Bitmap != null)
                {
                    if (rbtnCut.Checked)
                    {
                        if (!appSettings.Fields.NotConfirm)
                        {
                            if (MessageBox.Show("Cut selected area?"
                                 , Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes
                                 )
                            {
                                return;
                            }
                        }
                        RecognizeBubblesButton.Enabled = false;
                        Bitmap bmp = new Bitmap(areaNaturalSize.Width, areaNaturalSize.Height, PixelFormat.Format24bppRgb);
                        bmp = recTools.CopyBitmap(rec.Bitmap, areaNaturalSize);
                        //bmp.Save("Cut.bmp", ImageFormat.Bmp);
                        //if (bmp.Width > bmp.Height)
                        //{
                        //    bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        //}
                        rec.Bitmap = (Bitmap)bmp.Clone();
                        bmp.Dispose();
                        ShowImage(true);
                        isCut = true;
                        //isRotate = true;
                        if (appSettings.Fields.RecAfterCut)
                        {
                            ShetIdManualySet = false;
                            RecognizeAllButton.PerformClick();
                        }
                        return;
                    }
                    else if (rbtnClear.Checked)
                    {
                        for (int x1 = areaNaturalSize.X; x1 < areaNaturalSize.Right; x1++)
                        {
                            for (int y1 = areaNaturalSize.Y; y1 < areaNaturalSize.Bottom; y1++)
                            {
                                rec.Bitmap.SetPixel(x1, y1, Color.FromArgb(255, 255, 255));
                            }
                        }

                        //rec.Bitmap.Save("recClear.bmp", ImageFormat.Bmp);
                        isClear = true;
                        ShowImage(true);
                        return;
                    }
                    if (SelectedBarCodeItem != null)
                    {
                        if (areaNaturalSize.Width > pictureBox1.Image.Width / 3 || areaNaturalSize.Height > pictureBox1.Image.Height / 3)
                        {
                            messageBoxShow = true;
                            MessageBox.Show("Select more precisely the area of recognition"
                                , Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            return;
                        }
                        bool notConfident = false;
                        string barCodesPrompt = "";
                        string barcodeMem = "";
                        eDoctrinaUtils.Region region = new eDoctrinaUtils.Region();

                        foreach (var item in rec.regionsList)
                        {
                            if (item.SheetIdentifierName == rec.SheetIdentifier)
                            {
                                foreach (var itm in item.regions)
                                {
                                    if (itm.name == SelectedBarCodeItem.Text)
                                    {
                                        region = itm;
                                        break;
                                    }
                                }
                                if (!region.Equals(new eDoctrinaUtils.Region()))
                                    break;
                            }
                        }
                        string barcode = "";
                        if (SelectedBarCodeItem.radioButton1.Checked)
                        {
                            barcode = recTools.GetBarCode
                           (
                             rec.Bitmap
                           , ref notConfident
                           , ref barCodesPrompt
                           , ref rec.filterType
                           , ref barcodeMem
                           , areaNaturalSize.X
                           , areaNaturalSize.X + areaNaturalSize.Width
                           , areaNaturalSize.Y
                           , areaNaturalSize.Y + areaNaturalSize.Height
                           , rec.kx
                           , rec.ky
                           , curRect
                           , etRect
                           , deltaY
                           , region
                           , true
                           , region.percent_confident_text_region
                           , rec.defaults.PercentConfidentText
                           , rec.defaults.FontName
                           , ref currentBarCodeRectangle
                           , ref lastSymbolRectangle
                           , false
                           , true
                           );
                        }
                        else
                        {
                            Rectangle r2 = Rectangle.Empty;
                            Bitmap bmp = recTools.CopyBitmap(rec.Bitmap, areaNaturalSize);
                            if (rec.regions != null && rec.regions.heightAndWidthRatio < 1 && region.rotate == 90)
                                bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                            recTools.TextRecognizeExt
                               (
                                 rec.filterType
                               , region.percent_confident_text_region
                               , rec.DarknessPercent//percent_confident_text
                               , defaults.FontName
                               , ref barcode
                               , ref lastSymbolRectangle
                               , ref region.areas[1]
                               , false//filter
                               , ref r2
                               , ref bmp
                               , ""
                               , 0
                               , null
                               , true
                               );
                        }

                        if (!string.IsNullOrEmpty(barcode))
                        {
                            SelectedBarCodeItem.comboBox1.Text = barcode;
                            SelectedBarCodeItem.textChanged = true;
                            if (SelectedBarCodeItem.Name == "amout_of_questions")
                            {
                                if (linsForm == null)
                                    CreateLinsForm();

                                int amout_of_questions = -1;
                                try
                                {
                                    amout_of_questions = Convert.ToInt32(barcode);
                                }
                                catch (Exception)
                                {
                                }
                                SetAmoutOfQuestions(amout_of_questions);
                                RecognizeAllButton.Enabled = true;
                                RecognizeBubblesButton.Enabled = true;
                                SelectedBarCodeItem.btnCheck.PerformClick();
                            }
                            SelectedBarCodeItem.textChanged = true;
                            //SelectedBarCodeItem.btnCheck.Focus();
                            SelectedBarCodeItem.BarCodeListItemControl_Leave(SelectedBarCodeItem, e);
                            pictureBox1.Cursor = Cursors.Default;
                        }
                        else
                        {
                            if (areaNaturalSize.Width * areaNaturalSize.Height < 100)
                                return;
                            if (SelectedBarCodeItem.radioButton1.Checked)
                                MessageBox.Show("Bar code is not recognized", Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            else
                                MessageBox.Show("Text is not recognized", Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                        return;
                    }

                    if (rbtnRotate.Checked)
                    {
                        if (Math.Abs(x - begPointZoom.X) + Math.Abs(y - begPointZoom.Y) < 200)
                            return;
                        double angle = 0;
                        if (Math.Abs(begPointZoom.X - x) > Math.Abs(begPointZoom.Y - y))
                        {
                            if (begPointZoom.X < x)
                                angle = -180 + recTools.GetAngle2(begPointZoom, new Point(x, y));
                            else
                                angle = -180 + recTools.GetAngle2(new Point(x, y), begPointZoom);
                        }
                        else
                        {
                            if (begPointZoom.Y < y)
                                angle = -90 + recTools.GetAngle2(begPointZoom, new Point(x, y));
                            else
                                angle = -90 + recTools.GetAngle2(new Point(x, y), begPointZoom);
                        }
                        Bitmap b1 = new Bitmap(rec.Bitmap.Width, rec.Bitmap.Height, PixelFormat.Format24bppRgb);
                        Graphics g2 = Graphics.FromImage(b1);
                        Color argbWhite = Color.FromArgb(255, 255, 255);
                        g2.Clear(argbWhite);
                        g2.InterpolationMode = InterpolationMode.NearestNeighbor;
                        using (Matrix m2 = new Matrix())
                        {
                            m2.RotateAt((float)angle, new System.Drawing.Point(rec.Bitmap.Width / 2, rec.Bitmap.Height / 2));
                            g2.Transform = m2;
                            try
                            {
                                g2.DrawImageUnscaledAndClipped(rec.Bitmap, new Rectangle(0, 0, rec.Bitmap.Width, rec.Bitmap.Height));
                            }
                            catch { }
                            g2.ResetTransform();
                        }
                        rec.Bitmap = (Bitmap)b1.Clone();

                        //bmp.Save("Alignment.bmp", ImageFormat.Bmp);

                        b1.Dispose();
                        g2.Dispose();
                        pictureBox1.Image = (Bitmap)rec.Bitmap.Clone();//new Bitmap(rec.Bitmap);
                        //SizeFitButton.PerformClick();
                        Refresh();
                        isRotate = true;
                        if (appSettings.Fields.RecAfterCut)
                        {
                            if (appSettings.Fields.RecAfterCut)// && rec.BarCodesPrompt == ""
                            {
                                if (BarCodeItems != null && BarCodeItems.Count > 0)
                                {
                                    bool verify = true;
                                    for (int j = 0; j < BarCodeItems.Count; j++)
                                    {
                                        BarCodeItem b = BarCodeItems[j] as BarCodeItem;
                                        if (!b.Verify)
                                        {
                                            verify = false;
                                            break;
                                        }
                                    }
                                    if (verify)
                                    {
                                        RecognizeBubblesButton.PerformClick();
                                    }
                                    else
                                        RecognizeAllButton.PerformClick();
                                }
                            }
                        }
                        return;
                    }
                    else if (rbtnGrid.Checked)//linsForm.rbtnGrid.Checked
                    {
                        if (linsForm == null || linsForm.IsDisposed)
                            CreateLinsForm();
                        if (rec.SheetIdentifier != "FANDP")
                            linsForm.Visible = true;// linsForm.Show();
                                                    //linsFormIsVisible = true;
                        if (areaNaturalSize.Left < 0)
                        {
                            areaNaturalSize.Width -= areaNaturalSize.Left;
                            areaNaturalSize.X = 0;
                        }
                        if (areaNaturalSize.Top < 0)
                        {
                            areaNaturalSize.Height -= areaNaturalSize.Y;
                            areaNaturalSize.Y = 0;
                        }
                        if (areaNaturalSize.Left + areaNaturalSize.Width > rec.Bitmap.Width)
                            areaNaturalSize.Width = rec.Bitmap.Width - areaNaturalSize.Left;

                        if (areaNaturalSize.Width * areaNaturalSize.Height < 500)
                        {
                            DrawAllAreasNaturalSize();
                            return;
                        }
                        if (etalonAreas.Length < 3)
                        {
                            if (areaNaturalSize.X < pictureBox1.Image.Width / 4)//+ areaNaturalSize.Width / 2
                            {//пока только для двух полей!!!
                                linsForm.nudArea.Value = 1;
                            }
                            else
                            {
                                if (rec.AmoutOfQuestions != 0 && rec.AmoutOfQuestions <= factLinesPerArea[0])
                                {
                                    if (factLinesPerArea.Length > 1)
                                    {
                                        factLinesPerArea[1] = 0;
                                        linsForm.nudArea.Value = 2;
                                        Application.DoEvents();
                                        linsForm.nudRows.Value = 0;
                                        if (MessageBox.Show("Are you sure you want to select the first area here?"
                                        , Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                        {
                                            linsForm.nudArea.Value = 1;
                                        }
                                        else
                                        {
                                            pictureBox1.Refresh();
                                            return;
                                        }
                                    }
                                }
                                else
                                    linsForm.nudArea.Value = 2;
                            }
                        }
                        factAreas[(int)linsForm.nudArea.Value - 1].left = areaNaturalSize.Left;// (int)(area.Left / Zoom);
                        factAreas[(int)linsForm.nudArea.Value - 1].top = areaNaturalSize.Top;// (int)(area.Top / Zoom);
                        factAreas[(int)linsForm.nudArea.Value - 1].height = areaNaturalSize.Height;// (int)(area.Height / Zoom);
                        factAreas[(int)linsForm.nudArea.Value - 1].width = areaNaturalSize.Width;// (int)(area.Width / Zoom);
                        allAreasNaturalSize[(int)linsForm.nudArea.Value - 1] = areaNaturalSize;
                        factAreasManualSet[(int)linsForm.nudArea.Value - 1] = true;
                        double kx = 1;
                        double ky = 1;
                        int deltaX = 0, deltaY = 0;
                        int manualSelectedArea = -1;
                        int questions = 0;
                        for (int i = 0; i < factAreas.Length; i++)
                        {
                            if (!factAreasManualSet[i])
                                continue;
                            else
                            {
                                double EtStepX = (double)
                                (etalonAreas[i].width - etalonAreas[i].bubble.Width
                                * bubblesRegions.areas[i].bubblesPerLine)
                                / (bubblesRegions.areas[i].bubblesPerLine - 1);
                                int widthEt = (int)(bubblesRegions.areas[i].bubble.Width
                                    * factAreas[i].bubblesPerLine + EtStepX * (factAreas[i].bubblesPerLine - 1));//(int)linsForm.nudCols.Value - 1);
                                kx = (double)factAreas[i].width / widthEt;
                                rec.kx = (decimal)kx;
                                int maxBubblesCountOfArea = factLinesPerArea[i];
                                //(int)Math.Round((double)bubblesRegions.areas[i].height / bubblesRegions.areas[i].lineHeight);
                                double EtStepY = (double)(bubblesRegions.areas[i].height
                                 - bubblesRegions.areas[i].bubble.Height * maxBubblesCountOfArea) / (maxBubblesCountOfArea - 1);
                                double heightEt = bubblesRegions.areas[i].bubble.Height * factLinesPerArea[i]
                                   + EtStepY * (factLinesPerArea[i] - 1)
                                   + (bubblesRegions.areas[i].subLineHeight * (factAreas[i].subLinesAmount));
                                ky = (double)factAreas[i].height / heightEt;
                                rec.ky = (decimal)ky;
                                deltaX = (int)Math.Round(factAreas[i].left * kx) - etalonAreas[i].left;
                                deltaY = (int)Math.Round(factAreas[i].top * ky) - etalonAreas[i].top;
                                curRect = new Rectangle(bubblesRegions.areas[i].left, bubblesRegions.areas[i].top, bubblesRegions.areas[i].width, bubblesRegions.areas[i].height);
                                etRect = new Rectangle(factAreas[i].left, factAreas[i].top, factAreas[i].width, factAreas[i].height);
                                manualSelectedArea = i;// break;
                            }
                        }
                        if (manualSelectedArea > -1)
                        {
                            for (int i = 0; i < factAreas.Length; i++)
                            {
                                if (factLinesPerArea[i] == 0)
                                    continue;
                                if (factAreasManualSet[i])
                                {
                                    questions += factLinesPerArea[i];
                                    continue;
                                }
                                if (factAreas[i].bubblesPerLine == 0)
                                    factAreas[i].bubblesPerLine = etalonAreas[i].bubblesPerLine;
                                if (rec.AmoutOfQuestions != 0 && questions >= rec.AmoutOfQuestions)
                                {
                                    allAreasNaturalSize[i] = Rectangle.Empty;
                                    break;
                                }
                                questions += factLinesPerArea[i];
                                if (factLinesPerArea[i] == 0)
                                {
                                    int amout_of_questions = rec.AmoutOfQuestions;
                                    for (int k = 0; k < rec.linesPerArea.Length; k++)
                                    {
                                        if (amout_of_questions > 0)
                                        {
                                            if (amout_of_questions <= rec.linesPerArea[k])
                                            {
                                                factLinesPerArea[k] = amout_of_questions;
                                                break;
                                            }
                                            else
                                            {
                                                factLinesPerArea[k] = rec.linesPerArea[k];
                                                amout_of_questions -= factLinesPerArea[k];
                                            }
                                        }
                                        else
                                            factLinesPerArea[k] = rec.linesPerArea[k];
                                    }
                                }
                                double EtStepX = (double)
                                     (etalonAreas[i].width - etalonAreas[i].bubble.Width
                                     * bubblesRegions.areas[i].bubblesPerLine)
                                     / (bubblesRegions.areas[i].bubblesPerLine - 1);
                                int widthEt = (int)(bubblesRegions.areas[i].bubble.Width
                                    * factAreas[i].bubblesPerLine + EtStepX * factAreas[i].bubblesPerLine - 1);//(int)linsForm.nudCols.Value - 1);
                                int manualNearestArea = 0;
                                int delta = int.MaxValue;
                                for (int k = 0; k < factAreasManualSet.Length; k++)
                                {
                                    if (!factAreasManualSet[k] || k == i)
                                        continue;
                                    int d = Math.Abs(etalonAreas[i].left - etalonAreas[k].left);
                                    if (d < delta)
                                    {
                                        delta = d;
                                        manualNearestArea = k;
                                    }
                                }
                                int maxBubblesCountOfArea = rec.linesPerArea[0];//кроме "FANDP"!!!
                                //int maxBubblesCountOfArea = (int)Math.Round((double)etalonAreas[i].height /etalonAreas[i].lineHeight);
                                //double EtStepY = (double)(bubblesRegions.areas[i].height
                                //- bubblesRegions.areas[i].bubble.Height * rec.linesPerArea[i]) / (rec.linesPerArea[i] - 1);
                                double EtStepY = (double)(bubblesRegions.areas[i].height
                                - bubblesRegions.areas[i].bubble.Height * maxBubblesCountOfArea) / (maxBubblesCountOfArea - 1);
                                double heightEt = bubblesRegions.areas[i].bubble.Height * factLinesPerArea[i]
                                   + EtStepY * (factLinesPerArea[i] - 1)
                                   + (etalonAreas[i].subLineHeight
                                   * (factAreas[i].subLinesAmount));//bubblesRegions.areas[i].subLinesAmount -
                                Rectangle r = Rectangle.Empty;
                                if (etalonAreas[manualSelectedArea].left == etalonAreas[i].left)
                                    r.X = factAreas[i].left;
                                else
                                    //r.X = deltaX + (int)Math.Round((decimal)(etalonAreas[i].left * kx));//-deltaX +
                                    r.X = factAreas[0].left + (int)Math.Round((double)(etalonAreas[i].left - etalonAreas[manualNearestArea].left) * kx);
                                if (etalonAreas[manualSelectedArea].bubble.Y == etalonAreas[i].bubble.Y)
                                    r.Y = factAreas[manualSelectedArea].top;
                                else
                                    //r.Y = bubblesRegions.areas[i].top + (int)Math.Round((decimal)(deltaY));//* ky
                                    //r.Y = deltaY + (int)Math.Round((decimal)(etalonAreas[i].bubble.Y * ky));
                                    r.Y = factAreas[0].top + (int)Math.Round((double)(etalonAreas[i].top - etalonAreas[manualNearestArea].top) * ky);
                                r.Width = (int)(Math.Round(widthEt * kx));//deltaX +
                                int diff = factAreas[i].subLinesAmount - factAreas[manualNearestArea].subLinesAmount;
                                //int lineStep = (int)(Math.Round((double)(etalonAreas[manualNearestArea].lineHeight * ky)));
                                //r.Height = diff * (int)Math.Round((double)(etalonAreas[i].subLineHeight * ky))//factAreas[i].subLineHeight
                                ////+ lineStep
                                //+ ((int)(Math.Round(((double)(factAreas[manualNearestArea].height)//- ((int)(Math.Round((double)(EtStepY * ky))))
                                //* factLinesPerArea[i]) / factLinesPerArea[manualNearestArea])))
                                ////- lineStep/
                                //;
                                //int diff = factAreas[i].subLinesAmount - factAreas[manualNearestArea].subLinesAmount;
                                //r.Height = diff * (int)Math.Round((double)(etalonAreas[i].subLineHeight * ky))//factAreas[i].subLineHeight
                                //   - (maxBubblesCountOfArea - diff) * (int)Math.Round((double)(etalonAreas[i].lineHeight * ky))
                                //    + ((int)(Math.Round(((double)(factAreas[manualNearestArea].height)//- ((int)(Math.Round((double)(EtStepY * ky))))
                                //    * factLinesPerArea[i]) / factLinesPerArea[manualNearestArea])));
                                //r.Width = (int)(Math.Round(widthEt * kx));//deltaX +
                                r.Height = (int)(Math.Round(heightEt * ky));
                                if (r.X < 0)
                                    r.X = 0;
                                if (r.Y < 0)
                                    r.Y = 0;
                                //if (r.X<=)
                                //{

                                //}
                                if (r.Right > pictureBox1.Image.Width)
                                    r.Width = pictureBox1.Image.Width - r.X;
                                if (r.Bottom > pictureBox1.Image.Height)
                                    r.Height = pictureBox1.Image.Height - r.Y;
                                if (i == 0)
                                {
                                    if (etalonAreas[i].bubble.X < etalonAreas[i + 1].bubble.X)
                                    {
                                        if (r.Right >= etalonAreas[i + 1].bubble.X)
                                        {
                                            MessageBox.Show
                                                ("Please check that you have the correct number of columns"
                                                , Text, MessageBoxButtons.OK
                                                , MessageBoxIcon.Exclamation
                                                );
                                            allAreasNaturalSize[i] = Rectangle.Empty;
                                        }
                                        else//рисовать автоматически вычисленную
                                             if (linsForm.chbBuildAllAreas.Checked)
                                            allAreasNaturalSize[i] = r;
                                    }
                                }
                                else
                                {
                                    if (r.Width <= 0 || r.X < allAreasNaturalSize[0].Right)
                                    {
                                        MessageBox.Show
                                               ("Please check that you have the correct number of columns"
                                               , Text, MessageBoxButtons.OK
                                               , MessageBoxIcon.Exclamation
                                               );
                                        allAreasNaturalSize[i] = Rectangle.Empty;
                                    }
                                    else//рисовать автоматически вычисленную
                                        if (linsForm.chbBuildAllAreas.Checked)
                                        allAreasNaturalSize[i] = r;
                                }
                            }
                            DrawAllAreasNaturalSize();
                        }
                    }
                }
                if (animatedTimer != null)
                    animatedTimer.StartAnimation();
            }
            catch (Exception)
            {
            }
        }
Example #2
0
        //-------------------------------------------------------------------------
        private void InitLinsForm(bool BarCodeControlsBuilding)
        {
            Application.DoEvents();
            int qn = 0;
            try
            {
                RecognizeAllButton.Enabled = true;
                CreateEtalonAreas();
                if (BarCodeControlsBuilding)// && (rec.areas == null || rec.areas.Length == 0))
                {
                    rec.areas = (RegionsArea[])etalonAreas.Clone();
                }
                if (linsForm != null && appSettings.Fields.LinsTrackBar1Value != 0)
                    linsForm.trackBar1.Value = appSettings.Fields.LinsTrackBar1Value;
                UpdateZoomedImage(0, 0);
                //regions = recTools.GetRegions(rec.SheetIdentifier, rec.regionsList);
                int amout_of_questions = -1;
                if (rec.regions == null)
                {
                    if (appSettings.Fields.ChbSheetId && !string.IsNullOrEmpty(lastSheetId))
                    {
                        rec.regions = recTools.GetRegions(lastSheetId, rec.regionsList);
                    }
                }
                for (int i = 0; i < rec.regions.regions.Length; i++)
                {
                    var item = rec.regions.regions[i];
                    if (item.value == rec.SheetIdentifier)
                        continue;
                    if (item.type.StartsWith("bar"))
                    {
                        if (item.name == "sheetIdentifier")
                            continue;
                        int j = Array.IndexOf(rec.allBarCodeNames, item.name);
                        if (j >= 0)
                        {
                            BarCodeListItemControl b = barCodeList.ControlList[j] as BarCodeListItemControl;
                            switch (item.name)
                            {
                                case "district_id":
                                    if (string.IsNullOrEmpty(b.comboBox1.Text))//item.value == null
                                    {
                                        if (appSettings.Fields.DistrictId && lastDistrictId != "")
                                        {
                                            rec.allBarCodeValues[j] = lastDistrictId;
                                            b.comboBox1.Text = lastDistrictId;
                                            item.value = lastDistrictId;
                                            b.comboBox1.ForeColor = Color.Red;
                                        }
                                    }
                                    break;
                                case "amout_of_questions":
                                    if (rec != null && rec.SheetIdentifier == "FLEX")
                                    {
                                        BarCodeListItemControl b2 = barCodeList.ControlList["bubbles_per_line"] as BarCodeListItemControl;
                                        if (string.IsNullOrEmpty(b2.comboBox1.Text) || !utils.IsNumeric(b2.comboBox1.Text))
                                        {
                                            b.comboBox1.Text = "";
                                            MessageBox.Show("You must specify bubbles_per_line."
                                                , Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);

                                            return;
                                        }
                                        rep:
                                        for (int k = 0; k < BarCodeItems.Count; k++)
                                        {
                                            var itm = BarCodeItems[k];
                                            //}
                                            //foreach (var item in BarCodeItems)//barCodeList.ControlList
                                            //{
                                            if (itm.Name.StartsWith("question_number"))
                                            {
                                                qn++;
                                                if (qn > rec.AmoutOfQuestions)
                                                {
                                                    BarCodeItems.Remove(itm);
                                                    barCodeList.ControlList.Remove(barCodeList.ControlList[k]);
                                                    rec.BarCodeItems.RemoveAt(k);
                                                    goto rep;
                                                }
                                            }
                                        }
                                        Array.Resize(ref rec.allBarCodeNames, BarCodeItems.Count);
                                        Array.Resize(ref rec.allBarCodeValues, BarCodeItems.Count);
                                        Array.Resize(ref rec.questionNumbers, rec.AmoutOfQuestions);
                                        BarCodeControlsBuilding = true;
                                    }
                                    amout_of_questionsIndex = j;
                                    string s = rec.allBarCodeValues[j];
                                    if (!string.IsNullOrEmpty(s))
                                    {
                                        s = b.comboBox1.Text;
                                        rec.allBarCodeValues[j] = s;
                                    }
                                    try
                                    {
                                        j = Convert.ToInt32(s);
                                        amout_of_questions = j;
                                        amoutOfQuestions = j.ToString();
                                        SetRowsValue(amout_of_questions);
                                        if (rec.allBarCodeValues[amout_of_questionsIndex] != s)
                                        {
                                            rec.allBarCodeValues[amout_of_questionsIndex] = s;
                                            SetAmoutOfQuestions(amout_of_questions);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        if (appSettings.Fields.AmoutOfQuestions && lastAmoutOfQuestions != "")
                                        {
                                            b.comboBox1.Text = lastAmoutOfQuestions;
                                            b.comboBox1.ForeColor = Color.Red;
                                        }
                                        else
                                            amout_of_questions = -1;
                                    }
                                    break;
                            }
                            continue;
                        }
                        if (rec.QrCodeHeaders != null && rec.QrCodeHeaders.Length > 0 && rec.defaults.useStudentId)
                            continue;
                        if (item.areas != null && item.areas[0].width == 0)
                            continue;
                        string value = "";
                        bool verify = false;
                        if (item.name.StartsWith("question_number"))
                        {
                            string number = item.name;
                            number = number.Remove(0, number.LastIndexOf("_") + 1);
                            int index = Convert.ToInt32(number);
                            if (amout_of_questions > -1 && index > amout_of_questions)
                                continue;
                            if (rec.questionNumbers.Length < index)
                            {
                                Array.Resize(ref rec.questionNumbers, index);
                            }
                            index--;
                        }
                        switch (item.name)
                        {
                            case "district_id":
                                if (rec.QrCodeHeaders != null && rec.QrCodeHeaders.Length > 0)
                                {
                                    int index = Array.IndexOf(rec.QrCodeHeaders, "{" + item.name + "}");
                                    if (index > -1)
                                    {
                                        value = rec.QrCodeValues[index];
                                        districtId = value;
                                        verify = true;
                                    }
                                }
                                else if (appSettings.Fields.DistrictId && !string.IsNullOrEmpty(lastDistrictId))
                                    value = lastDistrictId;
                                break;
                            case "test_id":
                                if (rec.QrCodeHeaders != null && rec.QrCodeHeaders.Length > 0)
                                {
                                    int index = Array.IndexOf(rec.QrCodeHeaders, "{" + item.name + "}");
                                    if (index > -1)
                                    {
                                        value = rec.QrCodeValues[index];
                                        testId = value;
                                        verify = true;
                                    }
                                }
                                else if (appSettings.Fields.TestId && !string.IsNullOrEmpty(lastTestId))
                                {
                                    value = lastTestId;
                                    testId = lastTestId;
                                }
                                break;
                            case "amout_of_questions":
                                if (rec.QrCodeHeaders != null && rec.QrCodeHeaders.Length > 0)
                                {
                                    int index = Array.IndexOf(rec.QrCodeHeaders, "{" + item.name + "}");
                                    if (index > -1)
                                    {
                                        value = rec.QrCodeValues[index];
                                        amoutOfQuestions = value;
                                        rec.AmoutOfQuestions = Convert.ToInt32(value);
                                        verify = true;
                                    }
                                }
                                else if (appSettings.Fields.AmoutOfQuestions && !string.IsNullOrEmpty(lastAmoutOfQuestions))
                                    value = lastAmoutOfQuestions;
                                break;
                            case "question_number_1":
                            case "index_of_first_question":

                                if (rec.QrCodeHeaders != null && rec.QrCodeHeaders.Length > 0)
                                {
                                    int index = Array.IndexOf(rec.QrCodeHeaders, "{" + item.name + "}");
                                    if (index > -1)
                                    {
                                        value = rec.QrCodeValues[index];
                                        //indexOfQuestion = value;//???
                                        verify = true;
                                    }
                                }
                                else if (appSettings.Fields.IndexOfFirstQuestion && !string.IsNullOrEmpty(lastIndexOfQuestion))
                                    value = lastIndexOfQuestion;
                                break;
                            default:
                                value = "";
                                break;
                        }
                        if ((rec.QrCodeHeaders == null || rec.QrCodeHeaders.Length == 0)
                            && lastSheetId != "" && lastSheetId == rec.SheetIdentifier && lastTestId != "" && testId == lastTestId)
                        {
                            if (item.name == "amout_of_questions")
                            {
                                value = lastAmoutOfQuestions;
                                if (utils.IsNumeric(lastAmoutOfQuestions))
                                    rec.AmoutOfQuestions = Convert.ToInt32(lastAmoutOfQuestions);
                            }
                            else if (item.name == "index_of_first_question" || item.name == "question_number_1")
                            {
                                value = lastIndexOfQuestion;
                                if (utils.IsNumeric(lastIndexOfQuestion))
                                    rec.IndexOfFirstQuestion = Convert.ToInt32(lastIndexOfQuestion);
                            }
                        }

                        //var bItem = new BarCodeItem(item.name, "numbersText", value, value//item.type//rec.areas[1].type
                        //, new Rectangle(item.areas[0].left, item.areas[0].top, item.areas[0].width, item.areas[0].height));
                        var bItem = new BarCodeItem(item.name, item.areas[1].type, value, value//item.type//rec.areas[1].type
                        , new Rectangle(item.areas[0].left, item.areas[0].top, item.areas[0].width, item.areas[0].height));

                        recTools.SetOutputValues(ref rec.headers, ref rec.headersValues, ref rec.totalOutput, ref rec.allBarCodeNames
                               , ref rec.allBarCodeValues, bItem.Name, bItem.Value, item.outputPosition);

                        if (BarCodeControlsBuilding)
                            rec.FindOrAddAndSetValueBarcode(bItem.Name, value, verify);

                        if (item.name == "amout_of_questions")
                            amout_of_questionsIndex = rec.allBarCodeValues.Length - 1;
                        if (!verify || (item.name == "bubbles_per_line" && (bItem.Value != "5" || bItem.Value != "6")))
                        {
                            lblErr.Text = "Error in " + bItem.Name;
                            if (!errList.Contains(lblErr.Text))
                                errList.Add(lblErr.Text);
                            try
                            {
                                int index = Array.IndexOf(rec.allBarCodeNames, item.name);
                                BarCodeListItemControl b = barCodeList.ControlList[index] as BarCodeListItemControl;
                                b.comboBox1.ForeColor = Color.Red;
                            }
                            catch (Exception)
                            {
                            }
                            lblErr.Visible = true;
                        }
                        else if (item.name == "bubbles_per_line")
                        {
                            switch (bItem.Value)
                            {
                                case "5":
                                case "6":
                                    rec.bubbles_per_lineErr = false;
                                    break;
                                default:
                                    if (!errList.Contains(lblErr.Text))
                                        errList.Add(lblErr.Text);
                                    rec.bubbles_per_lineErr = true;
                                    try
                                    {
                                        int index = Array.IndexOf(rec.allBarCodeNames, item.name);
                                        BarCodeListItemControl b = barCodeList.ControlList[index] as BarCodeListItemControl;
                                        b.comboBox1.ForeColor = Color.Red;
                                    }
                                    catch (Exception)
                                    {
                                    }
                                    lblErr.Visible = true;
                                    break;
                            }
                        }
                    }
                    else if (item.type == "bubblesRegions")
                    {
                        rec.numberOfBubblesRegion = i;
                        bubblesRegions = item;
                        if (linsForm != null)
                        {
                            linsForm.nudArea.Maximum = item.areas.Length;
                            linsForm.nudArea.Value = 1;
                        }
                        if (factAreas.Length == 0)
                        {
                            for (int j = 0; j < rec.areas.Length; j++)
                                rec.areas[j].bubblesPerLine = item.areas[j].bubblesPerLine;
                        }

                        recTools.SetSettings
                            (
                              ref rec.bubblesRegions
                            , ref rec.bubblesOfRegion
                            , ref rec.bubblesSubLinesCount
                            , ref rec.bubblesSubLinesStep
                            , ref rec.bubblesPerLine
                            , ref rec.lineHeight
                            , ref rec.linesPerArea
                            , out rec.answersPosition
                            , out rec.indexAnswersPosition
                            , ref rec.totalOutput
                            , ref rec.bubbleLines
                            , item
                            , rec.kx, rec.ky
                            , rec.curRect, rec.etRect
                            );
                    }
                }
                if (BarCodeControlsBuilding)
                {
                    rec.maxAmoutOfQuestions = rec.linesPerArea.Sum();
                    allAreasNaturalSize = new Rectangle[bubblesRegions.areas.Length];
                    factAreasManualSet = new bool[bubblesRegions.areas.Length];

                    if (rec.SheetIdentifier == "FANDP")//rec.AmoutOfQuestions == 0
                    {
                        maxAmoutOfQuestions = rec.linesPerArea.Sum();
                        rec.AmoutOfQuestions = maxAmoutOfQuestions;
                        rec.FindOrAddAndSetValueBarcode("amout_of_questions", maxAmoutOfQuestions.ToString());
                        rec.FindOrAddAndSetValueBarcode("index_of_first_question", rec.IndexOfFirstQuestion.ToString());
                        BarCodeListItemControl b = barCodeList.ControlList["amout_of_questions"] as BarCodeListItemControl;
                        rbtnGrid.Enabled = false;//!!!
                        b.btnCheck.PerformClick();
                        linsForm.Close();
                        linsForm = null;
                        return;
                    }
                }
                //amout_of_questions = GetFactAreas(amout_of_questions);

                if (!SetFactAreasSetting())
                {
                    if (rec.AmoutOfQuestions > 0)
                    {
                        amout_of_questions = rec.AmoutOfQuestions;
                    }
                    amout_of_questions = GetFactAreas(amout_of_questions);
                    if (BarCodeControlsBuilding && lastSheetId != rec.SheetIdentifier || factAreas.Length == 0
                        || (amout_of_questions > 0 && amout_of_questions != factLinesPerArea.Sum()))
                    {
                        //if (factAreas.Length == 0)
                        //    amout_of_questions = GetFactAreas(amout_of_questions);

                        nudArea_ValueChanged(null, null);

                        for (int i = 0; i < factAreas.Length; i++)
                        {
                            factAreas[i].height = 0;
                        }
                    }
                    else
                    {
                        if (factLinesPerArea.Length == 0 && linsForm != null)
                            linsForm.nudRows.Value = rec.linesPerArea[0];
                        else
                        {
                            if (linsForm != null && BarCodeControlsBuilding)
                                linsForm.nudRows.Value = factLinesPerArea[0];
                        }
                        if (linsForm != null && BarCodeControlsBuilding)
                        {
                            linsForm.nudCols.Value = 4;// factAreas[0].bubblesPerLine;
                            linsForm.nudSubRows.Value = factAreas[0].subLinesAmount;
                        }
                    }
                }
                RecognizeBubblesButton.Enabled = true;
            }
            catch (Exception ex)
            {
                log.LogMessage(ex.Message);
            }
        }