使用Chrome headless和C#

使用Chrome headless和C#,c#,asp.net-mvc,google-chrome,google-chrome-headless,C#,Asp.net Mvc,Google Chrome,Google Chrome Headless,我正在尝试从ASP.Net MVC应用程序中使用Chrome headless拍摄屏幕截图,代码如下: public string TakeScreenshot(ScreenshotRequest request) { var pathToScreenshotFile = Path.Combine(Path.GetTempPath(), $"{request.FileName}.png"); var arguments = $@" --headless --hide-scroll

我正在尝试从ASP.Net MVC应用程序中使用Chrome headless拍摄屏幕截图,代码如下:

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;
    }
}
有什么想法吗?我认为它需要以管理员的身份运行,因此使用了“runas”,但这没有帮助

编辑:


我认为这与权限有关,因为当我从控制台应用程序运行它时,相同的代码也可以工作。现在我有一个包含Chrome的文件夹,可以完全控制所有人。我不知道我还缺少什么。

这对我来说非常有用。我确实必须在
进程startinfo
中包含
参数

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;
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{
var输出=截图(@“C:\Windows\TEMP\5353e1ab-783c-442a-8d72-54d030529e68a.png”);
}
公共字符串截图(字符串请求)
{
var pathToBrowser=@“C:\Program Files(x86)\Google\Chrome\Application\Chrome.exe”;
var pathToScreenshotFile=Path.Combine(Path.GetTempPath(),$“{request}.png”);
变量参数=$@”--无头--隐藏滚动条--禁用gpu--屏幕截图=“{pathToScreenshotFile}”--窗口大小={1920},{874}https://google.com";
var psi=newprocessstartinfo(路径浏览器,参数){UseShellExecute=false,Verb=“runas”};
使用(过程=过程启动(psi))
{
睡眠(1000);
var image=string.Empty;
var executionCount=0;
while(image==string.Empty&&executionCount<5)
{
if(File.Exists(pathToScreenshotFile))
{
image=Convert.ToBase64String(File.ReadAllBytes(pathToScreenshotFile));
}
其他的
{
睡眠(1000);
}
}
返回图像;
}
}
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;
    }
}