C# Selenium Chrome error: cannot convert from 'System.Drawing.Imaging.ImageFormat' to 'OpenQA.Selenium.ScreenshotImageFormat'

Viewed 6104

I have a problem with selenium in C#. Below is my code but I have still the same error: cannot convert from 'System.Drawing.Imaging.ImageFormat' to 'OpenQA.Selenium.ScreenshotImageFormat'

How can I fix it? Any ideas?

using System;
using System.Drawing.Imaging;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Drawing;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("xx://xx.xx.xx");
            driver.FindElement(By.Id("xx")).SendKeys("xxx");
            driver.FindElement(By.Id("xx")).SendKeys("xxx");
            driver.FindElement(By.Id("xx")).Click();

            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile(@"C:\\test.Png", System.Drawing.Imaging.ImageFormat.Png);

            Console.Read();
            Console.WriteLine("xxx");
        }

    }
}
3 Answers

Instead of System.Drawing.Imaging.ImageFormat.Png as the 2nd param of your SaveAsFile, you need to use OpenQA.Selenium.ScreenshotImageFormat.[something here]

That enum probably has it's own PNG format.

The error was persistent, saying:

System.Drawing.Imaging.ImageFormat is not supported. Please change format.

Then I changed code from:

ScreenShot.SaveAsFile(FileName.ToString(), System.Drawing.Imaging.ImageFormat.Jpeg); 

To:

ScreenShot.SaveAsFile(FileName.ToString(), ScreenshotImageFormat.Jpeg);

And it worked.

Instead of :

ss.SaveAsFile(@"C:\\test.Png", 
  System.Drawing.Imaging.ImageFormat.Png);

Change to:

ss.SaveAsFile(@"C:\\test.Png", 
  System.Drawing.Imaging.ScreenshotImageFormat.Png);

Also use or import:

using System.Drawing.Imaging;
Related