0

Code is here :

    public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
    {
        Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
        Graphics graphics = Graphics.FromImage(ScreenCaptureBmp as Image);
        graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
        graphics.Dispose();
        return ScreenCaptureBmp;
    }

    public Bitmap ResizeBitmap(Bitmap ResizeBmp, int RBmpWidth, int RBmpHeight)
    {
        Bitmap RBmp = new Bitmap(RBmpWidth, RBmpHeight);
        using (Graphics RBmpG = Graphics.FromImage((Image)RBmp))
            RBmpG.DrawImage(ResizeBmp, 0, 0, RBmpWidth, RBmpHeight);
        return RBmp;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320);
        Bitmap Pic1 = ResizeBitmap(Pic, 128, 64);
        pictureBox1.Image = Pic1;
    }

Which Bitmap I have to dispose? (I don't know if I have to dispose all of these "ScreenCaptureBmp", "ResizeBmp", "RBmp", "Pic", "Pic1" or Some of these).

Did I have to dispose return Bitmap of a method? (Example: "ScreenCaptureBmp", "RBmp").

Did I dispose Graphics("graphics", "RBmpG") in right way in this code?

WHERE I HAVE TO WRITE DISPOSE TO DISPOSE IN RIGHT WAY?

If I write this code:

    public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
    {
        Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
        using (Graphics graphics = Graphics.FromImage(ScreenCaptureBmp as Image))
            graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
        return ScreenCaptureBmp;
    }

    public Bitmap ResizeBitmap(Bitmap ResizeBmp, int RBmpWidth, int RBmpHeight)
    {
        Bitmap RBmp = new Bitmap(RBmpWidth, RBmpHeight);
        using (Graphics RBmpG = Graphics.FromImage((Image)RBmp))
            RBmpG.DrawImage(ResizeBmp, 0, 0, RBmpWidth, RBmpHeight);
        return RBmp;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320);
        Bitmap Pic1 = ResizeBitmap(Pic, 128, 64);
        Pic.Dispose();
            using (Image PreviewImage = pictureBox1.Image)
            {
                pictureBox1.Image = Pic1;
            }
    }

Does everything dispose in correct way in this(2nd) code?

Is disposing in "timer1_Tick" method correct in this(2nd) code?

  • Use a `using` block and all will be well. Neither method can dispose of the bitmap they created if the consuming code will use them. That code should though if new images are created/used – Ňɏssa Pøngjǣrdenlarp Jul 18 '16 at 21:29
  • Can You Please write example method? I tried to write "using" but I can't write successfully, so my application crashes after start debugging(when I use my method). I am a newbie. –  Jul 18 '16 at 21:35
  • For images used on a UI it's always a bit of a bother... you kind of have to keep track yourself of whether they're still used anywhere. – Nyerguds Jan 05 '18 at 06:01

2 Answers2

0

This applies (or at least, is supposed to) for all things in all languages:
Dispose is meant to be called when you are finished using something or if the disposable object will not live past the scope it was declared in. (See: Proper use of the IDisposable interface)

In C#, using a using block automatically disposes everything inside when it is finished executing. (See: Uses of "using" in C#)

In your case, in your ScreenCaptureBitmap method, your Graphics object is disposed of properly because

  • You are finished with it after you called CopyFromScreen
  • It will not continue to live outside of the ScreenCaptureBitmap method

In your ResizeBitmap method, you do not dispose RBmp because you are returning it, and it will continue to live outside of the method.
In your ResizeBitmap method, you automatically dispose your RBmpG because it is in a using block.

In your timer1_Tick method, after you use Pic to help create Pic1, you should dispose it as you are no longer using it and it will not need to live past the scope of the method.
In your timer1_Tick method, you do not dispose Pic1 because it is assigned to pictureBox1.Image, and pictureBox1 exists outside of the scope of the method.
However, since it is a tick method, I assume it will be called repeatedly, and you should clean up (dispose) of pictureBox1's previous Image provided that it exists.

Community
  • 1
  • 1
Yidna
  • 452
  • 4
  • 12
0

Did I have to dispose return Bitmap of a method? (Example: "ScreenCaptureBmp", "RBmp").

No, you had not. You're OK with the code of your API methods (ScreenCaptureBitmap/ResizeBitmap).

Did I dispose Graphics("graphics", "RBmpG") in right way in this code?

Yes, the demonstrated approaches are correct, but I've personally preferred to use the using approach because it more robust (you can google more about the using benefits. In short, it guaranteed the Dispose call and encapsulate all needed checks):

public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
    Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
    using(var graphics = Graphics.FromImage(ScreenCaptureBmp))
        graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
    return ScreenCaptureBmp;
}

But you're not OK with this API usage. The correct version should looks like this:

void timer1_Tick(object sender, EventArgs e)
{
    using(Bitmap Pic = ScreenCaptureBitmap(50, 50, 640, 320)) {
        Image oldImage = pictureBox1.Image;
        using(oldImage)
            pictureBox1.Image = ResizeBitmap(Pic, 128, 64);
    }
}
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • So I don't have to worry about disposing "ScreenCaptureBmp", "RBmp" & have to dispose other Bitmap except these two right? The code You write, doe's there "Pic", "oldImage" & pictureBox1 previous image disposing automatically? (I want to capture screen, resise it then show it in pictureBox1 & finally dispose whatever require to dispose). If I don't dispose, does my app take more ram continuously with respect the time? –  Jul 19 '16 at 08:54
  • As I have already mentioned you're correct with ScreenCaptureBmp/ResizeBitmap implementation. In my code, all the disposals are performed when the specific variable leave the corresponding `using` block. Thus, only the single image alive at each moment of time. And all the temporary objects(images/graphics) are disposed. – DmitryG Jul 19 '16 at 10:09
  • Is there any difference between "Graphics graphics" & "var graphics" ? –  Jul 19 '16 at 11:08
  • There are no technical differences rather than [coding style preferences](http://stackoverflow.com/questions/4868477/why-should-i-use-var-instead-of-a-type). – DmitryG Jul 19 '16 at 13:10
  • If I write "ResizeBmp.Dispose();" before the line "return RBmp;" , Is it right or wrong or better? If I write or not write that, compiler is not giving any error & app also not crashing. I don't know if the line "ResizeBmp.Dispose();" is necessary or not. –  Jul 19 '16 at 16:22
  • This is incorrect due to convenient method call reasons. The ResizeBMP is input parameter of your method and you should not affect it in any cases within your method. The disposing of this argument should be performed by ResizeBitmap method **caller**. – DmitryG Jul 21 '16 at 08:38
  • So if I don't write "ResizeBmp.Dispose();", then "ResizeBmp" will automatically dispose by "ResizeBitmap" method... am I right? –  Jul 21 '16 at 10:36
  • No, the disposing of the ResizeBmp parameter only depends on ResizeBitmap method **caller**. Take a look at the `timer1_Tick` code - this method is created the Pic bitmap, then pass a newly created bitmap into the ResizeBitmap method as a parameter, and only after that destroy it because it is not needed for now. – DmitryG Jul 21 '16 at 14:23
  • I have edited my question & wrote some code there(with the help of Your suggestion). Please kindly check if I am right or wrong? Thank You for Help. –  Jul 21 '16 at 15:47
  • You're right, - you can call the Dispose method for the `Pic` bitmap right after it is not needed(however I suggest you always use the `using` approach). – DmitryG Jul 21 '16 at 15:54