c++ - Screen capture only returns a black image -
i'm trying take screen capture of main dialog in mfc application , save image file. tried every example find online , end same result: image file has correct dimensions (i tried dialogs other main 1 sure), black. recent solution using cbitmap class transfer main dialog handle cimage. here code:
cwnd* mainwindow; cdc* mainwindowdc; cbitmap bitmaptosave; cimage imagetosave; crect windowrect; //get main window context , create bitmap mainwindow = afxgetmainwnd(); mainwindowdc = mainwindow->getwindowdc(); mainwindow->getwindowrect(&windowrect); bitmaptosave.createcompatiblebitmap(mainwindowdc, windowrect.width(), windowrect.height()); imagetosave.attach(bitmaptosave); imagetosave.save("c:\\capture\\image.bmp", gdiplus::imageformatbmp);
here way it:
hresult capturescreen(const cstring& simagefilepath) { cwnd* pmainwnd = afxgetmainwnd(); crect rc; pmainwnd->getwindowrect(rc); cimage img; img.create(rc.width(), rc.height(), 24); cdc memdc; cdc* pdc = pmainwnd->getwindowdc(); memdc.createcompatibledc(pdc); cbitmap* poldbitmap = memdc.selectobject(cbitmap::fromhandle((hbitmap)img)); memdc.bitblt(0, 0, rc.width(), rc.height(), pdc, 0, 0, srccopy); memdc.selectobject(poldbitmap); return img.save(simagefilepath, gdiplus::imageformatpng); }
please take @ nice implementation: http://www.codeguru.com/cpp/article.php/c18347/c-programming-easy-screen-capture-using-mfcatl.htm
Comments
Post a Comment