Avatar of Pacman
PacmanFlag for Germany

asked on 

Leak using CopyFromScreen()

This is managed GDI+.
I've encountered a resource leak (GDI handle count increases) in the following code.
I think it's the Graphics.CopyFromScreen() method which is leaking.

I've got a tool to watch the open GDI handles of an application.
It always increases number by one when I execute this code.

Does anybody know what's going wrong here ?

private void toolButtonTest_Click(object sender, EventArgs e)
{
    Rectangle screenRect = Screen.PrimaryScreen.WorkingArea;
 
    using (Bitmap dumpBitmap = new Bitmap(screenRect.Width, screenRect.Height))
    {
        using (Graphics targetGraphics = Graphics.FromImage(dumpBitmap))
        {
            targetGraphics.CopyFromScreen(0, 0, 0, 0, new Size(dumpBitmap.Width, dumpBitmap.Height));
        }
 
        dumpBitmap.Save(@"e:\_test.png");
        dumpBitmap.Dispose();
    }
}

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
Pacman
ASKER CERTIFIED SOLUTION
Avatar of Pacman
Pacman
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
you don't need to Dispose the bitmap manually since your are creating it with "using"
Avatar of Pacman
Pacman
Flag of Germany image

ASKER

Yes, it was only to make sure that absolutely nothing goes wrong from my side  :-D
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo