Skip to content

Commit 70518d1

Browse files
committed
Preparing .NET screenshot for removal of direct dependency on System.Drawing
1 parent c243c2c commit 70518d1

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

dotnet/src/webdriver/Screenshot.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@
2323

2424
namespace OpenQA.Selenium
2525
{
26+
/// <summary>
27+
/// File format for saving screenshots.
28+
/// </summary>
29+
public enum ScreenshotImageFormat
30+
{
31+
/// <summary>
32+
/// W3C Portable Network Graphics image format.
33+
/// </summary>
34+
Png,
35+
36+
/// <summary>
37+
/// Joint Photgraphic Experts Group image format.
38+
/// </summary>
39+
Jpeg,
40+
41+
/// <summary>
42+
/// Graphics Interchange Format image format.
43+
/// </summary>
44+
Gif,
45+
46+
/// <summary>
47+
/// Tagged Image File Format image format.
48+
/// </summary>
49+
Tiff,
50+
51+
/// <summary>
52+
/// Bitmap image format.
53+
/// </summary>
54+
Bmp
55+
}
56+
2657
/// <summary>
2758
/// Represents an image of the page currently loaded in the browser.
2859
/// </summary>
@@ -58,12 +89,24 @@ public byte[] AsByteArray
5889
get { return this.byteArray; }
5990
}
6091

92+
/// <summary>
93+
/// Saves the screenshot to a file, overwriting the file if it already exists.
94+
/// </summary>
95+
/// <param name="fileName">The full path and file name to save the screenshot to.</param>
96+
/// <param name="format">A <see cref="ScreenshotImageFormat"/> value indicating the format
97+
/// to save the image to.</param>
98+
public void SaveAsFile(string fileName, ScreenshotImageFormat format)
99+
{
100+
this.SaveAsFile(fileName, ConvertScreenshotImageFormat(format));
101+
}
102+
61103
/// <summary>
62104
/// Saves the screenshot to a file, overwriting the file if it already exists.
63105
/// </summary>
64106
/// <param name="fileName">The full path and file name to save the screenshot to.</param>
65107
/// <param name="format">A <see cref="System.Drawing.Imaging.ImageFormat"/> object indicating the format
66108
/// to save the image to.</param>
109+
[Obsolete("System.Drawing.Imaging.ImageFormat is not supported in .NET Core, and depending on it is being removed from WebDriver. Please convert to ScreenshotImageFormat.")]
67110
public void SaveAsFile(string fileName, ImageFormat format)
68111
{
69112
using (MemoryStream imageStream = new MemoryStream(this.byteArray))
@@ -81,5 +124,30 @@ public override string ToString()
81124
{
82125
return this.base64Encoded;
83126
}
127+
128+
private static ImageFormat ConvertScreenshotImageFormat(ScreenshotImageFormat format)
129+
{
130+
ImageFormat returnedFormat = ImageFormat.Png;
131+
switch (format)
132+
{
133+
case ScreenshotImageFormat.Jpeg:
134+
returnedFormat = ImageFormat.Jpeg;
135+
break;
136+
137+
case ScreenshotImageFormat.Gif:
138+
returnedFormat = ImageFormat.Gif;
139+
break;
140+
141+
case ScreenshotImageFormat.Bmp:
142+
returnedFormat = ImageFormat.Bmp;
143+
break;
144+
145+
case ScreenshotImageFormat.Tiff:
146+
returnedFormat = ImageFormat.Tiff;
147+
break;
148+
}
149+
150+
return returnedFormat;
151+
}
84152
}
85153
}

0 commit comments

Comments
 (0)