java - Capture entire window with winapi -
i using winapi capture window of opened software applications in java. function below captures window of software application , returns image.
public static bufferedimage capture(hwnd hwnd) { hdc hdcwindow = user32.instance.getdc(hwnd); hdc hdcmemdc = gdi32.instance.createcompatibledc(hdcwindow); rect bounds = new rect(); rect bounds1 = new rect(); user32extra.instance.getwindowrect(hwnd, bounds); user32extra.instance.getclientrect(hwnd, bounds1); int extragap = (bounds.right-bounds.left-bounds1.right); int width = bounds.right-bounds.left-extragap; int height = bounds.bottom-bounds.top-extragap ; hbitmap hbitmap = gdi32.instance.createcompatiblebitmap(hdcwindow, width, height); handle hold = gdi32.instance.selectobject(hdcmemdc, hbitmap); gdi32extra.instance.bitblt(hdcmemdc,0, 0, width, height, hdcwindow, bounds.left+bounds1.right-bounds.right+extragap, bounds.top+bounds1.bottom-bounds.bottom+extragap, wingdiextra.mergecopy); gdi32.instance.selectobject(hdcmemdc, hold); bitmapinfo bmi = new bitmapinfo(); bmi.bmiheader.biwidth = width; bmi.bmiheader.biheight = -height; bmi.bmiheader.biplanes = 1; bmi.bmiheader.bibitcount = 32; bmi.bmiheader.bicompression = wingdi.bi_rgb; memory buffer = new memory(width * height * 4); gdi32.instance.getdibits(hdcwindow, hbitmap, 0, height, buffer, bmi, wingdi.dib_rgb_colors); bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); image.setrgb(0, 0, width, height, buffer.getintarray(0, width * height), 0, width); system.out.println(gdi32.instance.deleteobject(hbitmap)); system.out.println(gdi32.instance.deleteobject(hdcmemdc)); system.out.println(user32.instance.releasedc(hwnd, hdcwindow)); return image; }
the image captured has errors like:-
this image of flash builder window eclipse title bar.
this image of chrome browser without window buttons.
this image of file browser eclipse title bar.
sometimes title bar not captured in image.
do have idea why, , how solve problem? wrong in code?
Comments
Post a Comment