internal TimerPosition GetTimerPosition() { var timerPosition = TimerPosition.Empty; var gameScreen = GetGameScreenshot(); var units = GraphicsUnit.Point; var bmpRectangleF = gameScreen.GetBounds(ref units); var bmpRectangle = Rectangle.Round(bmpRectangleF); var newCoordSys = new NewScreenCoordinateSystem(bmpRectangle); var bitmap = GetImgForTimerSerch(gameScreen); if (SearchInColorsList(bitmap, newCoordSys.GetAllTopCoordinate())) { timerPosition = TimerPosition.Top; } if (SearchInColorsList(bitmap, newCoordSys.GetAllRightCoordinate())) { timerPosition = TimerPosition.Right; } if (SearchInColorsList(bitmap, newCoordSys.GetAllBottomCoordinate())) { timerPosition = TimerPosition.Bottom; } if (SearchInColorsList(bitmap, newCoordSys.GetAllLeftCoordinate())) { timerPosition = TimerPosition.Left; } bitmap = null; GC.Collect(); return(timerPosition); }
/// <summary> /// Обрезка черных полос сверху и снизу изображения. /// </summary> /// <param name="bitmap">Орегинальный скриншот экрана</param> /// <returns></returns> private Bitmap CropLetterbox(Bitmap bitmap) { var topBlackLine = new Coordinate(); var botBlackLine = new Coordinate(); var leftBlackLine = new Coordinate(); var bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height); var newCoordSys = new NewScreenCoordinateSystem(bounds); // Подрезка боковых рамок окна и заголовка окна var borderSize = SystemInformation.BorderSize; int titleHeight = (int)(SystemInformation.CaptionHeight * 1.5); var workAreaRec = new Rectangle(borderSize.Width, titleHeight, bitmap.Width - borderSize.Width * 2, bitmap.Height - titleHeight); bitmap = bitmap.Clone(workAreaRec, bitmap.PixelFormat); bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height); newCoordSys = new NewScreenCoordinateSystem(bounds); var top = newCoordSys.GetAllTopCoordinate().Reverse(); var bot = newCoordSys.GetAllBottomCoordinate().Reverse(); var left = newCoordSys.GetAllLeftCoordinate().Reverse(); var right = newCoordSys.GetAllRightCoordinate().Reverse(); foreach (var item in top) { var color = bitmap.GetPixel(item.X, item.Y); if (color.ToArgb() != System.Drawing.Color.Black.ToArgb()) { topBlackLine = new Coordinate(item.X, item.Y); break; } } foreach (var item in bot) { var color = bitmap.GetPixel(item.X, item.Y); if (color.ToArgb() != System.Drawing.Color.Black.ToArgb()) { botBlackLine = new Coordinate(item.X, item.Y); break; } } foreach (var item in left) { var color = bitmap.GetPixel(item.X, item.Y); if (color.ToArgb() != System.Drawing.Color.Black.ToArgb()) { leftBlackLine = new Coordinate(item.X, item.Y); break; } } var imgBounds = new Rectangle(leftBlackLine.X, topBlackLine.Y, bitmap.Width - leftBlackLine.X * 2, botBlackLine.Y - topBlackLine.Y); bitmap = bitmap.Clone(imgBounds, bitmap.PixelFormat); return(bitmap); }
/// <summary> /// Получить граници блока с текстом /// </summary> /// <param name="bmp"></param> /// <returns></returns> private Rectangle GetBoundsTextBlock(Bitmap bmp, bool cropTextBorder = false) { var firstY = 0; var secondY = 0; var firstX = 0; var secontX = 0; var cropW = (int)(bmp.Width * 0.1); var cropH = (int)(bmp.Height * 0.1); var cropBounds = new Rectangle(0 + cropW, 0 + cropH, bmp.Width - cropW * 2, bmp.Height - cropH * 2); var cropBmp = bmp.Clone(cropBounds, bmp.PixelFormat); using (var g = Graphics.FromImage(bmp)) { g.Clear(System.Drawing.Color.White); g.CompositingMode = CompositingMode.SourceOver; g.DrawImage(cropBmp, cropW, cropH); } Mat mat; mat = bmp.ToMat(); Cv2.CvtColor(mat, mat, ColorConversionCodes.RGB2GRAY); mat = mat.Blur(new Size(5, 5)); mat = mat.Threshold(75, 255, ThresholdTypes.Binary); bmp = mat.ToBitmap(); var bounds = new Rectangle(0, 0, bmp.Width, bmp.Height); var newCoordSys = new NewScreenCoordinateSystem(bounds); var topCord = newCoordSys.GetAllTopCoordinate().Reverse(); var botCord = newCoordSys.GetAllBottomCoordinate().Reverse(); var leftCord = newCoordSys.GetAllLeftCoordinate().Reverse(); var rightCord = newCoordSys.GetAllRightCoordinate().Reverse(); foreach (var item in topCord) { var color = bmp.GetPixel(item.X, item.Y); if (color.ToArgb() != System.Drawing.Color.White.ToArgb()) { if (cropTextBorder == false) { firstY = item.Y + 10; } else { firstY = item.Y; } break; } } foreach (var item in botCord) { var color = bmp.GetPixel(item.X, item.Y); if (color.ToArgb() != System.Drawing.Color.White.ToArgb()) { if (cropTextBorder == false) { secondY = item.Y - 10; } else { secondY = item.Y; } break; } } foreach (var item in leftCord) { var color = bmp.GetPixel(item.X, item.Y); if (color.ToArgb() != System.Drawing.Color.White.ToArgb()) { if (cropTextBorder == false) { firstX = item.X + 10; } else { firstX = item.X; } break; } } foreach (var item in rightCord) { var color = bmp.GetPixel(item.X, item.Y); if (color.ToArgb() != System.Drawing.Color.White.ToArgb()) { if (cropTextBorder == false) { secontX = item.X - 10; } else { secontX = item.X; } break; } } var imgBounds = new Rectangle(firstX, firstY, secontX - firstX, secondY - firstY); return(imgBounds); }