c# - Sending screenshots taking too much memory -


i trying make small application serve screenshot of entire screen through network. want able serve 1 every 1-2 seconds through lan. thought won't big problem, chose c# , nancy www self host (as easiest option). now, speed allright needs, seems taking way memory, , grows time. seems settle on taking 3.5 gb ram , no longer grows, that's not acceptable amount - big resolution (mine 2560x1440).

is code bad, or nancy not suitable handling many big responses, or maybe c# method of capturing screen poorly optimized , should try pinvoke methods? or maybe it's terrible way , should try more advanced, using vnc library?

currently code looks this:

public class homemodule : nancymodule {     private bitmap bitmapscreencapture;     private graphics graphics;     private object lockme = new object();     private memorystream memorystream = new memorystream();      public homemodule()     {         get["image"] = parameters =>         {             lock (lockme)             {                 gc.collect();                  if (bitmapscreencapture == null || bitmapscreencapture.width != screen.primaryscreen.bounds.width || bitmapscreencapture.height != screen.primaryscreen.bounds.height)                 {                     if (bitmapscreencapture != null)                     {                         bitmapscreencapture.dispose();                     }                     bitmapscreencapture = new bitmap(screen.primaryscreen.bounds.width, screen.primaryscreen.bounds.height);                     graphics = graphics.fromimage(bitmapscreencapture);                 }                  graphics.copyfromscreen(screen.primaryscreen.bounds.x,                     screen.primaryscreen.bounds.y,                     0, 0,                     bitmapscreencapture.size,                     copypixeloperation.sourcecopy);                  bitmapscreencapture.save(memorystream, imageformat.png);                 memorystream.position = 0;                  return new response()                 {                     contents = stream => memorystream.copyto(stream)                 };             }         };     } } 

as possible, keep variables in local scope possible, , dispose of can.

part of issue may you're new'ing graphics instance repeatedly, never disposing of old reference. gc collect eventually, can place code in using block let know you're done it.

i haven't tested this, here i've made instances local, , disposed of graphics , bitmap instances. didn't dispose of memorystream since i'm not sure return value if do, play around it.

var screen = screen.primaryscreen;  using (var bitmap = new bitmap(screen.bounds.width, screen.bounds.height)) {     using (var g = graphics.fromimage(bitmap))     {         g.copyfromscreen(screen.bounds.left, screen.bounds.top, 0, 0, screen.bounds.size);     }      var imagestream = new memorystream();     bitmap.save(imagestream, imageformat.png);     imagestream.position = 0;      return new response()     {         contents = stream => memorystream.copyto(stream)     }; } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -