c#捕获没有边框的窗口内容

时间:2013-06-27 14:57:21

标签: c# window screen capture handle

我必须捕获一个窗口的内容和内容。像那样:

(由ScreenCaptor捕获)

但是我的程序捕获了:

我使用此代码:

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;
            }
        }

然后我选择我的子窗口,例如:

handle = result[0];

最后捕捉截图:

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);
            }

当然我无法裁剪拍摄的图像,因为不知道每个'边框'的大小

提前致谢

2 个答案:

答案 0 :(得分:1)

有点迟了但maby有人帮忙:

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;
}

答案 1 :(得分:0)

另一个解决方案是打印窗口并从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;
}