Search code examples
c#.netimage-processingbitmapdispose

Where I have to write dispose in a image processing method?


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?


Solution

  • 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);
        }
    }