如何将屏幕截图保存为PNG格式?

4
我在网上找到了这段代码可以打印屏幕(截图),但我不知道如何修改它以将结果保存到PNG文件中。
我现在可以将位图保存到剪贴板,但我现在需要保存为PNG文件。
  • 是否可以从剪贴板中提取位图并将其保存为PNG文件?
  • 还有其他的方法实现吗?
  • 如果有,怎么做?
到目前为止,我的代码是:
#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);
}

链接 - https://causeyourestuck.io/2016/01/12/screenshot-c-win32-api/ 作者Omar AFLAK

这是一篇关于使用C++ Win32 API进行屏幕截图的文章。通过使用该API,可以实现从桌面或活动窗口中获取屏幕截图的功能。本文提供了完整的代码示例,并对其中的关键部分进行了详细解释。

你可以停止重复说你想将东西保存为PNG文件并需要帮助。你已经说了十遍了,再说一遍也不会让你的问题更加明确! - Marcus Müller
我重新打开了这个问题,因为它比原来的版本好多了,但你仍然需要为你在网上找到的代码提供归属。网站链接和作者姓名是最基本的要求。 - Cody Gray
2
关于你的问题,你可以在这里找到答案,尽管那个问题并不是这个问题的重复。如果你提供了适当的编码器类ID,GDI+可以直接写入PNG文件。另请参阅:https://dev59.com/u2435IYBdhLWcg3wjw3S - Cody Gray
1个回答

6

首先,需要包含Gdiplus库。在Visual Studio中,您可以使用#pragma关键字或在项目设置中添加Gdiplus.lib。

接下来,使用Gdiplus::GdiplusStartup初始化Gdiplus。

像之前注意到的那样查找编码器CLSID

在您的代码中,您有abs(b.x-a.y),大概应该是abs(b.x-a.x)。如果a应该是起点,这个逻辑可能不起作用。只需确保POINT b大于POINT a。或者仔细阅读BitBlt的文档。

#include <Windows.h>
#include "gdiplus.h"

//Visual Studio shortcut for adding library:
#pragma comment(lib, "Gdiplus.lib")

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

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

    Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::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 -1;  // Failure
}

void screenshot(POINT a, POINT b)
{
    int w = b.x - a.x;
    int h = b.y - a.y;

    if(w <= 0) return;
    if(h <= 0) return;

    HDC     hScreen = GetDC(HWND_DESKTOP);
    HDC     hDc = CreateCompatibleDC(hScreen);
    HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h);
    HGDIOBJ old_obj = SelectObject(hDc, hBitmap);
    BitBlt(hDc, 0, 0, w, h, hScreen, a.x, a.y, SRCCOPY);

    Gdiplus::Bitmap bitmap(hBitmap, NULL);
    CLSID clsid;

    GetEncoderClsid(L"image/png", &clsid);

    //GDI+ expects Unicode filenames
    bitmap.Save(L"c:\\test\\test.png", &clsid);

    SelectObject(hDc, old_obj);
    DeleteDC(hDc);
    ReleaseDC(HWND_DESKTOP, hScreen);
    DeleteObject(hBitmap);
}

int main()
{
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    RECT      rc;
    GetClientRect(GetDesktopWindow(), &rc);
    POINT a{ 0, 0 };
    POINT b{ 100, 100 };

    screenshot(a, b);

    Gdiplus::GdiplusShutdown(gdiplusToken);

    return 0;
}

1
抛出c2731错误:“winmain”函数不能被重载。如何修复? - F.Fipoo
1
你之前展示了代码中的 main 入口点。如果你将 mainwWinMainWinMain 混合使用,会出现错误。GDI+ 函数需要 Unicode 输入/输出进行文本传输。 - Barmak Shemirani
根据Barmak Shemirani的暗示,如果您使用Visual Studio,则需要转到项目设置并更改“常规->项目默认值->字符集”为“使用Unicode字符集”,这样就可以修复编译错误。它默认为“使用多字节字符集”。 - kayleeFrye_onDeck
1
@kayleeFrye_onDeck 我刚刚改成了 main,因为 wmain 是专门针对 Visual Studio 的。 - Barmak Shemirani
2
bitmap.Save(L"c:\test\test.png", &clsid, NULL); 的Mingw实现。 - Aikon Mogwai
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接