windows C:截图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3370702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: take screenshot
提问by RYN
How can I capture screen and save it as am image in C?
OS: windows (XP & Seven)
如何捕获屏幕并将其保存为 C 中的图像?
操作系统:windows (XP & 7)
Thanks
谢谢
采纳答案by Greg S
Have you tried google? This forum entryhas an example, complete with C source code using the Win32 API.
你试过谷歌吗?此论坛条目有一个示例,其中包含使用 Win32 API 的 C 源代码。
EDIT: Found a duplicate in the meantime: How can I take a screenshot and save it as JPEG on Windows?
编辑:同时发现一个副本:如何在 Windows 上截取屏幕截图并将其另存为 JPEG?
回答by Mihir Mehta
in case you don't want to bother to click on link
如果您不想费心点击链接
#include <windows.h>
bool SaveBMPFile(char *filename, HBITMAP bitmap, HDC bitmapDC, int width, int height);
bool ScreenCapture(int x, int y, int width, int height, char *filename){
// get a DC compat. w/ the screen
HDC hDc = CreateCompatibleDC(0);
// make a bmp in memory to store the capture in
HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);
// join em up
SelectObject(hDc, hBmp);
// copy from the screen to my bitmap
BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);
// save my bitmap
bool ret = SaveBMPFile(filename, hBmp, hDc, width, height);
// free the bitmap memory
DeleteObject(hBmp);
return ret;
}
main(){
ScreenCapture(500, 200, 300, 300, "testScreenCap.bmp");
system("pause");
}