1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

boost.GILを使ってWindowsで画面イメージを取り込み

Posted at

画面画像をキャプチャしてPNG形式で保存.そのときに,boost::gilを使用した例.

/// 画面をキャプチャする
void CPY_CaptureScreen(const std::wstring &szFilePath) {

    auto pDlg = GetMainDlg(); // 窓(CWnd*)を得る

    CRect rectClient;
    pDlg->GetClientRect( &rectClient );

    CClientDC dc(pDlg);

    CDC dc2;
    dc2.CreateCompatibleDC( &dc );

    CBitmap bitmap;
    bitmap.CreateCompatibleBitmap(&dc, rectClient.Width(), rectClient.Height() );
    
    auto pOldBitmap = dc2.SelectObject( &bitmap );

    dc2.BitBlt(0,0, rectClient.Width(), rectClient.Height(), &dc, 0, 0, SRCCOPY );

    int nWidthAdjust = rectClient.Width();
    if ( nWidthAdjust % 4 ) { nWidthAdjust += 4 - nWidthAdjust % 4;}

    std::vector<BYTE> bitdata( nWidthAdjust * rectClient.Height() * 3 );
    
    dc2.SelectObject( pOldBitmap );

    BITMAPINFO bitmapinfo;
    ZeroMemory(&bitmapinfo, sizeof(BITMAPINFO) );
    bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bitmapinfo.bmiHeader.biWidth = nWidthAdjust;
    bitmapinfo.bmiHeader.biHeight = rectClient.Height();
    bitmapinfo.bmiHeader.biPlanes = 1;
    bitmapinfo.bmiHeader.biBitCount = 24;
    bitmapinfo.bmiHeader.biCompression = BI_RGB;
    GetDIBits( dc2, bitmap, 0, rectClient.Height(), bitdata.data(), &bitmapinfo, DIB_RGB_COLORS );


    boost::gil::rgb8_image_t img( nWidthAdjust, rectClient.Height() );
    copy_pixels(boost::gil::interleaved_view(nWidthAdjust, rectClient.Height(), (const boost::gil::rgb8_pixel_t*)bitdata.data(), nWidthAdjust*3), view(img));
    png_write_view(CStringA(szFilePath.c_str()), boost::gil::flipped_up_down_view( const_view(img) ) );
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?