c# - Take screenshot of multiple desktops of all visible applications and forms -


i'm working system has 4 outputs (monitors) e.g. 1280x1024 pixels each output. need screenshot of whole desktop , open applications on it.

i tried getdesktopwindow() (msdn) doesn't work properly. forms don't shown on captured picture.

i tried getdesktopwindow() function doesn't work properly.

of course not.

the getdesktopwindow function returns handle desktop window. doesn't have capturing image of window.

besides, desktop window not same thing "the entire screen". refers desktop window. see this article more information , can go wrong when abuse handle returned function.

i'm working system have 4 outputs (monitors) 1280x1024(e.g) each output. need screenshot whole desktop , open applications on it.

this relatively simple in .net framework using graphics.copyfromscreen method. don't need p/invoke!

the trick in case making sure pass appropriate dimensions. since have 4 monitors, passing dimensions of primary screen won't work. need pass dimensions of entire virtual screen, contains of monitors. retrieve querying systeminformation.virtualscreen property, returns bounds of virtual screen. documentation indicates, bounds of entire desktop on multiple monitor system.

sample code:

// determine size of "virtual screen", includes monitors. int screenleft   = systeminformation.virtualscreen.left; int screentop    = systeminformation.virtualscreen.top; int screenwidth  = systeminformation.virtualscreen.width; int screenheight = systeminformation.virtualscreen.height;  // create bitmap of appropriate size receive screenshot. using (bitmap bmp = new bitmap(screenwidth, screenheight)) {     // draw screenshot our bitmap.     using (graphics g = graphics.fromimage(bmp))     {         g.copyfromscreen(screenleft, screentop, 0, 0, bmp.size);     }      // bitmap here, save file:     bmp.save(savepath, imageformat.jpeg); } 

edit:

please check solution wpf application in thread not main thread. tried it. doesn't work!

hmm, didn't see wpf tag on question or mentioned anywhere in body.

no matter, though. code posted works fine in wpf application, long add appropriate references , using declarations. need system.windows.forms , system.drawing. there might more wpf-esque way of doing doesn't require dependency on these winforms assemblies, wouldn't know is.

it works on thread. there nothing here require ui thread.

yes, tested it. here full test code:

using system.windows; using system.windows.forms;   // requires reference assembly using system.drawing;         // requires reference assembly using system.drawing.imaging; using system.threading;  public partial class mainwindow : window {    public mainwindow()    {       initializecomponent();    }     private void button1_click(object sender, routedeventargs e)    {       // create new thread demonstration purposes.       thread thread = new thread(() =>       {          // determine size of "virtual screen", includes monitors.          int screenleft   = systeminformation.virtualscreen.left;     int screentop    = systeminformation.virtualscreen.top;     int screenwidth  = systeminformation.virtualscreen.width;     int screenheight = systeminformation.virtualscreen.height;           // create bitmap of appropriate size receive screenshot.          using (bitmap bmp = new bitmap(screenwidth, screenheight))          {             // draw screenshot our bitmap.             using (graphics g = graphics.fromimage(bmp))             {                g.copyfromscreen(screenleft, screentop, 0, 0, bmp.size);             }              // bitmap here, save file:             bmp.save("g:\\testimage.jpg", imageformat.jpeg);          }       });       thread.setapartmentstate(apartmentstate.sta);       thread.start();    } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -