美文网首页
(C语言)屏幕截取程序

(C语言)屏幕截取程序

作者: 牛i蛋 | 来源:发表于2019-04-04 20:01 被阅读0次

    背景

    今天和大家分享一下C语言实现屏幕截取程序,也算是一个练手的小程序,这个代码直接编译就可以啦,已经调试完毕了。

    解决方案

    首先利用argv设置截屏后的图片的保存路径 运行程序,路径上会生成一个bmp图片文件
    那么屏幕就截取下来了,下面直接分享源码:

    源码

    #include <Windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    HANDLE      DDBtoDIB( HBITMAP bitmap, DWORD dwCompression, HPALETTE  hPal,DWORD * sizeimage) ;
    BOOL        CapScreen(LPTSTR FileName);
    
    HANDLE DDBtoDIB( HBITMAP bitmap, DWORD dwCompression, HPALETTE  hPal,DWORD * sizeimage) 
    {
        BITMAP              bm;
        BITMAPINFOHEADER    bi;
        LPBITMAPINFOHEADER  lpbi;
        DWORD               dwLen;
        HANDLE              hDib;
        HANDLE              handle;
        HDC                 hdc;
    
        if( dwCompression == BI_BITFIELDS )
            return NULL;
    
        if (hPal==NULL)
            hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE );
    
        GetObject(bitmap,sizeof(bm),(LPSTR)&bm);
    
        bi.biSize        = sizeof(BITMAPINFOHEADER);
        bi.biWidth        = bm.bmWidth;
        bi.biHeight         = bm.bmHeight;
        bi.biPlanes         = 1;
        bi.biBitCount        = bm.bmPlanes * bm.bmBitsPixel;
        bi.biCompression    = dwCompression;
        bi.biSizeImage        = 0;
        bi.biXPelsPerMeter    = 0;
        bi.biYPelsPerMeter    = 0;
        bi.biClrUsed        = 0;
        bi.biClrImportant    = 0;
    
        int ncolors = (1 << bi.biBitCount); if( ncolors> 256 ) 
            ncolors = 0;
        dwLen  = bi.biSize + ncolors * sizeof(RGBQUAD);
    
        hdc = GetDC(NULL);
        hPal = SelectPalette(hdc,hPal,FALSE);
        RealizePalette(hdc);
    
        hDib = GlobalAlloc(GMEM_FIXED,dwLen);
    
        if (!hDib){
            SelectPalette(hdc,hPal,FALSE);
            ReleaseDC(NULL,hdc);
            return NULL;
        }
    
        lpbi = (LPBITMAPINFOHEADER)hDib;
        *lpbi = bi;
    
        GetDIBits(hdc, bitmap, 0L, (DWORD)bi.biHeight,
            (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS );
    
        bi = *lpbi;
    
        if (bi.biSizeImage == 0){
            bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) 
                * bi.biHeight;
            if (dwCompression != BI_RGB)
                bi.biSizeImage = (bi.biSizeImage * 3) / 2;
        }
    
    
        dwLen += bi.biSizeImage;
        if (handle = GlobalReAlloc(hDib, dwLen, GMEM_MOVEABLE))
            hDib = handle;
        else{
            GlobalFree(hDib);
    
    
            SelectPalette(hdc,hPal,FALSE);
            ReleaseDC(NULL,hdc);
            return NULL;
        }
    
        lpbi = (LPBITMAPINFOHEADER)hDib;
    
        BOOL bgotbits = GetDIBits( hdc, bitmap,
            0L,                     
            (DWORD)bi.biHeight,      
            (LPBYTE)lpbi            
            + (bi.biSize + ncolors * sizeof(RGBQUAD)),
            (LPBITMAPINFO)lpbi,     
            (DWORD)DIB_RGB_COLORS);  
    
        if( !bgotbits )
        {
            GlobalFree(hDib);
    
            SelectPalette(hdc,hPal,FALSE);
            ReleaseDC(NULL,hdc);
            return NULL;
        }
    
        SelectPalette(hdc,hPal,FALSE);
        ReleaseDC(NULL,hdc);
        *sizeimage=bi.biSizeImage;
        return hDib;
    } 
    
    BOOL CapScreen(LPTSTR FileName)
    {
        DWORD sizeimage;
        HDC hdc = CreateDC("DISPLAY", NULL, NULL, NULL); 
        HDC CompatibleHDC = CreateCompatibleDC(hdc); 
        HBITMAP BmpScreen = CreateCompatibleBitmap(hdc,GetDeviceCaps(hdc, HORZRES),GetDeviceCaps(hdc, VERTRES)); 
        SelectObject(CompatibleHDC, BmpScreen);
        BitBlt(CompatibleHDC,0,0,GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES),hdc,0,0,SRCCOPY);
    
        HANDLE  pbitmapwithoutfileh=DDBtoDIB(BmpScreen, BI_RGB,0,&sizeimage);
        BITMAPFILEHEADER bfh;
    
        bfh.bfType = ((WORD)('M'<< 8)|'B');
        bfh.bfReserved1 = 0;
        bfh.bfReserved2 = 0;
        bfh.bfSize = 54+sizeimage;
        bfh.bfOffBits = 54;
    
        HANDLE  hFile=CreateFile(FileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
        DWORD dwWrite;
    
        WriteFile(hFile,&bfh,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);
    
        WriteFile(hFile,pbitmapwithoutfileh,bfh.bfSize,&dwWrite,NULL);  
        DeleteDC(hdc);
    
        CloseHandle(CompatibleHDC);
        return true;
    }
    
    int main(int argc,char * argv[])
    {
        BOOL  result;
        result = CapScreen(argv[1]);
        if(result == TRUE)
        {
            printf("Success!\n");
        }
        else
        {
            printf("Failed!\n");
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:(C语言)屏幕截取程序

          本文链接:https://www.haomeiwen.com/subject/abnqiqtx.html