bitmap - C# PrintWindow - GetHdc crashes after many iterations -


first question on here, if can improve posting feel free tell me :) i'm programming rather simple .net application in c# uses "printwindow" "user32.dll" in order take screenshots of other application if runs behind window.

i aim let program run endlessly / long period of time, encounter problem cannot solve.

at around 10.000 screenshots application crashes. here code used in console application reproduce bug , error comes it:

class program {     /* image if process running behind window ******************* */     [dllimport("user32.dll")]     public static extern bool printwindow(intptr hwnd, intptr hdcblt, uint nflags);     [dllimport("user32.dll")]     public static extern intptr getwindowdc(intptr hwnd);     /* ****************************************************************************** */      static void main(string[] args)     {         process process = returnprocess();         int counter = 0;          console.writeline(rotmg.tostring());          while (true)         {             bitmap bmptest = capturewindow(rotmg.mainwindowhandle);             bmptest.dispose();              counter++;             console.writeline(counter.tostring());         }     }      private static process returnprocess()     {         process[] processes = process.getprocessesbyname("desiredprocess");         return processes[0];     }      public static bitmap capturewindow(intptr hwnd)     {         rectangle rctform = system.drawing.rectangle.empty;         using (graphics grfx = graphics.fromhdc(getwindowdc(hwnd)))         {             rctform = rectangle.round(grfx.visibleclipbounds);         }         bitmap pimage = new bitmap(rctform.width, rctform.height);         graphics graphics = graphics.fromimage(pimage);         intptr hdc = graphics.gethdc();            try         {             printwindow(hwnd, hdc, (uint)0);         }                 {             graphics.releasehdc(hdc);             graphics.dispose();         }         return pimage;     } } 

intptr hdc = graphics.gethdc(); system.argumentexception: parameter not valid

in real application not supposed capture images fast, same error occurs after few hours.

i code important code here: https://codereview.stackexchange.com/questions/29364/capturing-and-taking-a-screenshot-of-a-window-in-a-loop

do have ditch printwindow project? rather stick way found far capture window in background.

all right! found problem, helps in future. of gdiview found application leaked "dc" objects. gdi refuses work if more 10.000 objects created (which should have looked in first place). dc not being deleted afterwards hides in following line:

using (graphics grfx = graphics.fromhdc(getwindowdc(hwnd))) 

if add following reference:

[dllimport("gdi32.dll")] static extern intptr deletedc(intptr hdc); 

and modify code this:

intptr windowdc = getwindowdc(hwnd); using (graphics grfx = graphics.fromhdc(windowdc)) {     rctform = rectangle.round(grfx.visibleclipbounds); } deletedc(windowdc); 

then dc object deleted correctly, program no longer exceeds 10.000 dc objects , not crash anymore.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -