Skip to content

Commit

Permalink
Write .NET screenshots using the Save overload that takes a FileStream
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimevans committed Mar 22, 2018
1 parent d8bdaa2 commit 96c1b99
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions dotnet/src/webdriver/Screenshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 96c1b99

Please sign in to comment.