AnalysisImageClick.OCRTools.extractImage C# (CSharp) Method

extractImage() public static method

public static extractImage ( Bitmap bmp ) : Bitmap
bmp System.Drawing.Bitmap
return System.Drawing.Bitmap
        public static Bitmap extractImage(Bitmap bmp)
        {
            try {
                for (int w = 0; w < bmp.Width; w++) {
                    for (int h = 0; h < bmp.Height; h++){
                        Color c = bmp.GetPixel(w, h);
                        if (c.R == 226 && c.G == 226 && c.B == 226){
                            Color c2 = Color.Red;
                            bmp.SetPixel(w, h, c2);
                        }
                    }
                }

                //ScreenCapture.saveBitmap("red_zones.png", bmp);
                int topRedY = detectFirstRedLine(bmp);
                topRedY = detectEndFirtRedLine(bmp, topRedY);
                if (topRedY != -1) {
                    bmp = cutTopImage(bmp, 0, topRedY);
                    //ScreenCapture.saveBitmap("top_red_zones.png", bmp);
                    int bottomRedY = detectFirstRedLine(bmp);
                    if (bottomRedY != -1){
                        int startY = (bottomRedY > 0) ? (bottomRedY * -1) : 0;
                        bmp = cutImage(bmp, bmp.Width, bottomRedY, 0, 0);
                        //ScreenCapture.saveBitmap("bottom_red_zones.png", bmp);
                        int leftRedX = detectSecondRedLine(bmp);
                        leftRedX = detectEndSecondRedLine(bmp, leftRedX);
                        bmp = cutTopImage(bmp, leftRedX, 0);
                        //ScreenCapture.saveBitmap("left_red_zones.png", bmp);
                        leftRedX = detectSecondRedLine(bmp);
                        bmp = cutImage(bmp, leftRedX, bmp.Height, 0, 0);
                        //ScreenCapture.saveBitmap("rigth_red_zones.png", bmp);
                    }
                }

                for (int w = 0; w < bmp.Width; w++)
                {
                    for (int h = 0; h < bmp.Height; h++)
                    {
                        Color c = bmp.GetPixel(w, h);
                        if (c.R == 255)
                        {
                            Color c2 = Color.FromArgb(0, 226, 226, 226);
                            bmp.SetPixel(w, h, c2);
                        }
                    }
                }

            }catch(Exception e){}
            return bmp;
        }

Usage Example

Beispiel #1
0
        public void updateMouseDown(int x, int y)
        {
            Bitmap bmp = ScreenCapture.captureScreen(0);

            if (bmp != null)  //40x35
            {
                Bitmap   zone = new Bitmap(40 * 2, 35 * 2);
                Graphics g    = Graphics.FromImage(zone);
                g.DrawImage(bmp, (x * -1) + 40, (y * -1) + 35);
                g.Dispose();
                bmp              = null;
                zone             = OCRTools.toGrayscale(zone);
                zone             = OCRTools.extractImage(zone);
                pictureBox.Image = zone;
            }
        }