Aunix
Aunix

Reputation: 101

C# Selenium - How do you take a screenshot in Visual Studio 2015

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.

Upvotes: 1

Views: 27820

Answers (2)

Gülsen Keskin
Gülsen Keskin

Reputation: 810

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

Upvotes: 0

Aunix
Aunix

Reputation: 101

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.

Upvotes: 9

Related Questions