【问题标题】:Taking a screenshot of the Windows desktop with a C program.使用 C 程序截取 Windows 桌面的屏幕截图。
【发布时间】:2011-11-24 06:44:28
【问题描述】:

是否可以通过C编程将桌面截图并保存为.jpg,jpeg,...在计算机中的任何位置?我很想知道是否有任何方法和方法可以完成这项任务。我正在使用 Windows XP。

【问题讨论】:

  • 有许多可用的截图程序,适用于所有平台。如果您想自己编写,为什么不看看它们以及它们的作用?
  • @JoachimPileborg:我在搜索时获取的是软件而不是代码,所以你能推荐一些链接,其中包含用 C、C++、Java、c# 编写的示例代码。
  • 我认为这取决于操作系统。您使用哪种操作系统?

标签: c windows-xp


【解决方案1】:

假设 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);   

【讨论】:

  • #include 那些:
猜你喜欢
  • 1970-01-01
  • 2021-06-28
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多