【问题标题】:Gdi+ Take Screenshot multiple monitorsGdi+ 多台显示器截图
【发布时间】:2016-03-30 10:20:19
【问题描述】:

我有一个使用 gdi plus 和 c++ 在 Windows 平台上截取屏幕截图的例程,它只与一个监视器完美配合,但是当我在一台机器或带有 2 个监视器的 VM 中运行它时,它只拍摄一张照片。

这就是我正在做的:

#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <GdiPlus.h>
#include <wingdi.h>
#include <fstream>
#include <unistd.h>

#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "ws2_32.lib")

using namespace std;

// Se encarga de configurar, por asi decirlo, el formato
// de la imagen, este metodo es llamado en gdiscreen al
// momento de guardar la imagen.

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
  using namespace Gdiplus;
  UINT  num = 0;          // number of image encoders
  UINT  size = 0;         // size of the image encoder array in bytes

  ImageCodecInfo* pImageCodecInfo = NULL;

  GetImageEncodersSize(&num, &size);
  if(size == 0)
    return -1;  // Failure

  pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  if(pImageCodecInfo == NULL)
    return -1;  // Failure

  GetImageEncoders(num, size, pImageCodecInfo);

  for(UINT j = 0; j < num; ++j)
  {
    if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
    {
      *pClsid = pImageCodecInfo[j].Clsid;
      free(pImageCodecInfo);
      return j;  // Success
    }
  }

  free(pImageCodecInfo);
  return 0;
}

// Este es el metodo que tomo la captura.
// c es solo una variable que utilice para probar el
// metodo en un loop, la puede quitar y remplazar c
// por el log_sec_num.

inline void take_screenshot(const std::string & filename)
{
    using namespace Gdiplus;
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    {
        HDC scrdc, memdc;
        HBITMAP membit;
        scrdc = ::GetDC(0);
        int Height = GetSystemMetrics(SM_CYSCREEN);
        int Width = GetSystemMetrics(SM_CXSCREEN);
        memdc = CreateCompatibleDC(scrdc);
        membit = CreateCompatibleBitmap(scrdc, Width, Height);
        HBITMAP hOldBitmap =(HBITMAP) SelectObject(memdc, membit);
        BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);

        Gdiplus::Bitmap bitmap(membit, NULL);
        CLSID clsid;
        const char* name = filename.c_str();
        const size_t cSize = strlen(name)+1;
        wchar_t* wc = new wchar_t[cSize];
        mbstowcs (wc, name, cSize);
        GetEncoderClsid(L"image/png", &clsid);
        bitmap.Save(wc, &clsid,NULL);

        SelectObject(memdc, hOldBitmap);

        DeleteObject(memdc);

        DeleteObject(membit);

        ::ReleaseDC(0,scrdc);
    }

    GdiplusShutdown(gdiplusToken);
}

int main ()
{
    const std::string & filename = "C:\\Screenshot.png";
    take_screenshot(filename);
}

如果尝试获取整个屏幕,但我最终只获得了图片。谁能看到我的错误在哪里??

【问题讨论】:

  • 图片只有一台显示器的大小吗?还是更大,但其他显示器应该在的地方有黑色区域?
  • 这是一台显示器的大小

标签: c++ screenshot gdi+ gdi


【解决方案1】:
    int Height = GetSystemMetrics(SM_CYSCREEN);

引用 MSDN 文档:“主显示器屏幕的高度”。你得到了正是你所要求的,主显示器是你的显示器之一。第一个。

请改用SM_CYVIRTUALSCREEN。 X 也一样。

【讨论】:

    【解决方案2】:

    可能的解决方案 1

    使用GetDC(0),您将仅获得主监视器的 HDC。来自CreateDCMSDN

    如果系统上有多个监视器,则调用 CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL) 将创建一个覆盖所有 监视器。

    它可能会奏效,但尚未经过测试。

    可能的解决方案 2

    分别捕获每个桌面,然后将它们合并。要获取所有显示,请使用EnumDisplayMonitors。一些工作示例在一些代码项目的article 中。

    【讨论】:

    • 'CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL)' 不起作用,我仍然只有一台显示器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2011-12-11
    相关资源
    最近更新 更多