windows - PrintWindow bitmap differs from PrintScreen Key bitmap -


when capturing window manually print screen+alt key combo, following:

enter image description here

but if try programmatically using windows api, this:

enter image description here

why discrepancy? how first programmatically?

here code:

    [dllimport("user32.dll")]     [return: marshalas(unmanagedtype.bool)]     private static extern bool printwindow(intptr hwnd, intptr hdcblt, int nflags);      public bitmap printwindow()     {         bitmap bmp = new bitmap(windowrect.width, windowrect.height, pixelformat.format32bppargb);         graphics gfxbmp = graphics.fromimage(bmp);         intptr hdcbitmap = gfxbmp.gethdc();          bool success = printwindow(windowhandle, hdcbitmap, 0);         gfxbmp.releasehdc(hdcbitmap);          if (!success)         {             console.writeline("error copying image");             console.writeline(getlasterror());         }          gfxbmp.dispose();          return bmp;     } 

update: doing bitblt same thing.

here's code codeproject still returns black-masked image:

public image capturewindow(intptr handle) {     // te hdc of target window     intptr hdcsrc = user32.getwindowdc(handle);     // size     user32.rect windowrect = new user32.rect();     user32.getwindowrect(handle,ref windowrect);     int width = windowrect.right - windowrect.left;     int height = windowrect.bottom - windowrect.top;     // create device context can copy     intptr hdcdest = gdi32.createcompatibledc(hdcsrc);     // create bitmap can copy to,     // using getdevicecaps width/height     intptr hbitmap = gdi32.createcompatiblebitmap(hdcsrc,width,height);     // select bitmap object     intptr hold = gdi32.selectobject(hdcdest,hbitmap);     // bitblt on     gdi32.bitblt(hdcdest, 0, 0, width, height, hdcsrc, 0, 0, copypixeloperation.sourcecopy | copypixeloperation.captureblt);     // restore selection     gdi32.selectobject(hdcdest,hold);     // clean     gdi32.deletedc(hdcdest);     user32.releasedc(handle,hdcsrc);     // .net image object     image img = image.fromhbitmap(hbitmap);     // free bitmap object     gdi32.deleteobject(hbitmap);      img.save("sampleimage.png");     return img; } 

i have tried many combinations of copypixeloperation, (somewhere around 15,000 of 131,000) still doesn't work.

using windows 8, amd radeon hd 6870.


update 2

it seems window transparent, allowing blue color of window bleed through. when change window color black (using windows personalization dialog), similar second window. border still missing though.

i haven't found solution, it's insight problem.

the reason printwindow not work because depends on correct handling of wm_print message app. lot of apps flaky wm_print , don't implement right or ever test it. it's therefore bad idea rely on unless use on known , tested apps.

if want grab window shows on screen, blt desktop window handle (getdesktopwindow()) , blt rect includes window.

transparency issue window grabbing. impossible capture modern windows os' windows, because there no image file type supports fanciness blurring underlying imagery. however, it's possible capture simple transparency png file.

be careful assuming window looks best seen on screen. depending on what's underneath, may not true. may bad idea copy whatever's underneath may not sanctioned appear in image, explicit background image appearing in business presentation under screenshot of excel :).

if blt desktop, copy see on screen, including overlaying windows , underlying windows (where transparent). regarded "cleaner" grab window only, printwindow, may want composite on background of choice, white or blue. if want blt screen, there ways temporarily hide windows overlay target, it's bunch of work enumwindows , such.

grabbing windows correctly not easy task in windows , there many screen-grab apps compete each other virtue of how deal issue. also, in windows there several ways make make areas of windows transparent, , can used well.

also, in vista+, there's dwm thumbnail api allows copy of app window drawn on window. demo , source, see sharex (formerly zscreen).

finally, can source of open broadcaster software, uses direct3d screen grabbing.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -