簡體   English   中英

使用C程序截取Windows桌面的屏幕快照。

[英]Taking a screenshot of the Windows desktop with a C program.

是否可以拍攝桌面的屏幕截圖,然后通過C編程將其另存為.jpg,jpeg,...在計算機中的任何位置? 我很好奇,是否有任何方法和方法可以完成此任務。 我正在使用Windows XP。

假設使用Visual Studio:
這是從工作代碼中復制並粘貼的內容(某些行被省略):

HDC     hdcScreen   = NULL;
HDC     hdcMemDC    = NULL;
HBITMAP     hbmScreen   = NULL;

CImage      myimage;
IStream*    pIStream    = NULL;
STATSTG     stg;
HGLOBAL     hGlobal     = NULL;
HRESULT     hResult     = 0;

UINT        nDataSize   = 0;

do
{
    //
    //  Get the screen capture in an HBITMAP.
    //  -------------------------------------
    // Retrieve the handle to a display device context for the client 
    // area of the window. 
    hdcScreen = ::GetDC(NULL);
    if ( hdcScreen == NULL )
    {
        SET_CHECKPOINT();
        break;
    }

    // Create a compatible DC which is used in a BitBlt from the window DC
    hdcMemDC = CreateCompatibleDC(hdcScreen); 
    if ( hdcMemDC == NULL )
    {
        SET_CHECKPOINT();
        break;
    }

    // Get the client area for size calculation
    int cx = GetSystemMetrics(SM_CXSCREEN);
    int cy = GetSystemMetrics(SM_CYSCREEN);

    // Create a compatible bitmap from the Window DC
    hbmScreen = CreateCompatibleBitmap(hdcScreen, cx, cy);
    if ( hbmScreen == NULL )
    {
        SET_CHECKPOINT();
        break;
    }

    // Select the compatible bitmap into the compatible memory DC.
    SelectObject(hdcMemDC,hbmScreen);

    // Bit block transfer into our compatible memory DC.
    BitBlt(hdcMemDC, 0,0, cx, cy, hdcScreen, 0,0, SRCCOPY);

    // Create a stream to have CImage write data to.
    hResult = CreateStreamOnHGlobal(NULL, TRUE, &pIStream);
    if ( hResult != S_OK )
    {
        SET_CHECKPOINT();
        break;
    }

    // Attach an ATL CImage to the hbitmap.
    myimage.Attach(hbmScreen);

    // Write data to stream.
    hResult = myimage.Save(pIStream, Gdiplus::ImageFormatJPEG);
    if ( hResult != S_OK )
    {
        SET_CHECKPOINT();
        break;
    }
    myimage.Detach();

    // Get the stream's HGLOBAL.
    hResult = GetHGlobalFromStream(pIStream, &hGlobal);
    if ( hResult != S_OK )
    {
        SET_CHECKPOINT();
        break;
    }

    // Get a pointer to the data in the HGLOBAL.
    char* pJPGData = (char*)GlobalLock(hGlobal);
    pIStream->Stat(&stg, STATFLAG_NONAME);
    nDataSize = (UINT)stg.cbSize.QuadPart;

    // TODO: Open a file instead of a pipe...

    //pJPGData points to the data, nDataSize is, well...
    if ( WriteFile(hFile, pJPGData, nDataSize, &dwBytesWritten, NULL) == FALSE )
    {
        SET_CHECKPOINT();
        break;
    }

    // TODO: Close the file.
}
while (0,0);

//
//  Free resources.
//  ---------------
if ( pIStream != NULL )                 pIStream->Release();

//Clean up
if ( hbmScreen ) DeleteObject(hbmScreen);
if ( hdcMemDC  ) DeleteObject(hdcMemDC);
if ( hdcScreen ) ::ReleaseDC(NULL,hdcScreen);   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM