{"id":987,"date":"2023-12-13T10:30:10","date_gmt":"2023-12-13T00:30:10","guid":{"rendered":"https:\/\/www.nickdu.com\/?p=987"},"modified":"2023-12-13T10:30:10","modified_gmt":"2023-12-13T00:30:10","slug":"multi-monitor-screenshots-using-winapi","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=987","title":{"rendered":"Multi-monitor Screenshots Using WinAPI"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>void GdiPlusScreenCapture(MONITORINFOEX info, IStream* istream)\n{\n    HDC DevC = CreateDC(nullptr, info.szDevice, nullptr, nullptr);\n    DWORD Width = info.rcMonitor.right - info.rcMonitor.left;\n    DWORD Height = info.rcMonitor.bottom - info.rcMonitor.top;\n\n    HDC CaptureDC = CreateCompatibleDC(DevC);\n    HBITMAP CaptureBitmap = CreateCompatibleBitmap(DevC, Width, Height);\n    HGDIOBJ old_obj = SelectObject(CaptureDC, CaptureBitmap);\n    BitBlt(CaptureDC, 0, 0, Width, Height, DevC, 0, 0, SRCCOPY | CAPTUREBLT);\n\n    Gdiplus::Bitmap bitmap(CaptureBitmap, nullptr);\n\n    CLSID pngClsid;\n    CLSIDFromString(L\"{557cf401-1a04-11d3-9a73-0000f81ef32e}\", &amp;pngClsid);\n\n    bitmap.Save(istream, &amp;pngClsid, nullptr);\n\n    SelectObject(CaptureDC, old_obj);\n    DeleteDC(CaptureDC);\n    ReleaseDC(nullptr, DevC);\n    DeleteObject(CaptureBitmap);\n    DeleteDC(DevC);\n}\n\nvoid SaveScreenCapture()\n{\n    std::vector&lt;MONITORINFOEX> monitorInfos;\n    GdiplusStartupInput gdiplusStartupInput;\n    ULONG_PTR gdiplusToken;\n    GdiplusStartup(&amp;gdiplusToken, &amp;gdiplusStartupInput, nullptr);\n\n    \/\/ get the bitmap handle to the bitmap screenshot\n    HDC hdc = GetDC(nullptr);\n    EnumDisplayMonitors(hdc, nullptr, &#91;](HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) -> BOOL {\n    std::vector&lt;MONITORINFOEX>* pMonData = reinterpret_cast&lt;std::vector&lt;MONITORINFOEX>*>(dwData);\n\t\tMONITORINFOEX info;\n\t\tinfo.cbSize = sizeof(MONITORINFOEX);\n\t\tGetMonitorInfo(hMonitor, &amp;info);\n\t\tpMonData->push_back(info);\n\t\treturn true;\n\t}, reinterpret_cast&lt;LPARAM>(&amp;monitorInfos));\n    \n    int index = 1;\n    for (auto&amp; monitorInfo : monitorInfos)\n    {\t\/\/Create an empty IStream:\n\tIStream* pIStream = nullptr;\n\tif (CreateStreamOnHGlobal(nullptr, true, &amp;pIStream) == S_OK)\n        {\n\n            GdiPlusScreenCapture(monitorInfo, pIStream);\n\t    HGLOBAL hg = nullptr;\n\t    GetHGlobalFromStream(pIStream, &amp;hg);\n\t    SIZE_T bufsize = GlobalSize(hg);\n\t    LPVOID ptr = GlobalLock(hg);\n            TCHAR jpegFile&#91;MAX_PATH + 1] = { 0 };\n\t    _stprintf_s(jpegFile, MAX_PATH, \"%d.jpg\", index);\n\t    \/\/write from memory to file for testing:\n\t    std::ofstream fout(jpegFile, std::ios::binary);\n\t    fout.write((char*)ptr, bufsize);\n\t    fout.close();\n\t    pIStream->Release();\n\t    GlobalUnlock(hg);\n        }\n    }\n\n    ReleaseDC(nullptr, hdc);\n    GdiplusShutdown(gdiplusToken);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2],"tags":[],"class_list":["post-987","post","type-post","status-publish","format-standard","hentry","category-cc","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=987"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/987\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}