截取屏幕截图后程序卡住

Program gets stuck after taking screenshot

本文关键字:程序 屏幕截图 截取      更新时间:2023-10-16
#include "screenshot.h"
#include "changewallpaper.h"
using namespace std;
int main()
{
screenshot();
changewallpaper();
}

我的截图((;

#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
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
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 -1;  // Failure
}
void screenshot()
{
// get the device context of the screen
HDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);
int width = GetDeviceCaps(hScreenDC, HORZRES);
int height = GetDeviceCaps(hScreenDC, VERTRES);
POINT a,b;
a.x=0;
a.y=0;
b.x=width;
b.y=height;
// 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);
//Initialize GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Gdiplus::Bitmap bitmap(hBitmap, NULL);
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bitmap.Save(L"D:\Pictures\screen.png", &pngClsid, NULL);
// clean up
SelectObject(hDC, old_obj);
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);
DeleteObject(hBitmap);
//delete image;
GdiplusShutdown(gdiplusToken);
}

我的问题是changewallpaper(;永远不会运行。如果我放置更改壁纸((;在屏幕截图之前((;在我的主要情况下,一切都有效,但如果我像上面那样拥有它,则不会。我需要我的程序在更改壁纸之前截取屏幕截图,所以我不能只是切换它们。有谁知道可能有什么问题?我毫无头绪。

@xsoftie,你挂在image::~image()里。

根据GdiplusShutdown

必须删除所有 GDI+ 对象(或让它们退出 范围(,然后再调用 GdiplusShutdown。

Gdiplus::Bitmap bitmap(hBitmap, NULL);

bitmap存储在堆栈上,系统在其生命周期结束时({} 之后(自动释放它们。 Scoftie的方法是一种可行的解决方案。

当然,您也可以使用new在堆上请求 GDI + 对象指针,如以下示例所示:

Gdiplus::Bitmap *bitmap = new Gdiplus::Bitmap(hBitmap, NULL);
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bitmap->Save(L"D:\Pictures\screen.png", &pngClsid, NULL);
//...
delete bitmap;
GdiplusShutdown(gdiplusToken);

或者,将GdiplusStartupGdiplusShutdown放在主函数中:

int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
screenshot();
changewallpaper();
GdiplusShutdown(gdiplusToken);
}

此外,当您不再需要hScreenDC时,调用DeleteDC函数。

一些变化。范围限定对于使位图超出范围并在自身之后进行清理是必要的。第二个是发布hScreenDC,尽管我会把它作为一个练习,让你看看是否有必要。你挂在图像::~图像功能中。让我知道它是否为您修复了它。

{
Gdiplus::Bitmap bitmap(hBitmap, NULL);
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bitmap.Save(L"c:\temp\screen.png", &pngClsid, NULL);
}
// clean up
SelectObject(hDC, old_obj);
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);
DeleteObject(hBitmap);
DeleteDC(hScreenDC);