0

Relevant Code:

private Bitmap GetScreenShot()
    {
        Bitmap screenImage = null;

        using (screenImage = new Bitmap(panelPreview.Width, panelPreview.Height))
        {
            using (Graphics g = Graphics.FromImage(screenImage))
            {                    
                Rectangle rectanglePanelVideoPreview = panelPreview.Bounds;
                Point sourcePoints = panelPreview.PointToScreen(new Point(panelPreview.ClientRectangle.X, panelPreview.ClientRectangle.Y));
                g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size);
            }                
        }

        return screenImage;
    }

Bitmap screenImage throws an exception as I step through the code and get to

return screenImage

Screenshot

For some reason the integrity of the Bitmap fails after it leaves

using (screenImage = new Bitmap(panelPreview.Width, panelPreview.Height))
{...
}

Any assistance at all would be appreciated, thanks.

Pearcy
  • 13
  • 6
  • Why not reuse existing samples such as [so](http://stackoverflow.com/questions/362986/capture-the-screen-into-a-bitmap) – Lei Yang Nov 16 '16 at 00:53
  • `Using` disposes the bitmap. Nothing will be able to use it once you return it. Remove the outer using block and just the return the bitmap. Then when you are finished using it in the rest of the code you can dispose it. – FloatingKiwi Nov 16 '16 at 00:58
  • @LeiYang I was attempting to separate it so I could use it elsewhere without rewriting the code multiple times, but I can work around it. Thanks heaps for your help. – Pearcy Nov 16 '16 at 00:59
  • @FloatingKiwi That worked perfect, thanks heaps for your help. – Pearcy Nov 16 '16 at 01:02

2 Answers2

4

This happens because the code:

using (screenImage = ...)
{
}

Ends up disposing of screenImage. So what you end up returning is a disposed object.

A slight modification of your code to remove the using will fix the problem:

private Bitmap GetScreenShot()
{
    Bitmap screenImage = new Bitmap(panelPreview.Width, panelPreview.Height))
    using (Graphics g = Graphics.FromImage(screenImage))
    {                    
        Rectangle rectanglePanelVideoPreview = panelPreview.Bounds;
        Point sourcePoints = panelPreview.PointToScreen(new Point(panelPreview.ClientRectangle.X, panelPreview.ClientRectangle.Y));
        g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size);
    }                

    return screenImage;
}

But you should remember to call Dispose on that bitmap when you're done using it. Especially if this is something that you'll be doing often.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

It should also be mentioned that returning a Bitmap like this can lead to a memory leak if the Bitmap is not properly disposed of in the returning function. The answer from Jim above will solve your program to get it to no longer have a runtime error, but as long as there is not proper disposal of the Bitmap returned, the GC will likely have a delay in returning the memory, so you'll have to manually clean up, Dispose(), of the Bitmap wherever it was returned.

Travis Primm
  • 149
  • 2
  • 3