rohit
rohit

Reputation: 501

What is the best practice when it comes to dispose image object in c#?

I have following two pieces of code First one is not explicitly disposing Image object while second one is disposing it correctly. Please suggest which one to use in production code.

private bool SavePatientChartImage(byte[] ImageBytes, string ImageFilePath, string IMAGE_NAME, int rotationAngle)
    {
        bool success = false;
        System.Drawing.Image newImage;
        try
        {
            using (MemoryStream stream = new MemoryStream(ImageBytes))
            {
                newImage = System.Drawing.Image.FromStream(stream);
                switch (rotationAngle)
                {
                    case 90:
                        newImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        break;
                    case 180:
                        newImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        break;
                    case 270:
                        newImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        break;
                    default:
                        newImage = newImage;
                        break;
                }
                newImage.Save(Path.Combine(ImageFilePath, IMAGE_NAME));
                success = true;
            }
        }
        catch (Exception ex)
        {
            success = false;
        }
        return success;
    }

and

private bool SavePatientChartImage(byte[] ImageBytes, string ImageFilePath, string IMAGE_NAME, int rotationAngle)
    {
        bool success = false;
        System.Drawing.Image newImage;
        try
        {
            using (MemoryStream stream = new MemoryStream(ImageBytes))
            {
                using(newImage = System.Drawing.Image.FromStream(stream))
                {
                switch (rotationAngle)
                {
                    case 90:
                        newImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        break;
                    case 180:
                        newImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        break;
                    case 270:
                        newImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        break;
                    default:
                        newImage = newImage;
                        break;
                }
                newImage.Save(Path.Combine(ImageFilePath, IMAGE_NAME));
                success = true;
              }
            }
        }
        catch (Exception ex)
        {
            success = false;
        }
        return success;
    }

Which one to follow religiously. Please suggest

Upvotes: 0

Views: 696

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64648

You should always dispose the disposable instances somewhere. So take the latter.

You could make it a bit more readable:

       using (MemoryStream stream = new MemoryStream(ImageBytes))
       using(var newImage = System.Drawing.Image.FromStream(stream))
       {
         // ...

Note: It doesn't make sense to have the variable declared outside the using statement. You shouldn't use it outside.

Upvotes: 2

Related Questions