Как сохранить изображение из буфера обмена в файл в C ++ - PullRequest
0 голосов
/ 03 мая 2020

Итак, я сохранил изображение в своем буфере обмена, и теперь я хочу сохранить это изображение в файл с определенным именем. Код, который у меня есть, только снимает скриншот всего экрана и сохраняет его в буфер обмена. И мой последний шаг - сохранить это изображение в файл.

Это код, который у меня есть:

#include <iostream>
#include <windows.h>
#include "wtypes.h"

using namespace std;

// Get the horizontal and vertical screen sizes in pixel
void GetDesktopResolution(int& horizontal, int& vertical)
{
  RECT desktop;
  // Get a handle to the desktop window
  const HWND hDesktop = GetDesktopWindow();
  // Get the size of screen to the variable desktop
  GetWindowRect(hDesktop, &desktop);
  // The top left corner will have coordinates (0,0)
  // and the bottom right corner will have coordinates
  // (horizontal, vertical)
  horizontal = desktop.right;
  vertical = desktop.bottom;
}

void screenshot(POINT a, POINT b)
{
    // copy screen to bitmap
    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.x), abs(b.y-a.y), hScreen, a.x, a.y, SRCCOPY);

    // save bitmap to clipboard
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hBitmap);
    CloseClipboard();

    GetClipboardData(CF_BITMAP);
    HBITMAP hBitmap1 = (HBITMAP)GetClipboardData(CF_BITMAP);


    // clean up
    SelectObject(hDC, old_obj);
    DeleteDC(hDC);
    ReleaseDC(NULL, hScreen);
    DeleteObject(hBitmap);
}

int main() 
{
    POINT a,b;
    a.x=0;
    a.y=0;

   int vert, hor;
   GetDesktopResolution(hor,vert);

   b.x = hor;
   b.y = vert;

   screenshot(a,b);
}
...