Gdi+;拍摄多个监视器的屏幕截图 我有一个程序,在GDI PLUS和C++上在Windows平台上进行截屏,它只对一个监视器进行完美的操作,但是当我在一台机器上运行它,它有2个监视器,它只拍摄图片一张。

Gdi+;拍摄多个监视器的屏幕截图 我有一个程序,在GDI PLUS和C++上在Windows平台上进行截屏,它只对一个监视器进行完美的操作,但是当我在一台机器上运行它,它有2个监视器,它只拍摄图片一张。,c++,screenshot,gdi+,gdi,C++,Screenshot,Gdi+,Gdi,这就是我正在做的: #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,

这就是我正在做的:

#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);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“gdiplus.lib”)
#pragma注释(lib,“ws2_32.lib”)
使用名称空间std;
//这是一种结构形式,是一种决策形式
//在图片中,我看到了我的照片
//这是我的梦想。
int GetEncoderClsid(常量WCHAR*格式,CLSID*pClsid)
{
使用名称空间Gdiplus;
UINT num=0;//图像编码器的数量
UINT size=0;//图像编码器数组的大小(字节)
ImageCodecInfo*pImageCodecInfo=NULL;
GetImageEncodersSize(&num,&size);
如果(大小==0)
返回-1;//失败
pImageCodecInfo=(ImageCodecInfo*)(malloc(size));
如果(pImageCodecInfo==NULL)
返回-1;//失败
GetImageEncoders(num、size、pImageCodecInfo);
对于(UINT j=0;j

如果我试图得到整个屏幕,但我最终得到的只是图片。有人能看出我的错误在哪里吗???

可能的解决方案1

使用
GetDC(0)
您将只获得主监视器的HDC。来自MSDN

如果系统上有多个监视器,则调用
CreateDC(文本(“显示”),NULL,NULL,NULL)
将创建一个覆盖所有 监视器

它可能会奏效,但还没有经过测试

可能的解决方案2


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

可能的解决方案1

使用
GetDC(0)
您将只获得主监视器的HDC。来自MSDN

如果系统上有多个监视器,则调用
CreateDC(文本(“显示”),NULL,NULL,NULL)
将创建一个覆盖所有 监视器

它可能会奏效,但还没有经过测试

可能的解决方案2

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

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

改用
SM\u CYVIRTUALSCREEN
。X也一样

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


改用
SM\u CYVIRTUALSCREEN
。与X相同。

图片是否只有一个显示器的大小?或者它更大,但是其他显示器应该在黑色区域?是一个显示器的大小还是图片只有一个显示器的大小?或者它更大,但是其他监视器应该在黑色区域?它只有一个监视器的大小“CreateDC(文本(“显示”),NULL,NULL,NULL)”不起作用,我仍然只有一个监视器。“CreateDC(文本(“显示”),NULL,NULL,NULL)”不起作用,我仍然只有一个监视器。
    int Height = GetSystemMetrics(SM_CYSCREEN);