【问题标题】:C: take screenshotC:截图
【发布时间】:2010-07-30 10:42:36
【问题描述】:

如何捕获屏幕并将其保存为 C 中的图像?
操作系统:windows(XP 和 7)

谢谢

【问题讨论】:

  • 这有点含糊,很大程度上取决于底层操作系统。在任何情况下,您都必须通过操作系统附带的 API,获取原始像素数据并将其保存为图像 - 对于最后一部分,您可能希望使用现有库,这样您就不会重新发明轮子。
  • 请考虑向此问题添加信息。操作系统?图形环境?可用的库?
  • 您好,感谢您的回复,我更新了帖子。
  • 您可能希望使用BitBltGetDCGetDesktopWindow 的组合。

标签: c windows screenshot


【解决方案1】:

你试过google吗?这个forum entry 有一个示例,包含使用 Win32 API 的 C 源代码。

编辑:同时发现重复:How can I take a screenshot and save it as JPEG on Windows?

【讨论】:

    【解决方案2】:

    如果您不想费心点击链接

    #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");
    }
    

    【讨论】:

    • 嗨,这对我也不起作用!我以为我们在 C (?) 中没有“bool”数据类型。它会出现此错误:“在'SaveBMPFile'之前预期'=',',',';','asm'或'attribute'”和“ScreenCapture”功能的相同错误
    • 尝试以下操作: typedef char bool; #define true 1 #define false 0
    • 好的,对于追随者,我冒昧地在这里整理了一个可编译文件(包括缺少的 SaveBMPFile 方法):gist.github.com/rdp/9821698(@4r1y4n 你需要使用 g++ 编译)
    • 这个问题没有被标记为“c++”。
    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2011-03-08
    相关资源
    最近更新 更多