c# - When capturing screenshots of the desktop, why am I getting an out of memory exception after a few minutes? -



c# - When capturing screenshots of the desktop, why am I getting an out of memory exception after a few minutes? -

this question has reply here:

out of memory capuring screenshots 2 answers

in form1 designer added timer , first set 1000ms changed 100ms , in 100ms after saving screenshots hard disk after minutes got exception out of memory.

then changed 500ms , 1 time again out of memory after few minutes.

i think when it's 1000ms it's ok.

this class screenshots:

using system; using system.runtime.interopservices; using system.drawing; using system.drawing.imaging; namespace windowsformsapplication1 { /// <summary> /// provides functions capture entire screen, or particular window, , save file. /// </summary> public class screencapture { /// <summary> /// creates image object containing screen shot of entire desktop /// </summary> /// <returns></returns> public image capturescreen() { homecoming capturewindow(user32.getdesktopwindow()); } /// <summary> /// creates image object containing screen shot of specific window /// </summary> /// <param name="handle">the handle window. (in windows forms, obtained handle property)</param> /// <returns></returns> 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 re-create intptr hdcdest = gdi32.createcompatibledc(hdcsrc); // create bitmap can re-create 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, gdi32.srccopy); // 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); homecoming img; } /// <summary> /// captures screen shot of specific window, , saves file /// </summary> /// <param name="handle"></param> /// <param name="filename"></param> /// <param name="format"></param> public void capturewindowtofile(intptr handle, string filename, imageformat format) { image img = capturewindow(handle); img.save(filename, format); } /// <summary> /// captures screen shot of entire desktop, , saves file /// </summary> /// <param name="filename"></param> /// <param name="format"></param> public void capturescreentofile(string filename, imageformat format) { image img = capturescreen(); img.save(filename, format); } /// <summary> /// helper class containing gdi32 api functions /// </summary> private class gdi32 { public const int srccopy = 0x00cc0020; // bitblt dwrop parameter [dllimport("gdi32.dll")] public static extern bool bitblt(intptr hobject, int nxdest, int nydest, int nwidth, int nheight, intptr hobjectsource, int nxsrc, int nysrc, int dwrop); [dllimport("gdi32.dll")] public static extern intptr createcompatiblebitmap(intptr hdc, int nwidth, int nheight); [dllimport("gdi32.dll")] public static extern intptr createcompatibledc(intptr hdc); [dllimport("gdi32.dll")] public static extern bool deletedc(intptr hdc); [dllimport("gdi32.dll")] public static extern bool deleteobject(intptr hobject); [dllimport("gdi32.dll")] public static extern intptr selectobject(intptr hdc, intptr hobject); } /// <summary> /// helper class containing user32 api functions /// </summary> private class user32 { [structlayout(layoutkind.sequential)] public struct rect { public int left; public int top; public int right; public int bottom; } [dllimport("user32.dll")] public static extern intptr getdesktopwindow(); [dllimport("user32.dll")] public static extern intptr getwindowdc(intptr hwnd); [dllimport("user32.dll")] public static extern intptr releasedc(intptr hwnd, intptr hdc); [dllimport("user32.dll")] public static extern intptr getwindowrect(intptr hwnd, ref rect rect); } } }

and how utilize in form1:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace windowsformsapplication1 { public partial class form1 : form { int count; screencapture sc; public form1() { initializecomponent(); count = 0; sc = new screencapture(); } private void form1_load(object sender, eventargs e) { } private void timer1_tick(object sender, eventargs e) { count++; sc.capturescreentofile(@"c:\temp\screens3\" + count.tostring("d6") + ".jpg", system.drawing.imaging.imageformat.jpeg); } private void button1_click(object sender, eventargs e) { timer1.enabled = true; } } }

is there way maintain saving screenshots @ 100ms or 500ms without getting out of memory ?

while did job @ cleaning unmanaged resources still need clean managed resources. image notorious holding on resources not set memory pressure level on gc. causes garbage collection not automatically happen "normal" classes.

try disposing of image objects:

public void capturewindowtofile(intptr handle, string filename, imageformat format) { using(image img = capturewindow(handle)) { img.save(filename, format); } } public void capturescreentofile(string filename, imageformat format) { using(image img = capturescreen()) { img.save(filename, format); } }

c# .net winforms

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -