1

I need some help when it comes to taking a screenshot in visual studio 2015 using selenium and C#. I keep getting an error message when running the code below.

            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile(@"C:\Temp\Download\Image.png", 
            ScreenshotImageFormat.Png);

The error message I get is "System.Runtime.InteropServices.ExternalExcepton: A generic error occurred in GDI+"

If anyone is currently able to take screenshots, please let me know if you are doing anything differently.

Aunix
  • 101
  • 1
  • 1
  • 6
  • I haven't used `ScreenshotImageFormat`. Have you looked at [this answer?](http://stackoverflow.com/a/18660838/1183506) – mrfreester Apr 12 '17 at 18:10
  • I have looked at that answer, that's actually what I tried first but using `ss.SaveAsFile(@"D:\Screenshots\SeleniumTestingScreenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);` results in a message telling me that `ImageFormat` is obsolete. – Aunix Apr 12 '17 at 18:31
  • Interesting, I'm not seeing anything in the documentation saying it's obsolete, but maybe I'm looking in the wrong place. I assume you're on `.NET 4.6.2`? – mrfreester Apr 12 '17 at 18:54
  • Nevermind, I realized it's the `SaveAsFile(string, ImageFormat)` that is marked obsolete in the latest webdriver, not `ImageFormat` itself. At any rate, have you made sure your application has write access to that folder? Can you create a simple file or is this just an issue with screenshots? – mrfreester Apr 12 '17 at 19:13
  • I'm not sure how to verify whether or not I have write access, could you point me in the right direction to check for that? – Aunix Apr 12 '17 at 20:23
  • You could try executing a line like this `using (File.Create(filename)) ;` Just make sure your file is in the same location you're trying to save the screenshot to. If that works in your code, write access probably isn't the issue. – mrfreester Apr 12 '17 at 21:40
  • Thanks for all your help @mrfreester, I finally figured it out! You were right in your suspicion with the problem having to do with the `SaveAsFile`, not sure if it was a write access issue but there was definitely something wrong with the way I had the file path setup. Once I changed that, it started working. – Aunix Apr 12 '17 at 21:52
  • Great! Feel free to post your final solution as an answer for others with the same problem :) – mrfreester Apr 12 '17 at 21:53
  • 1
    Possible duplicate of [Take a screenshot with Selenium WebDriver](http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver) – JeffC Apr 14 '17 at 03:39
  • 'ImageFormat' is obsolete. Instead use 'ScreenshotImageFormat'. – Aman Khan Jun 09 '22 at 15:50

2 Answers2

9

Finally!

I found that there was an issue with the way that I had the file path setup, I changed my initial code to the following code and now it's working.

        Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile("C://Image.png", 
        ScreenshotImageFormat.Png);

Note This will save on your C: drive.

Aunix
  • 101
  • 1
  • 1
  • 6
  • You might want to add a time stamp to your image name otherwise you will probably run into problems with "file already exists". – Dazed Apr 13 '17 at 13:06
0

using this you can save the picture to your computer and show it in the picturebox

drv.Navigate().GoToUrl("https://www.youtube.com/");
Thread.Sleep(2000);
ITakesScreenshot screenshot= drv as ITakesScreenshot;
Screenshot screenshot1 = screenshot.GetScreenshot();
screenshot1.SaveAsFile(@"D:\KayıtResmi.png",ScreenshotImageFormat.Png);
screenshot1.SaveAsFile(@"D:\KayıtResmi.png",ScreenshotImageFormat.Png);
Bitmap bitmap = new Bitmap(@"D:\KayıtResmi.png");
pictureBox1.Image = (Image)bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Gülsen Keskin
  • 657
  • 7
  • 23