Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 980
Com Piler Avatar asked Dec 06 '25 06:12

Com Piler


1 Answers

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;
}
like image 132
joie95 Avatar answered Dec 08 '25 19:12

joie95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!