diff --git a/Objectivity.Test.Automation.Common/Helpers/TakeScreenShot.cs b/Objectivity.Test.Automation.Common/Helpers/TakeScreenShot.cs index c9784b61c..1511d32cb 100644 --- a/Objectivity.Test.Automation.Common/Helpers/TakeScreenShot.cs +++ b/Objectivity.Test.Automation.Common/Helpers/TakeScreenShot.cs @@ -166,29 +166,30 @@ public static string TakeScreenShotOfElement(int iframeLocationX, int iframeLoca Logger.Debug(CultureInfo.CurrentCulture, "Cutting out screenshot of element locationX:{0} locationY:{1} elementWidth:{2} elementHeight:{3}", locationX, locationY, elementWidth, elementHeight); - var importFile = new Bitmap(filePath); - ////Check if new size of image is not bigger than imported. - if (importFile.Size.Height - locationY < elementHeight) - { - elementHeight = importFile.Size.Height - locationY; - } - - if (importFile.Size.Width - locationX < elementWidth) - { - elementHeight = importFile.Size.Height - locationX; - } - - var image = new Rectangle(locationX, locationY, elementWidth, elementHeight); string newFilePath; + Bitmap importFile = null; Bitmap cloneFile; try { + importFile = new Bitmap(filePath); + ////Check if new size of image is not bigger than imported. + if (importFile.Size.Height - locationY < elementHeight) + { + elementHeight = importFile.Size.Height - locationY; + } + + if (importFile.Size.Width - locationX < elementWidth) + { + elementWidth = importFile.Size.Width - locationX; + } + + var image = new Rectangle(locationX, locationY, elementWidth, elementHeight); newFilePath = Path.Combine(folder, screenshotName + ".png"); cloneFile = (Bitmap)importFile.Clone(image, importFile.PixelFormat); } finally { - importFile.Dispose(); + importFile?.Dispose(); } Logger.Debug(CultureInfo.CurrentCulture, "Saving screenshot of element {0}", newFilePath);