2017-10-16 6 views
1

Je suis en train de prendre une capture d'écran à l'aide sans tête Chrome d'une application ASP.Net MVC, voici le code:Prendre une capture d'écran à l'aide sans tête Chrome et C#

public string TakeScreenshot(ScreenshotRequest request) 
{ 
    var pathToScreenshotFile = Path.Combine(Path.GetTempPath(), $"{request.FileName}.png"); 
    var arguments = [email protected]" --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; 
    } 
} 

Les pathToBrowser des points variables à l'exécutable chrome: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

Pour une raison quelconque, le fichier ne soit pas créé, mais si j'ouvre un terminal et exécutez la commande suivante cela fonctionne:

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 

a ny idées? Je pensais qu'il devait fonctionner comme administrateur d'où les "runas", mais cela n'a pas aidé.

Edit:

Je pense qu'il est quelque chose lié à des autorisations parce que le même code fonctionne quand je le lance à partir d'une application console. En ce moment j'ai le dossier contenant Chrome avec contrôle total à tout le monde. Je ne sais pas quoi d'autre me manque.

Répondre

0

Cela a très bien fonctionné pour moi. J'ai dû inclure le arguments dans le 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 = [email protected]" --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; 
    } 
}