Preparing .NET screenshot for removal of direct dependency on System.Drawing
namespace OpenQA.Selenium
{
+ /// <summary>
+ /// File format for saving screenshots.
+ /// </summary>
+ public enum ScreenshotImageFormat
+ {
+ /// <summary>
+ /// W3C Portable Network Graphics image format.
+ /// </summary>
+ Png,
+
+ /// <summary>
+ /// Joint Photgraphic Experts Group image format.
+ /// </summary>
+ Jpeg,
+
+ /// <summary>
+ /// Graphics Interchange Format image format.
+ /// </summary>
+ Gif,
+
+ /// <summary>
+ /// Tagged Image File Format image format.
+ /// </summary>
+ Tiff,
+
+ /// <summary>
+ /// Bitmap image format.
+ /// </summary>
+ Bmp
+ }
+
/// <summary>
/// Represents an image of the page currently loaded in the browser.
/// </summary>
@@ -58,12 +89,24 @@ public byte[] AsByteArray
get { return this.byteArray; }
}
+ /// <summary>
+ /// Saves the screenshot to a file, overwriting the file if it already exists.
+ /// </summary>
+ /// <param name="fileName">The full path and file name to save the screenshot to.</param>
+ /// <param name="format">A <see cref="ScreenshotImageFormat"/> value indicating the format
+ /// to save the image to.</param>
+ public void SaveAsFile(string fileName, ScreenshotImageFormat format)
+ {
+ this.SaveAsFile(fileName, ConvertScreenshotImageFormat(format));
+ }
+
/// <summary>
/// Saves the screenshot to a file, overwriting the file if it already exists.
/// </summary>
/// <param name="fileName">The full path and file name to save the screenshot to.</param>
/// <param name="format">A <see cref="System.Drawing.Imaging.ImageFormat"/> object indicating the format
/// to save the image to.</param>
+ [Obsolete("System.Drawing.Imaging.ImageFormat is not supported in .NET Core, and depending on it is being removed from WebDriver. Please convert to ScreenshotImageFormat.")]
public void SaveAsFile(string fileName, ImageFormat format)
{
using (MemoryStream imageStream = new MemoryStream(this.byteArray))
@@ -81,5 +124,30 @@ public override string ToString()
{
return this.base64Encoded;
}
+
+ private static ImageFormat ConvertScreenshotImageFormat(ScreenshotImageFormat format)
+ {
+ ImageFormat returnedFormat = ImageFormat.Png;
+ switch (format)
+ {
+ case ScreenshotImageFormat.Jpeg:
+ returnedFormat = ImageFormat.Jpeg;
+ break;
+
+ case ScreenshotImageFormat.Gif:
+ returnedFormat = ImageFormat.Gif;
+ break;
+
+ case ScreenshotImageFormat.Bmp:
+ returnedFormat = ImageFormat.Bmp;
+ break;
+
+ case ScreenshotImageFormat.Tiff:
+ returnedFormat = ImageFormat.Tiff;
+ break;
+ }
+
+ return returnedFormat;
+ }
}
}
