Skip to content

Commit 96c1b99

Browse files
committed
Write .NET screenshots using the Save overload that takes a FileStream
Rather than using the Image.Save overload that takes a string containing the file name, we should use a System.IO.FileStream. The FileStream object handles validating the existence of the path to which one is attempting to write, so this will avoid the cryptic GDI+ error that occurs if the file already exists. Additionally, the SaveAsFile method now does what it says on the tin, that the file will be overwritten if it already exists. Fixes issue #4645.
1 parent d8bdaa2 commit 96c1b99

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

dotnet/src/webdriver/Screenshot.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,17 @@ public void SaveAsFile(string fileName, ScreenshotImageFormat format)
119119

120120
using (MemoryStream imageStream = new MemoryStream(this.byteArray))
121121
{
122-
#if NETCOREAPP2_0 || NETSTANDARD2_0
123122
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
124-
{
123+
{
124+
#if NETCOREAPP2_0 || NETSTANDARD2_0
125125
imageStream.WriteTo(fileStream);
126-
}
127126
#else
128-
Image screenshotImage = Image.FromStream(imageStream);
129-
screenshotImage.Save(fileName, ConvertScreenshotImageFormat(format));
127+
using (Image screenshotImage = Image.FromStream(imageStream))
128+
{
129+
screenshotImage.Save(fileStream, ConvertScreenshotImageFormat(format));
130+
}
130131
#endif
132+
}
131133
}
132134
}
133135

0 commit comments

Comments
 (0)