Ejemplo n.º 1
0
        /// <summary>
        /// Redraws the screenShot
        /// </summary>
        /// <param name="highlightIndex">Which of the labels should be highlighted, -1 for none.</param>
        /// <param name="showLabels">Show set label rectangles.</param>
        /// <param name="whiteThreshold">Preview of white-Threshold. -1 to disable.</param>
        /// <param name="manualRectangle">draw a custom rectangle</param>
        private void RedrawScreenshot(int highlightIndex = -1, bool showLabels = true, int whiteThreshold = -1, Rectangle manualRectangle = default)
        {
            if (_screenshot == null ||
                OCRDebugLayoutPanel.Controls.Count == 0 ||
                !(OCRDebugLayoutPanel.Controls[OCRDebugLayoutPanel.Controls.Count - 1] is PictureBox p)
                )
            {
                return;
            }

            _redrawingDebouncer.Cancel();

            Bitmap b = new Bitmap((whiteThreshold >= 0 ? ArkOcr.RemovePixelsUnderThreshold(ArkOcr.GetGreyScale(_screenshot), (byte)whiteThreshold, true) : _screenshot));

            if (showLabels || !manualRectangle.IsEmpty)
            {
                using (Graphics g = Graphics.FromImage(b))
                    using (Pen penW = new Pen(Color.White, 2))
                        using (Pen penY = new Pen(Color.Yellow, 2))
                            using (Pen penB = new Pen(Color.Black, 2))
                            {
                                penW.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                                penY.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                                penB.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;

                                if (!manualRectangle.IsEmpty)
                                {
                                    var rec = manualRectangle;
                                    rec.Inflate(2, 2);
                                    g.DrawRectangle(penY, rec);
                                    rec.Inflate(2, 2);
                                    g.DrawRectangle(penB, rec);
                                }
                                else
                                {
                                    for (int r = 0; r < ArkOcr.Ocr.ocrConfig.labelRectangles.Length; r++)
                                    {
                                        var rec = ArkOcr.Ocr.ocrConfig.labelRectangles[r];
                                        rec.Inflate(2, 2);
                                        g.DrawRectangle(r == highlightIndex ? penY : penW, rec);
                                        rec.Inflate(2, 2);
                                        g.DrawRectangle(penB, rec);
                                    }
                                }
                            }

                var magnifiedRectangle = highlightIndex != -1
                    ? ArkOcr.Ocr.ocrConfig.labelRectangles[highlightIndex]
                    : !manualRectangle.IsEmpty
                        ? manualRectangle
                        : Rectangle.Empty;

                if (!magnifiedRectangle.IsEmpty)
                {
                    var       currentWhiteThreshold = Properties.Settings.Default.OCRWhiteThreshold;
                    var       magnifiedMarginBottom = -1;
                    var       magnifiedMarginTop    = -1;
                    const int recMargin             = 30;

                    if (magnifiedRectangle.Y - OCRDebugLayoutPanel.VerticalScroll.Value >
                        OCRDebugLayoutPanel.Height / 2)
                    {
                        magnifiedMarginTop = recMargin + OCRDebugLayoutPanel.VerticalScroll.Value;
                    }
                    else
                    {
                        magnifiedMarginBottom = OCRDebugLayoutPanel.Height < b.Height
                            ? b.Height - OCRDebugLayoutPanel.Height + recMargin - OCRDebugLayoutPanel.VerticalScroll.Value
                            : recMargin;
                    }

                    DrawMagnifiedRectangle(b, magnifiedRectangle, currentWhiteThreshold, magnifiedMarginTop, magnifiedMarginBottom);
                }
            }

            Bitmap disp = (Bitmap)p.Image; // take pointer to old image to dispose it soon

            p.Image = b;
            if (disp != null && disp != _screenshot)
            {
                disp.Dispose();
            }
        }