I am trying to capture desktop windows in C # based on window handles. I use .NET and use PInvoke for GetWindowRect () to capture the rectangle of the window. I have a window selection and a rectangle capture that works fine.
However, the captured rectangles of the window include not only the actual size of the window, but also windows, such as the shadow around it. When I try to copy a window into a bitmap, the bitmap contains an area and a shadow. On Windows 10, I get a transparent shadow area, including any content that can be seen under the active window:

The code I use is quite simple, capturing a Window using Win32 GetWindowRect () via a PInvoke call:
var rect = new Rect(); GetWindowRect(handle, ref rect); var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); var result = new Bitmap(bounds.Width, bounds.Height); using (var graphics = Graphics.FromImage(result)) { graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); } return result;
Then I capture the image and assign it to the image window.
In addition, it seems that there are some differences between the windows - some windows have shadows that are not there. Most of them, but some, such as Visual Studio and Chrome, do not do this, so itβs not even easy to remove extra pixels.
I tried using GetClientRect (), but this only gives me the client area, which is not what I got after. What I would like to get is the actual Window rectangle with borders but no shadows.
Is there any way to do this?
c # windows winapi pinvoke
Rick strahl
source share