I found this code on the web to print the screen (take a screenshot), but I don't know how to modify it to save the results to a PNG file.
I can save a bitmap to the clipboard but I need save to PNG file now.
- Is it possible to extract a bitmap from the clipboard and save it as a PNG file?
- Can this be done differently?
- If so, how?
My code so far is:
#include <iostream>
#include <windows.h>
#include <gdiplus.h>
#include <stdexcept>
using namespace std;
using namespace Gdiplus;
using namespace Gdiplus::DllExports;
using std::runtime_error;
void screenshot(POINT a, POINT b)
{
HDC hScreen = GetDC(NULL);
HDC hDc = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, abs(b.x-a.x), abs(b.y-a.y));
HGDIOBJ old_obj = SelectObject(hDc, hBitmap);
BOOL bRet = BitBlt(hDc, 0, 0, abs(b.x-a.y), abs(b.y-a.y), hScreen, a.x, a.y, SRCCOPY);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
SelectObject(hDc, old_obj);
DeleteDC(hDc);
ReleaseDC(NULL, hScreen);
DeleteObject(hBitmap);
}
int main()
{
POINT a,b;
a.x=386;
a.y=749;
b.x=686;
b.y=1049;
screenshot(a,b);
}
Link - https://causeyourestuck.io/2016/01/12/screenshot-c-win32-api/ Author Omar AFLAK