void SetupWindowCapture()
        {
            //UpdateCursorInfo();

            // Get the device context
            if (isDesktop)
            {
                hdc = Win32Funcs.GetDC(IntPtr.Zero);
            }
            else
            {
                hdc = Win32Funcs.GetWindowDC(hwnd);
            }

            // Create a device context to use yourself
            hDest = Win32Funcs.CreateCompatibleDC(hdc);

            Win32Types.RECT windowRect;

            if (isDesktop)
            {
                Win32Types.MonitorInfo mi = new Win32Types.MonitorInfo();
                mi.cbSize = Marshal.SizeOf(mi);
                Win32Funcs.GetMonitorInfo(hwnd, ref mi);
                windowRect = mi.rcMonitor;
            }
            else
            {
                // Get the size of the window
                Win32Funcs.GetWindowRect(hwnd, out windowRect);
            }
            // Sets the width and height of the captured window
            windowWidth  = windowRect.Width;
            windowHeight = windowRect.Height;

            if (onlyCaptureMouse)
            {
                windowWidth  = cursorRect.Width;
                windowHeight = cursorRect.Height;
            }

            // From http://stackoverflow.com/questions/7502588/createcompatiblebitmap-and-createdibsection-memory-dcs
            bmi = new Win32Types.BitmapInfo();
            bmi.bmiHeader.Init();
            bmi.bmiHeader.biSize        = (uint)Marshal.SizeOf(bmi);
            bmi.bmiHeader.biWidth       = windowWidth;
            bmi.bmiHeader.biHeight      = -windowHeight; // top-down
            bmi.bmiHeader.biPlanes      = 1;
            bmi.bmiHeader.biBitCount    = 24;
            bmi.bmiHeader.biCompression = Win32Consts.BitmapCompressionMode.BI_RGB;


            IntPtr outBits;

            curRenderingBitmap = Win32Funcs.CreateDIBSection(hdc, ref bmi, (uint)Win32Consts.DIB_Color_Mode.DIB_PAL_COLORS, out outBits, IntPtr.Zero, (uint)0);

            oldhDest = Win32Funcs.SelectObject(hDest, curRenderingBitmap);
        }
Example #2
0
 public static bool GetScreenRect(out Win32Types.RECT result)
 {
     return(Win32Funcs.GetWindowRect(Win32Funcs.GetDesktopWindow(), out result));
 }
Example #3
0
        public byte[] GetWindowBitmapUsingBitBlt(out int numBytesPerRow)
        {
            windowRect = new Win32Types.RECT();
            bool result;

            if (isDesktop)
            {
                Win32Types.MonitorInfo mi = new Win32Types.MonitorInfo();
                mi.cbSize = Marshal.SizeOf(mi);
                result    = Win32Funcs.GetMonitorInfo(hwnd, ref mi);
                if (result)
                {
                    windowRect = mi.rcMonitor;
                }
            }
            else
            {
                // Get the size of the window
                result = Win32Funcs.GetWindowRect(hwnd, out windowRect);
            }

            if (onlyCaptureMouse)
            {
                windowRect = cursorRect;
                result     = true;
            }

            if (!result)
            {
                // Failed getting rect
                numBytesPerRow = 0;

                return(null);
            }

            // If they resized the window we need to reinit the memory
            if (windowWidth != windowRect.Width || windowHeight != windowRect.Height)
            {
                CleanupWindowCapture();
                SetupWindowCapture();
                windowWidth  = windowRect.Width;
                windowHeight = windowRect.Height;
            }



            // Use the previously created device context with the bitmap
            Win32Funcs.SelectObject(hDest, curRenderingBitmap);

            if (onlyCaptureMouse)
            {
                if (isDesktop)
                {
                    Win32Funcs.BitBlt(hDest, 0, 0, cursorRect.Width, cursorRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                else
                {
                    Win32Funcs.BitBlt(hDest, cursorRect.Left, cursorRect.Height, cursorRect.Width, cursorRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                Win32Funcs.DrawIconEx(hDest, 1, 1, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL);
            }
            else
            {
                // Copy from the screen device context to the bitmap device context
                if (isDesktop)
                {
                    Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                else
                {
                    Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                Win32Funcs.DrawIconEx(hDest, cursorRect.Left, cursorRect.Top, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL);
            }



            Win32Types.BITMAP bitmap = new Win32Types.BITMAP();

            Win32Funcs.GetObjectBitmap(curRenderingBitmap, Marshal.SizeOf(bitmap), ref bitmap);

            numBytesPerRow = bitmap.bmWidthBytes;

            if (bitmapBytes == null || bitmapBytes.Length != bitmap.bmHeight * bitmap.bmWidthBytes)
            {
                bitmapBytes = new byte[bitmap.bmHeight * bitmap.bmWidthBytes];
            }


            if (bitmap.bmBits != IntPtr.Zero)
            {
                Marshal.Copy(bitmap.bmBits, bitmapBytes, 0, bitmapBytes.Length);
            }

            if (bitmapBytes == null || bitmapBytes.Length == 0)
            {
                return(null);
            }

            return(bitmapBytes);
        }
Example #4
0
 public void GetRect()
 {
     Win32Funcs.GetWindowRect(hwnd, out windowRect);
 }
        public byte[] GetWindowBitmapUsingBitBlt(out int numBytesPerRow)
        {
            windowRect = new Win32Types.RECT();
            bool result;

            if (isDesktop)
            {
                Win32Types.MonitorInfo mi = new Win32Types.MonitorInfo();
                mi.cbSize = Marshal.SizeOf(mi);
                result    = Win32Funcs.GetMonitorInfo(hwnd, ref mi);
                if (result)
                {
                    windowRect = mi.rcMonitor;
                }
            }
            else
            {
                // Get the size of the window
                result = Win32Funcs.GetWindowRect(hwnd, out windowRect);
            }

            if (onlyCaptureMouse)
            {
                windowRect = cursorRect;
                result     = true;
            }

            if (!result)
            {
                // Failed getting rect
                numBytesPerRow = 0;

                return(null);
            }

            // If they resized the window we need to reinit the memory

            if (windowWidth != windowRect.Width || windowHeight != windowRect.Height)
            {
                CleanupWindowCapture();
                SetupWindowCapture();
                windowWidth  = windowRect.Width;
                windowHeight = windowRect.Height;
            }
            oldhDest = Win32Funcs.SelectObject(hDest, curRenderingBitmap);
            Win32Types.BITMAP bitmap = new Win32Types.BITMAP();
            Win32Funcs.GetObjectBitmap(curRenderingBitmap, Marshal.SizeOf(bitmap), ref bitmap);

            if (onlyCaptureMouse)
            {
                if (isDesktop)
                {
                    Win32Funcs.BitBlt(hDest, 0, 0, cursorRect.Width, cursorRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                else
                {
                    Win32Funcs.BitBlt(hDest, cursorRect.Left, cursorRect.Height, cursorRect.Width, cursorRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                Win32Funcs.DrawIconEx(hDest, 1, 1, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL);
            }
            else
            {
                // Copy from the screen device context to the bitmap device context
                var testing = false;
                if (isDesktop)
                {
                    Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY);
                }
                else
                {
                    if (testing)
                    {
                        Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY);
                    }
                    else
                    {
                        if (needsGDIp)
                        {
                            Bitmap   map      = new Bitmap(windowRect.Width, windowRect.Height);
                            Graphics graphics = Graphics.FromHdc(hDest);
                            try
                            {
                                graphics.CopyFromScreen(new System.Drawing.Point(windowRect.Left, windowRect.Top), System.Drawing.Point.Empty, windowRect.Size, CopyPixelOperation.SourceCopy);
                            }
                            finally
                            {
                                map.Dispose();
                                graphics.Dispose();
                            }
                        }
                    }
                }
                Win32Funcs.DrawIconEx(hDest, cursorRect.Left, cursorRect.Top, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL);
            }

            numBytesPerRow = bitmap.bmWidthBytes;

            if (bitmapBytes == null || bitmapBytes.Length != bitmap.bmHeight * bitmap.bmWidthBytes)
            {
                bitmapBytes = new byte[bitmap.bmHeight * bitmap.bmWidthBytes];
            }

            if (bitmap.bmBits != IntPtr.Zero)
            {
                Marshal.Copy(bitmap.bmBits, bitmapBytes, 0, bitmapBytes.Length);
            }

            if (bitmapBytes == null || bitmapBytes.Length == 0)
            {
                return(null);
            }
            Win32Funcs.DeleteObject(bitmap.bmBits);
            Win32Funcs.SelectObject(hDest, oldhDest);
            return(bitmapBytes);
        }