public void ArrangeWindow() { WinAPIs.RECT wSize = new WinAPIs.RECT(); WinAPIs.GetWindowRect(ScreenshotHandler.unityWindow, ref wSize); // Get the size of the window WinAPIs.GetClientRect(ScreenshotHandler.unityWindow, out unitySize); this.Location = new Point(wSize.left, wSize.top); this.Size = new Size(wSize.right - wSize.left, wSize.bottom - wSize.top); // Set size and location }
public static Image GetImageFromWindow() { IntPtr wDC = WinAPIs.GetWindowDC(unityWindow); // Device Context WinAPIs.RECT winSize = new WinAPIs.RECT(); WinAPIs.GetWindowRect(unityWindow, ref winSize); // Get the size of the window WinAPIs.RECT clientSize = new WinAPIs.RECT(); WinAPIs.GetClientRect(unityWindow, out clientSize); // Get the client bounds for cropping later int width = winSize.right - winSize.left; int height = winSize.bottom - winSize.top; int clientWidth = clientSize.right - clientSize.left; int clientHeight = clientSize.bottom - clientSize.top; int winBorderSize = (width - clientWidth) / 2; int titleBorderSize = (height - clientHeight) - winBorderSize; IntPtr newDest = WinAPIs.CreateCompatibleDC(wDC); // New device context IntPtr newBitmap = WinAPIs.CreateCompatibleBitmap(wDC, width, height); // New bitmap for the image IntPtr bitmapObj = WinAPIs.SelectObject(newDest, newBitmap); WinAPIs.BitBlt(newDest, 0, 0, width, height, wDC, 0, 0, WinAPIs.SRC_COPY); // Copy the exact image from these bounds WinAPIs.SelectObject(newDest, newBitmap); // Reset the selection WinAPIs.DeleteDC(newDest); WinAPIs.ReleaseDC(unityWindow, wDC); // Clean up memeory Bitmap cropImage = Image.FromHbitmap(newBitmap); // Convert into a bitmap WinAPIs.DeleteObject(newBitmap); // More cleaning Bitmap cropped = cropImage.Clone(new Rectangle(winBorderSize, titleBorderSize, cropImage.Width - 2 * winBorderSize, cropImage.Height - titleBorderSize - winBorderSize), PixelFormat.Format24bppRgb); // Remove the window borders cropImage.Dispose(); // Prevent a memleak return(cropped); }