Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Screenshots are saves as png despite .jpg extension #500

Open
olivercoad opened this issue Jun 12, 2020 · 2 comments
Open

Screenshots are saves as png despite .jpg extension #500

olivercoad opened this issue Jun 12, 2020 · 2 comments

Comments

@olivercoad
Copy link
Contributor

Describe the bug
While looking into #499, I realised that the screenshot function saves the image in png format despite adding the .jpg file extension.

let private saveScreenshot directory filename pic =
if not <| Directory.Exists(directory)
then Directory.CreateDirectory(directory) |> ignore
IO.File.WriteAllBytes(Path.Combine(directory,filename + ".jpg"), pic)
let private takeScreenShotIfAlertUp () =
try
let screenBounds = canopy.screen.getPrimaryScreenBounds ()
let bitmap = new Bitmap(width = screenBounds.width, height = screenBounds.height, format = PixelFormat.Format32bppArgb);
use graphics = Graphics.FromImage(bitmap)
graphics.CopyFromScreen(screenBounds.x, screenBounds.y, 0, 0, screenBounds.size, CopyPixelOperation.SourceCopy);
use stream = new MemoryStream()
bitmap.Save(stream, ImageFormat.Png)
stream.Close()
stream.ToArray()
with ex ->
printfn "Sorry, unable to take a screenshot. An alert was up, and the backup plan failed!
Exception: %s" ex.Message
Array.empty<byte>
let private takeScreenshot directory filename (browser : IWebDriver) =
try
let pic = (browser :?> ITakesScreenshot).GetScreenshot().AsByteArray
saveScreenshot directory filename pic
pic
with
| :? UnhandledAlertException as ex->
let pic = takeScreenShotIfAlertUp()
saveScreenshot directory filename pic
let alert = browser.SwitchTo().Alert()
alert.Accept()
pic
let private pngToJpg pngArray =
let pngStream = new MemoryStream()
let jpgStream = new MemoryStream()
pngStream.Write(pngArray, 0, pngArray.Length)
let img = Image.FromStream(pngStream)
img.Save(jpgStream, ImageFormat.Jpeg)
jpgStream.ToArray()
(* documented/actions *)
let screenshot directory filename (browser : IWebDriver) =
match box browser with
| :? ITakesScreenshot -> takeScreenshot directory filename browser |> pngToJpg
| _ -> Array.empty<byte>

Unfortunately, I don't think fixing this won't help reduce file size for #499 because failure screenshots uses the returned byte array which is actually converted to jpg format.

To Reproduce
Steps to reproduce the behavior:

  1. Take a screenshot
start chrome
url "https://github.com/lefthandedgoat/canopy/issues/500"
screenshot "screenshots" "issue500"
  1. Check the file format. For example in linux (or WSL),
$ file screenshot/issue500.jpg
issue500.jpg: PNG image data, 824 x 918, 8-bit/color RGBA, non-interlaced

Expected behavior
Should convert to jpg before saving the file. Or save with .png file extension.

Other information

  • OS: Windows 10
  • Browser: chrome
  • NETCore 3.1
  • canopy 2.1.5

Dotnet core
screenshot actually fails when I use dotnet core. It saves the png format file (with .jpg extension) and then throws when trying to convert to jpg. Maybe I'm missing a paket reference or something.

System.PlatformNotSupportedException: System.Drawing is not supported on this platform.
   at System.Drawing.Image.FromStream(Stream stream)
   at canopy.parallell.functions.pngToJpg(Byte[] pngArray) in C:\projects\canopy\src\canopy\canopy.parallell.functions.fs:line 89
   at <StartupCode$FSI_0013>.$FSI_0013.main@()
@lefthandedgoat
Copy link
Owner

I can fix this this weekend. canopy uses https://www.nuget.org/packages/System.Drawing.Common/ which should work with .net core but I will also try it on windows + osx, no linux vm handy sorry.

@IvanRainbolt
Copy link

IvanRainbolt commented Aug 24, 2020

I think my issue is related here from comparing the error messages. My script is here:
https://gist.github.com/IvanRainbolt/42b8c0c7fe3742e741e40373582be86e
and my error message is this:
D:\OneDrive\T\canopy.experiment>fsi tryscreenshot.fsx
READY_
Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 65077
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
DevTools listening on ws://127.0.0.1:65080/devtools/browser/0185cedf-d3a3-49d7-a520-81494d7584b7
path: C:\Users\netaz\AppData\Roaming\canopy
System.TypeLoadException: Could not load type 'System.MarshalByRefObject' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
at canopy.parallell.functions.pngToJpg(Byte[] pngArray)
at <StartupCode$FSI_0001>.$FSI_0001.main@() in D:\OneDrive\T\canopy.experiment\tryscreenshot.fsx:line 46

Stopped due to error
D:\OneDrive\T\canopy.experiment>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants