thitemple
thitemple

Reputation: 6059

Taking a screenshot using Chrome headless and C#

I'm trying to take a screenshot using Chrome headless from an ASP.Net MVC app, here's the code:

public string TakeScreenshot(ScreenshotRequest request)
{
    var pathToScreenshotFile = Path.Combine(Path.GetTempPath(), $"{request.FileName}.png");
    var arguments = $@" --headless --hide-scrollbars --disable-gpu --screenshot=""{pathToScreenshotFile}"" --window-size={request.Width},{request.Height} https://google.com";

    var psi = new ProcessStartInfo(pathToBrowser) { UseShellExecute = false, Verb = "runas" };
    using (Process process = Process.Start(psi))
    {
        Thread.Sleep(1000);
        var image = string.Empty;
        var executionCount = 0;
        while(image == string.Empty && executionCount < 5)
        {
            if (File.Exists(pathToScreenshotFile))
            {
                image = Convert.ToBase64String(File.ReadAllBytes(pathToScreenshotFile));
            }
            else
            {
                Thread.Sleep(1000);
            }
        }
        return image;
    }
}

The pathToBrowser variable points to the chrome executable: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

For some reason, the file does not get created, but if I open a terminal and run the following command it works:

E:\sources\chromium\bin\chrome.exe" --headless --hide-scrollbars --disable-gpu --screenshot="C:\Windows\TEMP\5353e1ab-783c-442a-8d72-54d030529e68a.png" --window-size=1920,874 https://google.com

Any ideas? I thought it needed to run as admin hence the "runas", but that didn't help.

Edit:

I think it's something related to permissions because the same code works when I run it from a console application. Right now I have the folder containing Chrome with Full Control to Everyone. I don't know what else I'm missing.

Upvotes: 1

Views: 2960

Answers (1)

user8259632
user8259632

Reputation:

It worked great for me. I did have to include the arguments in the ProcessStartInfo.

private void Form1_Load(object sender, EventArgs e)
{
    var output = TakeScreenshot(@"C:\Windows\TEMP\5353e1ab-783c-442a-8d72-54d030529e68a.png");
}
public string TakeScreenshot(string request)
{
    var pathToBrowser = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    var pathToScreenshotFile = Path.Combine(Path.GetTempPath(), $"{request}.png");
    var arguments = $@" --headless --hide-scrollbars --disable-gpu --screenshot=""{pathToScreenshotFile}"" --window-size={1920},{874} https://google.com";

    var psi = new ProcessStartInfo(pathToBrowser,arguments) { UseShellExecute = false, Verb = "runas" };
    using (Process process = Process.Start(psi))
    {
        Thread.Sleep(1000);
        var image = string.Empty;
        var executionCount = 0;
        while (image == string.Empty && executionCount < 5)
        {
            if (File.Exists(pathToScreenshotFile))
            {
                image = Convert.ToBase64String(File.ReadAllBytes(pathToScreenshotFile));
            }
            else
            {
                Thread.Sleep(1000);
            }
        }
        return image;
    }
}

Upvotes: 1

Related Questions