Raysefo

Raysefo

  • 1.5k
  • 284
  • 150.2k

CopyFromScreen increases Memory Usage

Mar 9 2012 2:49 AM
Hi,

I have a windows forms app running on a remote server. When I first start this app, the memory usage on task manager was 9876 KB.This app copies screen shots for every 1 minute. When it starts copying screen shots, the memory usage increases to 23993 KB. How can I avoid memory usage? It seems like memory usage will increase much more.

Best Regards.

Here is my code sample:
... 
//Get Memory Usage             int memKB = (int)System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1024;               Microsoft.Win32.SystemEvents.SessionSwitch += new 
                Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);             if (flag)             {                 String time1 = DateTime.Now.ToString("HH:mm:ss");                 Bitmap b = new Bitmap(ScreenWidth, ScreenHeight);                 g = Graphics.FromImage(b);                 g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);                 time1 = time1.Replace(':''_');                 if (flag_Credentials)                 {                     IntPtr token = IntPtr.Zero;                     bool isSuccess = LogonUser("User""Domain""Pass", LOGON32_LOGON_NEW_CREDENTIALS, 
                                        LOGON32_PROVIDER_DEFAULT, ref token);                     if (isSuccess)                     {                         using (WindowsImpersonationContext person = new WindowsIdentity(token).Impersonate())                         {                             b.Save(@"\\IP\logs\shot" + name + "_" + time1 + "__" + memKB.ToString() + ".jpg"
                                        System.Drawing.Imaging.ImageFormat.Jpeg);                                                          g.Dispose();                             person.Undo();                         }                     }                 }                 else                 {                     b.Save(@"\\IP\logs\shot" + name + "_" + time1 + "__" + memKB.ToString() + ".jpg"
                                        System.Drawing.Imaging.ImageFormat.Jpeg);

                                         g.Dispose();                 }             }
...

Answers (4)