/* @param size: if different than window size, resizes the window before taking the screenshot * note that this is different than resizing a bitmap, we are literally changing the window size */ public Bitmap Take(Window window, bool clientOnly, Size size, Mode mode) { // We cannot use this method if the window is minimized if (window.Minimized) { return(null); } Size originalWindowSize = window.Size; bool needResize = !originalWindowSize.Equals(size); try { if (needResize) { // If we are taking the client only, we don't need the extra repaint window.Resize(size, clientOnly ? false : true); } Rectangle screenshotRect = clientOnly ? window.ClientRectangle : window.Rectangle; Bitmap result = new Bitmap(screenshotRect.Width, screenshotRect.Height, PixelFormat.Format24bppRgb); if (mode == Mode.PrintWindow) { GetScreenshotUsingPrintWindow(window, clientOnly, result); } else if (mode == Mode.PrintScreen) { GetScreenshotUsingPrintScreen(window, clientOnly, result); } // Restore original dimension if (needResize) { window.Resize(originalWindowSize, true); // OK, repaint now! } return(result); } catch (Exception e) { Trace.WriteLine("Failed to take screenshot of " + window.Title + ": " + e.Message); return(null); } }
/* @param size: if different than window size, resizes the window before taking the screenshot * note that this is different than resizing a bitmap, we are literally changing the window size */ public Bitmap Take(Window window, bool clientOnly, Size size, Mode mode) { // We cannot use this method if the window is minimized if (window.Minimized) return null; Size originalWindowSize = window.Size; bool needResize = !originalWindowSize.Equals(size); try { if (needResize) { // If we are taking the client only, we don't need the extra repaint window.Resize(size, clientOnly ? false : true); } Rectangle screenshotRect = clientOnly ? window.ClientRectangle : window.Rectangle; Bitmap result = new Bitmap(screenshotRect.Width, screenshotRect.Height, PixelFormat.Format24bppRgb); if (mode == Mode.PrintWindow) { GetScreenshotUsingPrintWindow(window, clientOnly, result); } else if (mode == Mode.PrintScreen) { GetScreenshotUsingPrintScreen(window, clientOnly, result); } // Restore original dimension if (needResize) { window.Resize(originalWindowSize, true); // OK, repaint now! } return result; } catch (Exception e) { Trace.WriteLine("Failed to take screenshot of " + window.Title + ": " + e.Message); return null; } }
public bool VisuallyProcessTableImage(Bitmap screenshot) { UpdateCardMatchDialogSpawnLocation(); /* This code would resize the map and recompute the data in it, * but we don't use this approach anymore. */ //recognitionMap.AdjustToSize(screenshot.Size); /* Instead if the screenshot we took differs in size from the map at our disposal * we resize the window and retake the screenshot */ if (!screenshot.Size.Equals(recognitionMap.OriginalMapSize)) { // Globals.Director.WriteDebug(String.Format("Screenshot size ({0}x{1}) differs from our map image ({2}x{3}), resizing window...", // screenshot.Size.Width, screenshot.Size.Height, recognitionMap.OriginalMapSize.Width, recognitionMap.OriginalMapSize.Height)); Size winSize = tableWindow.Size; Size difference = new Size(screenshot.Size.Width - recognitionMap.OriginalMapSize.Width, screenshot.Size.Height - recognitionMap.OriginalMapSize.Height); Size newSize = winSize - difference; tableWindow.Resize(newSize, true); // Globals.Director.WriteDebug(" --- CurrentHeroSeat: " + table.CurrentHeroSeat); // Globals.Director.WriteDebug(" --- resizing window try again later"); return(false); // At next iteration this code should not be executed because sizes will be the same, unless the player resizes the window } if (this.processingScreenShot) { //Globals.Director.WriteDebug("Processing Existing screenshot, returning"); // Dispose screenshot if (screenshot != null) { screenshot.Dispose(); } return(false); } else { processingScreenShot = true; } int heroSeat = table.CurrentHeroSeat == 0 ? 1 : table.CurrentHeroSeat; // If we don't know where the player is seated, we don't need to process any further if (table.CurrentHeroSeat == 0) { Globals.Director.WriteDebug(" ERROR: --- could not find CurrentHeroSeat???"); return(false); } foreach (Player player in table.PlayerList) { int seat = player.SeatNumber; ProcessPlayerCardActions(screenshot, seat, seat == heroSeat); } ProcessCommunityCardActions(screenshot); // Dispose screenshot if (screenshot != null) { screenshot.Dispose(); } processingScreenShot = false; return(true); }
void timedScreenshotTaker_ScreenshotTaken(Bitmap screenshot) { UpdateCardMatchDialogSpawnLocation(); /* This code would resize the map and recompute the data in it, * but we don't use this approach anymore. */ //recognitionMap.AdjustToSize(screenshot.Size); /* Instead if the screenshot we took differs in size from the map at our disposal * we resize the window and retake the screenshot */ if (!screenshot.Size.Equals(recognitionMap.OriginalMapSize)) { Trace.WriteLine(String.Format("Screenshot size ({0}x{1}) differs from our map image ({2}x{3}), resizing window...", screenshot.Size.Width, screenshot.Size.Height, recognitionMap.OriginalMapSize.Width, recognitionMap.OriginalMapSize.Height)); Size winSize = tableWindow.Size; Size difference = new Size(screenshot.Size.Width - recognitionMap.OriginalMapSize.Width, screenshot.Size.Height - recognitionMap.OriginalMapSize.Height); Size newSize = winSize - difference; tableWindow.Resize(newSize, true); return; // At next iteration this code should not be executed because sizes will be the same, unless the player resizes the window } // If we don't know where the player is seated, we don't need to process any further if (table.CurrentHeroSeat == 0) { return; } /* Try to match player cards */ List <Bitmap> playerCardImages = new List <Bitmap>(); ArrayList playerCardsActions = colorMap.GetPlayerCardsActions(table.CurrentHeroSeat); foreach (String action in playerCardsActions) { Rectangle actionRect = recognitionMap.GetRectangleFor(action); if (!actionRect.Equals(Rectangle.Empty)) { playerCardImages.Add(ScreenshotTaker.Slice(screenshot, actionRect)); } else { Trace.WriteLine("Warning: could not find a rectangle for action " + action); } } CardList playerCards = matcher.MatchCards(playerCardImages, false); if (playerCards != null) { Trace.WriteLine("Matched player cards! " + playerCards.ToString()); handler.PlayerHandRecognized(playerCards); } // Dispose foreach (Bitmap image in playerCardImages) { if (image != null) { image.Dispose(); } } /* If community cards are supported, try to match them */ if (colorMap.SupportsCommunityCards) { List <Bitmap> communityCardImages = new List <Bitmap>(); ArrayList communityCardsActions = colorMap.GetCommunityCardsActions(); foreach (String action in communityCardsActions) { Rectangle actionRect = recognitionMap.GetRectangleFor(action); if (!actionRect.Equals(Rectangle.Empty)) { communityCardImages.Add(ScreenshotTaker.Slice(screenshot, actionRect)); } else { Trace.WriteLine("Warning: could not find a rectangle for action " + action); } } // We try to identify as many cards as possible CardList communityCards = matcher.MatchCards(communityCardImages, true); if (communityCards != null && communityCards.Count > 0) { Trace.WriteLine("Matched board cards! " + communityCards.ToString()); handler.BoardRecognized(communityCards); } // Dispose foreach (Bitmap image in communityCardImages) { if (image != null) { image.Dispose(); } } } // Dispose screenshot if (screenshot != null) { screenshot.Dispose(); } }