将窗口内容显示为位图

Posted

技术标签:

【中文标题】将窗口内容显示为位图【英文标题】:Showing window content as bitmap 【发布时间】:2014-07-15 04:11:35 【问题描述】:

我想拍摄窗口内容的图像并在该窗口中将其显示为较小的位图...我关注了这篇文章:http://msdn.microsoft.com/en-us/library/dd183402(v=vs.85).aspx 当我想截取整个桌面时 - 它工作正常...问题是当我尝试仅获取窗口内容的位图时。任何想法我做错了什么?

这是我的代码:

HDC hDC;
HDC hDCMemDC = NULL;
HBITMAP hbmWindow = NULL;
BITMAP bmpWindow;

hDC = GetDC(hWnd);

hDCMemDC = CreateCompatibleDC(hDC);

RECT clientRect;
GetClientRect(hWnd, &clientRect);

hbmWindow = CreateCompatibleBitmap(hDC, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);

SelectObject(hDCMemDC, hbmWindow);

BitBlt(hDCMemDC, 0, 0, 100, 100, hDC, 0, 0, SRCCOPY);

谢谢

【问题讨论】:

“我想拍摄窗口内容的图像并在该窗口中显示为较小的位图”——较小的位图将在窗口中与较小位图的位置相对应的位置显示什么? 根本不明白你的问题,但是那个位图是窗口的较小版本......无论如何,我想在窗口中获取形状的图像,删除这些形状并只显示一个位图他们 【参考方案1】:
void DrawSelf(HDC Context, RECT Area, RECT NewArea)

    uint32_t W = Area.right - Area.left;
    uint32_t H = Area.bottom - Area.top;
    uint32_t NW = NewArea.right - NewArea.left;
    uint32_t NH = NewArea.bottom - NewArea.top;

    StretchBlt(Context, NewArea.left, NewArea.top, NW, NH, Context, Area.left, Area.top, W, H, SRCCOPY);

那么你可以这样做:

RECT Area;
RECT Area2;
HDC DC = GetDC(hwnd);  //Gets the client area only.. Use GetWindowDC for the whole window including the title-bar.
GetClientRect(hwnd, &Area); //client area only.
GetClientRect(hwnd, &Area2);

//Smaller area in which to draw.
Area2.left += 5;
Area2.right -= 5;
Area2.top += 5;
Area2.bottom -= 5;


DrawSelf(DC, Area, Area2);

ReleaseDC(hwnd, dc);

【讨论】:

【参考方案2】:

使用 GetWindowDC 而不是 GetDC 来获取整个窗口区域。

【讨论】:

以上是关于将窗口内容显示为位图的主要内容,如果未能解决你的问题,请参考以下文章

获取火花窗口的位图

将网络摄像头中的帧捕获为位图 - VFW - WINAPI

为啥在 glfw 窗口上没有使用 opengl 显示位图图像?在 C++ 中读取位图图像文件时出现问题

如何使用 GDI 将 RGB 位图绘制到窗口?

调整窗口大小时位图消失

如何在 C# 中捕获 Windows 应用商店应用程序的窗口内容