windows截图保存为文件

2017-11-21 20:13:13来源:CSDN作者:yangyang031213人点击

分享

用到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);//ImageFormatJPEG          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);    }

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台