1717// </copyright>
1818
1919using System ;
20- using System . Drawing ;
21- using System . Drawing . Imaging ;
2220using System . IO ;
2321
2422namespace OpenQA . Selenium
2523{
26- /// <summary>
27- /// File format for saving screenshots.
28- /// </summary>
29- [ Obsolete ( "Support for decoding/encoding between different image formats is deprecated" ) ]
30- public enum ScreenshotImageFormat
31- {
32- /// <summary>
33- /// W3C Portable Network Graphics image format.
34- /// </summary>
35- Png ,
36-
37- /// <summary>
38- /// Joint Photgraphic Experts Group image format.
39- /// </summary>
40- Jpeg ,
41-
42- /// <summary>
43- /// Graphics Interchange Format image format.
44- /// </summary>
45- Gif ,
46-
47- /// <summary>
48- /// Tagged Image File Format image format.
49- /// </summary>
50- Tiff ,
51-
52- /// <summary>
53- /// Bitmap image format.
54- /// </summary>
55- Bmp
56- }
57-
5824 /// <summary>
5925 /// Represents an image of the page currently loaded in the browser.
6026 /// </summary>
@@ -76,69 +42,7 @@ public Screenshot(string base64EncodedScreenshot) : base(base64EncodedScreenshot
7642 /// <param name="fileName">The full path and file name to save the screenshot to.</param>
7743 public override void SaveAsFile ( string fileName )
7844 {
79- this . SaveAsFile ( fileName , ScreenshotImageFormat . Png ) ;
80- }
81-
82- /// <summary>
83- /// Saves the screenshot to a file, overwriting the file if it already exists.
84- /// </summary>
85- /// <param name="fileName">The full path and file name to save the screenshot to.</param>
86- /// <param name="format">A <see cref="ScreenshotImageFormat"/> value indicating the format
87- /// to save the image to.</param>
88- [ Obsolete ( "Support for decoding/encoding between different image formats is deprecated; use SaveAsFile(string fileName) method instead" ) ]
89- public void SaveAsFile ( string fileName , ScreenshotImageFormat format )
90- {
91- using ( MemoryStream imageStream = new MemoryStream ( this . AsByteArray ) )
92- {
93- using ( FileStream fileStream = new FileStream ( fileName , FileMode . Create ) )
94- {
95- // Optimization: The byte array is already a PNG, so we can just
96- // write directly to the file. If the user wants to convert to
97- // another image format, we'll allow them to do so, but on certain
98- // framework versions (.NET 6 or above) this is likely to fail at
99- // runtime. It is unclear how many Selenium users are using this
100- // feature to convert the returned screenshot into a different image
101- // format. Future mitigations of this issue would need to take a
102- // dependency on an image processing library like ImageSharp or
103- // similar.
104- if ( format == ScreenshotImageFormat . Png )
105- {
106- imageStream . WriteTo ( fileStream ) ;
107- }
108- else
109- {
110- using ( Image screenshotImage = Image . FromStream ( imageStream ) )
111- {
112- screenshotImage . Save ( fileStream , ConvertScreenshotImageFormat ( format ) ) ;
113- }
114- }
115- }
116- }
117- }
118-
119- private static ImageFormat ConvertScreenshotImageFormat ( ScreenshotImageFormat format )
120- {
121- ImageFormat returnedFormat = ImageFormat . Png ;
122- switch ( format )
123- {
124- case ScreenshotImageFormat . Jpeg :
125- returnedFormat = ImageFormat . Jpeg ;
126- break ;
127-
128- case ScreenshotImageFormat . Gif :
129- returnedFormat = ImageFormat . Gif ;
130- break ;
131-
132- case ScreenshotImageFormat . Bmp :
133- returnedFormat = ImageFormat . Bmp ;
134- break ;
135-
136- case ScreenshotImageFormat . Tiff :
137- returnedFormat = ImageFormat . Tiff ;
138- break ;
139- }
140-
141- return returnedFormat ;
45+ File . WriteAllBytes ( fileName , this . AsByteArray ) ;
14246 }
14347 }
14448}
0 commit comments