0

I have a simple loop to creates a bitmap using CopyFromScreen :

static void Main(string[] args)
    {
        while(true)
        {
            Bitmap bitmap = new Bitmap(makeScreenshot());
            Thread.Sleep(1000);

            bitmap.Dispose();

        }            
    }

    public static Bitmap makeScreenshot()
    {
        Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

        Graphics gfxScreenshot = Graphics.FromImage(screenshot);

        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

        gfxScreenshot.Dispose();

        return screenshot;
    }

however every loop i can see it increasing in memory? if i modify the new Bitmap to this:

Bitmap bitmap = new Bitmap(1024,768);

the dispose seems to work as expected, so is the makeScreenshot holding the object somehow?

Thanks

Dwayne Dibbley
  • 355
  • 3
  • 20
  • 2
    Though this is not your problem most likely, look up every single method you are using, and if it results in an object that implements `IDisposing` then do your self a favour and use the `using` statement – TheGeneral Jul 11 '18 at 07:00
  • 1
    Also yes it will increase memory until it is garbage collected (assuming you're dispose right) – TheGeneral Jul 11 '18 at 07:02
  • 2
    You're making a Bitmap from a Bitmap, which I'm assuming copies it. Just set Bitmap bitmap = makeScreenshot(); – MineR Jul 11 '18 at 07:02
  • 3
    makeScreenshot is your problem, its creating a bitmap and you are using it in a constructor and not disposing it – TheGeneral Jul 11 '18 at 07:04
  • @MineR cheers buddy that was it, Thanks – Dwayne Dibbley Jul 11 '18 at 07:16

0 Answers0