public Bitmap PrintWindow(IntPtr hwnd) { log.Info($"PrintWindow IntPtr = {hwnd.ToInt32()}"); WindowFunctions.RECT rc; WindowFunctions.GetWindowRect(hwnd, out rc); Bitmap src = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb); Graphics gfxBmp = Graphics.FromImage(src); IntPtr hdcBitmap = gfxBmp.GetHdc(); WindowFunctions.PrintWindow(hwnd, hdcBitmap, 0); gfxBmp.ReleaseHdc(hdcBitmap); gfxBmp.Dispose(); //NOTE: To make the image smaller and less OCR needed take a smaller picture. var croppedWidth = (int)(rc.Width * ImageCropWidth); var croppedHeight = (int)(rc.Height * ImageCropHeight); return(CroppedImage(src, croppedWidth, croppedHeight)); }
private async void MontiorFunctions() { try { Process[] processes = Process.GetProcesses().Where(x => x.ProcessName.Contains("League") && x.MainWindowHandle != IntPtr.Zero).ToArray(); var processCount = processes.Count(); var currentlyPlayingGame = processes.Any(x => x.ProcessName.Contains("League of Legends")); if (processCount > 0 && _LeagueWasRunning == false) { log.Info("MontiorFunctions League of legends is now running."); CurrentlyMonitoring = true; _LeagueWasRunning = true; } else if (_UserEnabledOrDisabled == false || _CurrentlyMonitoring == false || processCount == 0 || currentlyPlayingGame) { if (processCount == 0) { _LeagueWasRunning = false; } if (processCount > 0 && _UserEnabledOrDisabled == true && currentlyPlayingGame == false && _CurrentlyMonitoring == false) { //NOTE: If league is running, user did not disable the control, // we are not playing the game, and CurrentlyMonitoring is off, // turn back on currently monitoring, because we have just finished the game. CurrentlyMonitoring = true; } else { CurrentlyMonitoring = false; return; } } else { CurrentlyMonitoring = true; } _LoLProcess = processes.FirstOrDefault(x => (x.ProcessName.Contains("LeagueClientUxRender") || x.ProcessName.Contains("LeagueClient"))); if (_LoLProcess == null) { log.Warn("MontiorFunctions was unable to find a non null main window in its league of legends processes."); return; } IntPtr ptr = _LoLProcess.MainWindowHandle; //NOTE: The print window method does not work correctly when the applicaiton is minimized. if (AllowLaunchPageMinimizing == false) { WindowFunctions.ShowWindow(_LoLProcess.MainWindowHandle, WindowFunctions.SW_SHOWNORMAL); } var image = PrintWindow(ptr); log.Debug($"MontiorFunctions - Read Image."); var screenCaptureIron = new Tesseract.TesseractEngine(@"./tessdata", "eng").Process(image); var screenCaptureIronText = screenCaptureIron.GetText(); log.Debug($"MontiorFunctions - Result From Image = {screenCaptureIronText}"); var expectedString = "ACCEPT"; if (screenCaptureIronText.ToUpper().Contains(expectedString)) { WindowFunctions.SetForegroundWindow(_LoLProcess.MainWindowHandle); await Task.Delay(500); WindowFunctions.RECT LeagueRect; WindowFunctions.GetWindowRect(ptr, out LeagueRect); var right = (int)(AcceptLocationX * (double)(LeagueRect.Left + LeagueRect.Right)); var bottom = (int)(AcceptLocationY * (double)(LeagueRect.Top + LeagueRect.Bottom)); log.Debug($"LeftMouseClick center = {bottom} bottom = {right}"); WindowFunctions.LeftMouseClick(right, bottom); } } catch (Exception exception) { log.Error("MontiorFunctions", exception); } finally { this._MonitorTimer.Start(); } }