屏幕截图仅返回黑色图像
Screen capture only returns a black image
我正在尝试在我的MFC应用程序中截取主对话框的屏幕截图并将其另存为图像文件。我尝试了我能在网上找到的每个示例,并且总是得到相同的结果:图像文件具有正确的尺寸(我尝试使用主要对话框以外的对话框进行此操作以确保),但它都是黑色的。我最近的解决方案是使用 CBitmap 类将主对话框句柄传输到 CImage。这是我的代码:
CWnd* mainWindow;
CDC* mainWindowDC;
CBitmap bitmapToSave;
CImage imageToSave;
CRect windowRect;
//Get main window context and create bitmap from it
mainWindow = AfxGetMainWnd();
mainWindowDC = mainWindow->GetWindowDC();
mainWindow->GetWindowRect(&windowRect);
bitmapToSave.CreateCompatibleBitmap(mainWindowDC, windowRect.Width(), windowRect.Height());
imageToSave.Attach(bitmapToSave);
imageToSave.Save("C:\Capture\image.bmp", Gdiplus::ImageFormatBMP);
这是这样做的方法:
HRESULT CaptureScreen(const CString& sImageFilePath)
{
CWnd* pMainWnd = AfxGetMainWnd();
CRect rc;
pMainWnd->GetWindowRect(rc);
CImage img;
img.Create(rc.Width(), rc.Height(), 24);
CDC memdc;
CDC* pDC = pMainWnd->GetWindowDC();
memdc.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = memdc.SelectObject(CBitmap::FromHandle((HBITMAP)img));
memdc.BitBlt(0, 0, rc.Width(), rc.Height(), pDC, 0, 0, SRCCOPY);
memdc.SelectObject(pOldBitmap);
return img.Save(sImageFilePath, Gdiplus::ImageFormatPNG);
}
也请看一下这个不错的实现:http://www.codeguru.com/cpp/article.php/c18347/C-Programming-Easy-Screen-Capture-Using-MFCATL.htm
您创建了位图,但没有在其中添加任何内容。您需要从一个 DC 到另一个 DC 进行 bling 才能复制屏幕上的内容。
// ...
CMemDC dcMem;
dcMem.CreateCompatibleDC(&mainWindowDC);
CBitmap * pOld = dcMem.SelectObject(&bitmapToSave);
dcMem.BitBlt(0, 0, windowRect.Width(), windowRect.Height(), &mainWindowDC, windowRect.left, windowRect.top, SRCCOPY);
dcMem.SelectObject(pOld);
// ...
相关文章:
- 具有 GPU 时间表的卤化物产生黑色图像
- openCV中的Absdiff可以编译,但显示黑色图像
- 在图像上的矩形区域之外填充黑色
- 具有SPI_SETDESKWALLPAPER功能的程序仅在尝试使用C++将其更改为图像时将桌面背景更改为黑色
- OpenCv 3.3 Cuda 中值过滤器生成 2/3 的图像黑色
- OPENCV :: CUDA GPUMAT CV_8UC1至CV_32FC1转换黑色图像
- OpenCV,Dlib - 作为黑色图像输出的垫子对象
- 从C++中的屏幕选择创建位图时的黑色图像
- 屏幕截图仅返回黑色图像
- 在C++中,在图像周围填充黑色边框是最有效的方法
- 在opencv中使用imwrite保存图像时,会将图像全部写入黑色,但imshow会正确显示
- 从HWND获取并保存位图(每个像素8,4,1位-黑色图像)
- OpenCV Sobel过滤器导致几乎完全黑色的图像
- OpenGL 窗口的捕获图像在 Win7 下为黑色
- OpenCV 项目在将直方图应用于黑色图像时崩溃
- 黑色背景,图像在中心使用 GDI
- 如果新图像中的原始像素为黑色,则Qt绘制一个蓝色像素
- SDL产生黑色图像
- 我正在尝试将灰度转换为更大的黑色图像.我得到这个错误OpenCV错误:断言失败
- 在黑色图像上叠加精明的边缘