protected override sealed void ValidateWindowPosition(WindowPosition pos)
 {
     // Check if the aspect ratio of the window is 4:3 or higher.
     if (!(((double)pos.Size.Width / pos.Size.Height) >= 4d / 3d))
         throw new ArgumentException("The TT Rewritten window must have an aspect ratio " 
             + "of 4:3 or higher (e.g. 16:9).");
 }
        /// <summary>
        /// Determines the position and location of the client rectangle of the specified
        /// window. This method also checks if the specified window is in foreground.
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public WindowPosition GetWindowPosition(IntPtr hWnd)
        {
            // Check if the specified window is in foreground.
            if (NativeMethods.GetForegroundWindow() != hWnd)
                throw new Exception("The window is not in foreground any more.");

            // Get the client size.
            var clientRect = default(NativeMethods.RECT);
            if (!NativeMethods.GetClientRect(hWnd, ref clientRect))
                throw new Win32Exception();

            // Get the screen coordinates of the point (0, 0) in the client rect.
            var relPos = default(NativeMethods.POINT);
            if (!NativeMethods.ClientToScreen(hWnd, ref relPos))
                throw new Exception("Could not retrieve window client coordinates");

            // Check if the window is minimized.
            if (clientRect.Bottom - clientRect.Top == 0 && clientRect.Right - clientRect.Left == 0
                && relPos.X == -32000 && relPos.Y == -32000)
                throw new Exception("The window has been minimized.");


            var pos = new WindowPosition()
            {
                Coordinates = new Coordinates(relPos.X, relPos.Y),
                Size = new Size(clientRect.Right - clientRect.Left, clientRect.Bottom - clientRect.Top)
            };
            // Validate the position.
            ValidateWindowPosition(pos);
            return pos;
        }
            private ScreenshotContent(WindowPosition pos)
            {
                // Set the window position which will create a new rectangle.
                WindowPosition = pos;

                // Ensure we use Little Endian as byte order.
                // TODO: Is there a better way than using IPAddress to check this?
                if (IPAddress.HostToNetworkOrder((short)1) == 1)
                    throw new InvalidOperationException("This class currently only works "
                        + "on systems using little endian as byte order.");

                bmp = new Bitmap(rect.Width, rect.Height,
                    PixelFormat.Format32bppRgb);
            }
            public static ScreenshotContent Create(WindowPosition pos, 
                ScreenshotContent existingScreenshot = null)
            {
                // Try to reuse the existing screenshot's bitmap, if it has the same size.
                if (existingScreenshot != null && !(existingScreenshot.Size.Width == pos.Size.Width
                    && existingScreenshot.Size.Height == pos.Size.Height))
                {
                    // We cannot use the existing screenshot, so dispose of it.
                    existingScreenshot.Dispose();
                    existingScreenshot = null;
                }

                if (existingScreenshot == null)
                    existingScreenshot = new ScreenshotContent(pos);
                else
                    // The window could have been moved, so refresh the position.
                    existingScreenshot.WindowPosition = pos; 

                existingScreenshot.FillScreenshot();
                return existingScreenshot;
            }
 /// <summary>
 /// When overridden in subclasses, throws an exception if the window position is
 /// not valid. This implementation does nothing.
 /// </summary>
 /// <param name="pos">The WindowPosition to validate.</param>
 protected virtual void ValidateWindowPosition(WindowPosition pos)
 {
     // Do nothing.
 }
 /// <summary>
 /// When overridden in subclasses, throws an exception if the window position is
 /// not valid. This implementation does nothing.
 /// </summary>
 /// <param name="pos">The WindowPosition to validate.</param>
 protected virtual void ValidateWindowPosition(WindowPosition pos)
 {
     // Do nothing.
 }