.net - C# method CopyfFromScreen crashes after 200 uses? -


for program need method takes screenshot every half minute. googled , came method:

    public static bitmap capturescreen()     {         bitmap bmp = new bitmap(screen.primaryscreen.bounds.width, screen.primaryscreen.bounds.height,         system.drawing.imaging.pixelformat.format32bppargb);         graphics gfx = graphics.fromimage(bmp);         gfx.copyfromscreen(screen.primaryscreen.bounds.x, seen.primaryscreen.bounds.y, 0, 0,  screen.primaryscreen.bounds.size, copypixeloperation.sourcecopy);         return bmp;     } 

well works fine first 200 uses of method or so. function crashes @ copyfromscreen , says caused invalid argument exception. i'm confused why because parameters don't change.

could function has bug? if there alternatives take screenshot?

probably 2 separate failures dispose. both graphics , image / bitmap implement idisposable, "obvious" of 2 here:

using(graphics gfx = graphics.fromimage(bmp)) {     gfx.copyfromscreen(screen.primaryscreen.bounds.x,seen.primaryscreen.bounds.y,         0, 0,  screen.primaryscreen.bounds.size, copypixeloperation.sourcecopy); } return bmp; 

however: caller of method should also using result of capturescreen (to release bitmap's gdi+ handle), i.e.

using(var screen = capturescreen()) {     // work here } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -