【问题标题】:Get a window snapshot and crop it获取窗口快照并裁剪它
【发布时间】:2014-09-23 08:40:30
【问题描述】:

我的以下代码工作正常,它对我的​​应用程序上的活动窗口进行快照,将其放入 HBITMAP 变量并将其保存在文件中。 现在我希望能够根据给定的起始坐标和宽度/高度裁剪图像并仅保存其中的一部分。

重要的一点是我必须用标题栏保存窗口,而不仅仅是客户区,所以使用 PrintWindow() 而不是 BitBlt() 方法很容易实现。

我更喜欢使用 PrintWindow() 的解决方案,因为 BitBlt() 方法不能正确使用标题栏(除非您知道这样做的方法)。

当前适用于整个窗口的代码是:

HWND hParentWindow = GetActiveWindow();

    RECT rc;
    GetWindowRect(hParentWindow, &rc);
    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;

    //create
    HDC hdcParent = GetDC(NULL);
    HDC hdc = CreateCompatibleDC(hdcParent);
    HBITMAP hBmp = CreateCompatibleBitmap(hdcParent, width, height);
    SelectObject(hdc, hBmp);

    //Print to memory hdc
    PrintWindow(hParentWindow, hdc, 0);

    //copy to clipboard
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hBmp);
    CloseClipboard();

    // Save it in a file:
    saveBitmap(ofn.lpstrFile, hBmp);

    //release
    DeleteDC(hdc);
    DeleteObject(hBmp);
    ReleaseDC(NULL, hdcParent);

如何保存裁剪的位图?

【问题讨论】:

    标签: c++ winapi screenshot bitmapimage snapshot


    【解决方案1】:

    本质上是做一个 BitBlt。这是一个讨论此问题的线程,其解决方案似乎足以满足您的需求:

    Crop function BitBlt(...)

    【讨论】:

      【解决方案2】:
      • 创建另一个中间 hdc
      • 将窗口打印到此中间 hdc。
      • 从这个 hdc 复制 (bitblt) 你需要的 rect 到你的位图 hdc
      • 释放中间 hdc

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-22
        • 1970-01-01
        • 2012-09-11
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多