截取屏幕截图后程序卡住
Program gets stuck after taking screenshot
#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);
或者,将GdiplusStartup
和GdiplusShutdown
放在主函数中:
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);
相关文章:
- Winapi:屏幕截图未显示在窗口中
- 截取屏幕截图后程序卡住
- C++鼠标单击时的屏幕截图不起作用
- GTKMM:拍摄绘图区域的屏幕截图?
- 有没有办法使用 ofstream 保存屏幕截图?
- 如何使用 DX11 截取屏幕截图?
- avcodec_receive_packet错误(gdi 屏幕截图 + ffmpeg)
- C++屏幕截图 - 如何读取位图?
- 使用QT C 屏幕截图
- 如何将屏幕截图位图转换为 cv::Mat
- QT自动保存使用命名模板的屏幕截图
- C 屏幕截图
- BMP的完整屏幕截图.与块状和保存有关的问题
- 使用OpenCV和C++在按键上截取网络摄像头源的屏幕截图
- DirectX 部分屏幕截图
- 截取屏幕截图,分析它,然后删除它
- 为什么DirectX全屏应用程序会提供黑色屏幕截图
- 如何防止我的屏幕截图制作控制台程序阻止屏幕截图
- 如何用程序(c++)捕获Windows Phone 8.1的屏幕截图(全屏)
- 在c++ (Win32)中快速获取Windows应用程序的屏幕截图