代码之家  ›  专栏  ›  技术社区  ›  Ariyan

C:截图

  •  3
  • Ariyan  · 技术社区  · 16 年前

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

    谢谢

    2 回复  |  直到 16 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    你试过了吗 google ? 这个 forum entry

    编辑:同时发现一个副本: How can I take a screenshot and save it as JPEG on Windows?

        2
  •  3
  •   rogerdpack    12 年前

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