Com Piler
Com Piler

Reputation: 267

c# capture window content without borders

I have to capture content and only content of one window. Like that:

(captured by ScreenCaptor)

But my program captures that:

I use this code:

IntPtr ParenthWnd = GetForegroundWindow();
        if (!ParenthWnd.Equals(IntPtr.Zero))
        {
            IntPtr prevChild = IntPtr.Zero;
            IntPtr currChild = IntPtr.Zero;
            while (true)
            {
                currChild = FindWindowEx(ParenthWnd, prevChild, null, null);
                if (currChild == IntPtr.Zero) break;
                result.Add(currChild);
                label3.Text += currChild.ToString() + " _ ";
                prevChild = currChild;
            }
        }

then I choose my child window, e.g:

handle = result[0];

and finally capture screenshot:

RECT rect = new RECT();
            GetWindowRect(handle, ref rect);

            Bitmap image = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top);

            using (Graphics graphics = Graphics.FromImage(image))
            {
                IntPtr hDC = graphics.GetHdc();
                PrintWindow(new HandleRef(graphics, handle), hDC, 0);
                graphics.ReleaseHdc(hDC);
            }

of course I cannot crop captured image, because don't know size of each 'border'

thanks in advance

Upvotes: 3

Views: 2504

Answers (2)

joie95
joie95

Reputation: 11

Another solution is to print the window and remove the borders from the bmp:

[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);

public Bitmap printWindow()
{
     RECT clientRect;
     GetClientRect(WindowHandle, out clientRect);
     RECT windowRect;
     GetWindowRect(WindowHandle, out windowRect);
     int borderSize = (windowRect.Width - clientRect.Width) / 2;
     int titleBarSize = (windowRect.Height - clientRect.Height) - borderSize;
     PrintWindow(MainWindowHandle, hdcBitmap, 0);

     gfxBmp.ReleaseHdc(hdcBitmap);
     gfxBmp.Dispose();
     Bitmap bmp = bmp.Clone(new Rectangle(borderSize, titleBarSize, bmp.Width - 2*borderSize, bmp.Height - titleBarSize-borderSize), PixelFormat.Format32bppRgb);

     return bmp;
}

Upvotes: 0

joie95
joie95

Reputation: 11

a little bit late but maby it does someone help:

private const int GWL_STYLE = -16;              //hex constant for style changing
private const int WS_BORDER = 0x00800000;       //window with border
private const int WS_CAPTION = 0x00C00000;      //window with a title bar
private const int WS_SYSMENU = 0x00080000;      //window with no borders etc.
private const int WS_MINIMIZEBOX = 0x00020000;  //window with minimizebox


public static Bitmap printWindow(IntPtr hwnd)
{
    RECT rc;
    GetWindowRect(hwnd, out rc);
    Bitmap bmp;

    //make window borderless
    SetWindowLong(hwnd, GWL_STYLE, WS_SYSMENU);
    SetWindowPos(hwnd, -2, rc.X, rc.Y, rc.Width, rc.Height, 0x0040);

    DrawMenuBar(hwnd);
    bmp = new Bitmap(800, 800, PixelFormat.Format32bppArgb);
    Graphics gfxBmp = Graphics.FromImage(bmp);
    IntPtr hdcBitmap = gfxBmp.GetHdc();
    PrintWindow(hwnd, hdcBitmap, 0);

    gfxBmp.ReleaseHdc(hdcBitmap);
    gfxBmp.Dispose();

    //restore window
    SetWindowLong(hwnd, GWL_STYLE, WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX);
    DrawMenuBar(hwnd);
    ShowWindowAsync(hwnd, 1); //1 = Normal

    return bmp;
}

Upvotes: 1

Related Questions