Window Capture doesn't work right

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Hi, I have problem with Windows Capture.
All seems to work but sometimes I get no borders/titlebar on windows, sometimes the program only saves/shows parts of window.
Im using this code to save the image:

<div style="color:black; background-color:white
<pre><span style="color:blue void

<br/>

Saveit(HBITMAP hbmp, HDC hdc,<span style="color:blue const

<br/>

WCHAR *filename)<br/>

{<br/>

<span style="color:green // using namespace Gdiplus;

<br/>

<br/>

<span style="color:green // Initialize GDI+.

<br/>

<br/>

Gdiplus::GdiplusStartupInput gdiplusStartupInput;<br/>

ULONG_PTR gdiplusToken;<br/>

Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);<br/>

CLSID encoderClsid;<br/>

Gdiplus::Status stat;<br/>

RECT rcDt;<br/>

HWND hDtWnd = ::GetForegroundWindow();<br/>

<br/>

<br/>

<br/>

GetWindowRect(hDtWnd, &rcDt);<br/>

<br/>

<span style="color:green // Get the CLSID of the PNG encoder.

<br/>

<br/>

GetEncoderClsid(L<span style="color:#a31515 "image/png"

<br/>

, &encoderClsid);<br/>

<br/>

<span style="color:green // Get width and height of source HBITMAP

<br/>

<br/>

BITMAP bm;<br/>

memset((<span style="color:blue void

<br/>

*)&bm, 0, <span style="color:blue sizeof

<br/>

(BITMAP));<br/>

GetObject(hbmp, <span style="color:blue sizeof

<br/>

(BITMAP), (<span style="color:blue void

<br/>

*)&bm);<br/>

<span style="color:blue int

<br/>

width = bm.bmWidth;<br/>

<span style="color:blue int

<br/>

height = bm.bmHeight;<br/>

<br/>

<span style="color:green // Create a GDI+ bitmap of the same dimensions, with alpha.

<br/>

<br/>

Gdiplus::Bitmap* copy = <span style="color:blue new

<br/>

Gdiplus::Bitmap(width, height, PixelFormat32bppARGB);<br/>

copy = <span style="color:blue new

<br/>

Gdiplus::Bitmap(hbmp, NULL);<br/>

<br/>

<span style="color:green //Get an HDC for this new Bitmap

<br/>

<br/>

Gdiplus::Graphics* g = Gdiplus::Graphics::FromImage(copy);<br/>

<br/>

<br/>

HDC copyHdc = g->GetHDC();<br/>

<br/>

HDC srcHdc = ::CreateCompatibleDC(hdc);<br/>

::SelectObject(srcHdc, hbmp);<br/>

<br/>

<span style="color:green //This loses all alpha:

<br/>

<br/>

<span style="color:green // BOOL bbrv = ::BitBlt(copyHdc, 0, 0, width, height, srcHdc, 0,0, SRCCOPY);

<br/>

<br/>

<br/>

<span style="color:green //This retains some alpha (only where A == 0)

<br/>

<br/>

BLENDFUNCTION bf1;<br/>

bf1.BlendOp = AC_SRC_OVER;<br/>

bf1.BlendFlags = 0;<br/>

bf1.SourceConstantAlpha = 0xff;<br/>

bf1.AlphaFormat = AC_SRC_ALPHA;<br/>

<br/>

BOOL abrv = ::AlphaBlend(copyHdc,0,0 , width, height, srcHdc, 0 ,0, width, height, bf1);<br/>

<br/>

<br/>

<span style="color:green //

<br/>

<br/>

<br/>

::DeleteDC(srcHdc);<br/>

<br/>

g->ReleaseHDC(copyHdc);<br/>

<br/>

stat = copy->Save(filename, &encoderClsid , NULL);<br/>

<br/>

Gdiplus::GdiplusShutdown(gdiplusToken);<br/>

<br/>

} <br/>

[/code]

<div style="color:black; background-color:white
<pre><span style="color:blue void

<br/>

SaveScreen(HWND hwnd)<br/>

<br/>

{<br/>

<span style="color:blue using

<br/>

<span style="color:blue namespace

<br/>

Gdiplus;<br/>

Gdiplus::GdiplusStartupInput gdiplusStartupInput;<br/>

ULONG_PTR gdiplusToken;<br/>

Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);<br/>

CLSID encoderClsid;<br/>

Gdiplus::Status stat;<br/>

BITMAP bitmap;<br/>

<span style="color:blue if

<br/>

( hwnd )<br/>

<br/>

{<br/>

<br/>

HDC hdcScreen = GetWindowDC(hwnd);<br/>

<br/>

<br/>

<br/>

<span style="color:blue if

<br/>

( hdcScreen )<br/>

<br/>

{<br/>

<br/>

RECT rect;<br/>

<br/>

GetWindowRect(hwnd, &rect);<br/>

<br/>

<br/>

<br/>

<span style="color:blue int

<br/>

width = rect.right - rect.left;<br/>

<br/>

<span style="color:blue int

<br/>

height = rect.bottom - rect.top;<br/>

<br/>

<br/>

<br/>

HDC compat = CreateCompatibleDC(hdcScreen);<br/>

<br/>

<br/>

<br/>

<span style="color:blue if

<br/>

( compat )<br/>

<br/>

{<br/>

<br/>

HBITMAP BitmapHandle = CreateCompatibleBitmap(hdcScreen, width, height);<br/>

<br/>

<br/>

<br/>

<span style="color:blue if

<br/>

( BitmapHandle )<br/>

<br/>

{<br/>

<br/>

<span style="color:blue if

<br/>

( SelectObject(compat, BitmapHandle) )<br/>

<br/>

{<br/>

<br/>

<span style="color:blue if

<br/>

( BitBlt(compat, 0, 0, width, height, hdcScreen, 0, 0, SRCCOPY | CAPTUREBLT) ) <br/>

<br/>

{<br/>

<br/>

GetObject(BitmapHandle, <span style="color:blue sizeof

<br/>

(BITMAP), &bitmap);<br/>

Saveit(BitmapHandle, compat,L<span style="color:#a31515 "H:\MyPic\pic.png"

<br/>

);<br/>

<span style="color:green // Save bitmap

<br/>

<br/>

<br/>

}<br/>

<br/>

}<br/>

SelectObject(compat, BitmapHandle);<br/>

<br/>

<br/>

DeleteObject(BitmapHandle);<br/>

<br/>

}<br/>

<br/>

<br/>

<br/>

DeleteDC(compat);<br/>

<br/>

}<br/>

<br/>

<br/>

<br/>

ReleaseDC(hwnd, hdcScreen);<br/>

<br/>

}<br/>

<br/>

}<br/>

<br/>

}<br/>

<br/>

[/code]

what must i do how to fix it, what i did wrong???
<
"Writing in C or C++ is like running a chain saw with all the safety guards removed!"

View the full article
 
Back
Top