10

ページ全体だけでなく、ブラウザ画面全体(ツールバー、パネルなどを含む)のスクリーンショットをキャプチャしようとしているので、次のコードを取得しました。

using (FirefoxDriver driver = new FirefoxDriver())
{ 
    driver.Navigate().GoToUrl(url);                

    ScreenCapture sc = new ScreenCapture();

    // How can I find natural IntPtr handle of window here, using GUID-like identifier returning by driver.currentWindowHandle?
    Image img = sc.CaptureWindow(...);
    MemoryStream ms = new MemoryStream();
    img.Save(ms, ImageFormat.Jpeg);
    return new FileStreamResult(ms, "image/jpeg");
}
4

3 に答える 3

3

次を使用してウィンドウハンドルを取得できますProcess.GetProcesses

using (FirefoxDriver driver = new FirefoxDriver())
{
    driver.Navigate().GoToUrl(url);

    string title = String.Format("{0} - Mozilla Firefox", driver.Title);
    var process = Process.GetProcesses()
        .FirstOrDefault(x => x.MainWindowTitle == title);

    if (process != null)
    {
        var screenCapture = new ScreenCapture();
        var image = screenCapture.CaptureWindow(process.MainWindowHandle);
        // ...
    }
}

もちろん、これは、その特定のタイトルを持つ単一のブラウザー インスタンスがあることを前提としています。

于 2012-07-20T09:20:44.763 に答える
2

ちょうどそしてハックのアイデア。Reflectionメソッドを使用して、Firefoxインスタンスのプロセスを取得できます。まず、FirefoxDriverから継承されたFirefoxDriverExクラスを宣言します-プロセスインスタンスをカプセル化する保護されたBinaryプロパティにアクセスします。

 class FirefoxDriverEx : FirefoxDriver {
        public Process GetFirefoxProcess() {
            var fi = typeof(FirefoxBinary).GetField("process", BindingFlags.NonPublic | BindingFlags.Instance);
            return fi.GetValue(this.Binary) as Process;
        }
    }

MainWindowHandleプロパティにアクセスするためのプロセスインスタンスを取得するよりも

using (var driver = new FirefoxDriverEx()) {
            driver.Navigate().GoToUrl(url);

            var process = driver.GetFirefoxProcess();
            if (process != null) {
                var screenCapture = new ScreenCapture();
                var image = screenCapture.CaptureWindow(process.MainWindowHandle);
                // ...
            }
        }
于 2012-07-26T10:10:22.387 に答える