본문 바로가기

Windows/MFC

일단 화면상에 있는 Windows를 BMP로 저장하는 루틴을

일단 화면상에 있는 Windows를 BMP로 저장하는 루틴을 보내드립니다.
이 루틴은 MS사이트와 Codeguruㅅ이트에서 가져왔습니다.

간단히

WriteWindowToDIB( LPTSTR szFile, CWnd *pWnd )
이 펑션을 호출하면 bmp파일로 저장이 됩니다.

그럼 도움이 되시기를

-----------------------------------

[code]
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
    BITMAPFILEHEADER    hdr;
    LPBITMAPINFOHEADER lpbi;

    if (!hDIB)
        return FALSE;

    CFile file;
    if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
        return FALSE;

    lpbi = (LPBITMAPINFOHEADER)hDIB;
    int nColors = 1 << lpbi->biBitCount;


    // Fill in the fields of the file header

    hdr.bfType     = ((WORD) ('M' << 8) | 'B');    // is always "BM"
    hdr.bfSize     = GlobalSize (hDIB) + sizeof( hdr );
    hdr.bfReserved1     = 0;
    hdr.bfReserved2     = 0;
    hdr.bfOffBits     = (DWORD) (sizeof( hdr ) + lpbi->biSize +
                        nColors * sizeof(RGBQUAD));

    // Write the file header
    file.Write( &hdr, sizeof(hdr) );

    // Write the DIB header and the bits
    file.Write( lpbi, GlobalSize(hDIB) );

    return TRUE;
}

BOOL WriteWindowToDIB( LPTSTR szFile, CWnd *pWnd )
{
    CBitmap     bitmap;
    CWindowDC dc(pWnd);
    CDC         memDC;
    CRect     rect;

    memDC.CreateCompatibleDC(&dc);
    pWnd->GetWindowRect(rect);
    bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );


    CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
    memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);


    // Create logical palette if device support a palette
    CPalette pal;
    if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
    {
        UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
        LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
        pLP->palVersion = 0x300;
        pLP->palNumEntries =
            GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );

        // Create the palette
        pal.CreatePalette( pLP );
        delete[] pLP;
    }
    memDC.SelectObject(pOldBitmap);

    // Convert the bitmap to a DIB
    HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );

    if( hDIB == NULL )
        return FALSE;

    // Write it to file
    WriteDIB( szFile, hDIB );


    // Free the memory allocated by DDBToDIB for the DIB
    GlobalFree( hDIB );
    return TRUE;
}
[/code]

'Windows > MFC' 카테고리의 다른 글

Caret  (0) 2013.10.02
word wrap  (0) 2013.10.02
레지스트리에 입력하기  (0) 2013.10.02
윈도우 종료(리부팅) ExitWindowsEx( EWX_REBOOT, 0);  (0) 2013.10.02
시피유 정보 얻기  (0) 2013.10.02