0

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

rohit
  • 501
  • 7
  • 21
  • 1
    Rarely does anybody follow any advice religiously in programming. Every feature in a programming language was added for some use case, so whenever you reach that use case (it may be rare - an example would be the C goto) you should make use of the feature. – Adam Mihalcin Feb 24 '12 at 05:37
  • http://stackoverflow.com/a/2809026/555547 may be helpful. Personally, I would just use the second because it's not that hard, and it should dispose of resources properly when complete, but I'm no expert. – Jason Feb 24 '12 at 05:38

1 Answers1

2

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.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193