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