2017-10-16 4 views
1

Ich versuche, einen Screenshot mit Chrome ohne Kopf von einem ASP.Net MVC-app zu nehmen, hier ist der Code:Einen Screenshot mit Chrome ohne Kopf und 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; 
    } 
} 

Die pathToBrowser Variable auf das Chrom ausführbar: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

Aus irgendeinem Grund erhalten die Datei nicht erstellt, aber wenn ich ein Terminal öffnen und den folgenden Befehl ausführen, funktioniert es:

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 Ideen? Ich dachte, es müsste als Admin laufen, also die "Runas", aber das hat nicht geholfen.

Edit:

Ich denke, es ist etwas zu Berechtigungen verwendet, weil der gleiche Code funktioniert, wenn ich es von einer Konsole-Anwendung ausführen. Jetzt habe ich den Ordner mit Chrome mit Vollzugriff für alle. Ich weiß nicht, was ich sonst noch vermisse.

Antwort

0

Es funktionierte gut für mich. Ich musste die arguments in die ProcessStartInfo einschließen.

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; 
    } 
}