diff --git a/dotnet/src/webdriver/Screenshot.cs b/dotnet/src/webdriver/Screenshot.cs index 0c3a3de5243c3..8b2900bfe34b8 100644 --- a/dotnet/src/webdriver/Screenshot.cs +++ b/dotnet/src/webdriver/Screenshot.cs @@ -119,15 +119,17 @@ public void SaveAsFile(string fileName, ScreenshotImageFormat format) using (MemoryStream imageStream = new MemoryStream(this.byteArray)) { -#if NETCOREAPP2_0 || NETSTANDARD2_0 using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) - { + { +#if NETCOREAPP2_0 || NETSTANDARD2_0 imageStream.WriteTo(fileStream); - } #else - Image screenshotImage = Image.FromStream(imageStream); - screenshotImage.Save(fileName, ConvertScreenshotImageFormat(format)); + using (Image screenshotImage = Image.FromStream(imageStream)) + { + screenshotImage.Save(fileStream, ConvertScreenshotImageFormat(format)); + } #endif + } } }