windows截图保存为文件

用到MFC的版本:

      for(int i = 0;i < 30; ++i)
      {
          HDC hdcSrc = GetDC(NULL);
          int nBitPerPixel = GetDeviceCaps(hdcSrc,BITSPIXEL);
          int nWidth = GetDeviceCaps(hdcSrc,HORZRES);
          int nHeight = GetDeviceCaps(hdcSrc,VERTRES);
          CImage image;
          image.Create(nWidth,nHeight,nBitPerPixel);
          BitBlt(image.GetDC(),0,0,nWidth,nHeight,hdcSrc,0,0,SRCCOPY | CAPTUREBLT);
          ReleaseDC(NULL,hdcSrc);
          image.ReleaseDC();
          CString s;
          s.Format("file%d.png",i);
          image.Save(s,Gdiplus::ImageFormatPNG);
          Sleep(200);
      }

不用MFC的话,保存为bmp:

    for(int i = 0;i < 30; ++i)
    {
        HDC hDC = GetDC(NULL);
        HDC comHDC = CreateCompatibleDC(hDC);
        int width = GetDeviceCaps(hDC,HORZRES);
        int height = GetDeviceCaps(hDC,VERTRES);
        HBITMAP hBitmap = CreateCompatibleBitmap(hDC,width,height);
        SelectObject(comHDC,hBitmap);
        if(!BitBlt(comHDC,0,0,width,height,hDC,0,0,SRCCOPY | CAPTUREBLT))
        {
            AfxMessageBox("BitBlt failed.");
            continue;
        }

        BITMAPINFOHEADER bmiHeader;
        bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bmiHeader.biWidth = width;
        bmiHeader.biHeight = height;
        bmiHeader.biPlanes = 1;
        bmiHeader.biBitCount = 16;
        bmiHeader.biCompression = BI_RGB;
        bmiHeader.biSizeImage = 0;
        bmiHeader.biXPelsPerMeter = 0;
        bmiHeader.biYPelsPerMeter = 0;
        bmiHeader.biClrImportant = 0;
        bmiHeader.biClrUsed = 0;

        DWORD dwBmpSize = ((width * bmiHeader.biBitCount + 31) / 32) * 4 * height;
        DWORD dwSizeofBIB = dwBmpSize + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
        BITMAPFILEHEADER bmfHeader;
        bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
        bmfHeader.bfSize = dwSizeofBIB;
        bmfHeader.bfType = 0x4D42;

        char * lpbitmap = new char[dwBmpSize];
        ZeroMemory(lpbitmap,dwBmpSize);
        GetDIBits(comHDC,hBitmap,0,(UINT)height,lpbitmap,(BITMAPINFO*)&bmiHeader,DIB_RGB_COLORS);

        char s[64] = {0};
        sprintf_s(s,"file%d.bmp",i);
        FILE * f = fopen(s,"wb+");
        if(f)
        {
            fwrite((char*)&bmfHeader,sizeof(BITMAPFILEHEADER),1,f);
            fwrite((char*)&bmiHeader,sizeof(BITMAPINFOHEADER),1,f);
            fwrite(lpbitmap,dwBmpSize,1,f);
            fclose(f);
        }
        Sleep(200);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值