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); }
private static void TCPReceiver() { using (NetworkStream nStream = tcp.GetStream()) // Get the stream { using (BinaryReader reader = new BinaryReader(nStream)) // Read it { uiWriter = new BinaryWriter(nStream); // Set the writer while (true) // Permanently try to read { string newData = reader.ReadString(); // Unbelievably messy code for receiving commands (somebody else can improve it :)) ) if (newData.Equals("SHOWSAND")) { instance.Invoke(() => { MessageBox.Show("You can now start the Sandbox", "Connected!"); }); } else if (newData.StartsWith("SHOWMSG")) { instance.Invoke(() => { MessageBox.Show(newData.Split('|')[1], "Message from TABS"); }); } else if (newData.StartsWith("INGAME")) { isIngame = bool.Parse(newData.Split('|')[1]); // Change the ingame status } else if (newData.StartsWith("DEBUG")) { Console.WriteLine(newData.Split('|')[1]); // Print out debug text } else if (newData.StartsWith("WINH")) { ScreenshotHandler.unityWindow = new IntPtr(long.Parse(newData.Split('|')[1])); // Set the handle new Thread(() => ScreenshotHandler.FramingThread()).Start(); // Start the framing thread } else if (newData.StartsWith("GSTARTED")) { bool started = bool.Parse(newData.Split('|')[1]); // Is game started? if (!isIngame) { continue; // Continue if the player's in the main menu } if (!isHost) { if (started) { screenshareForm.ArrangeWindow(); screenshareForm.Visible = true; WinAPIs.SetForegroundWindow(screenshareForm.Handle); ScreenshotHandler.WriteToUdp(StrToByte("HELLO")); // Arrange and make the screenshare window ready for receiving } else { screenshareForm.Visible = false; WinAPIs.SetForegroundWindow(ScreenshotHandler.unityWindow); // Hide the screenshare form // and set the game as the active one again } } ScreenshotHandler.streaming = started; } } } } }