在webBrowser1_DocumentCompleted中调用我的函数并没有完成加载浏览器

时间:2014-11-15 19:35:17

标签: c# webbrowser-control

我想在视频的前30秒拍摄ScreenShoot()。但是当我在webBrowser1_DocumentCompleted中调用它时,它会停止加载视频。因此,屏幕截图将为空白。这只是我的代码的粗略副本。有没有办法在所有内容完全加载并播放视频后调用takeScreenShoot()?或者在newForm.ShowDialog()之后调用它; ?

main(
Form1 newForm = new Form1();
newForm.loadStream();           
newForm.ShowDialog();

}

public void loadVideo()
    {
        //// When the form loads, open this web page.
        //this.webBrowser1.Navigate("http://www.dotnetperls.com/");

        SuppressScriptErrorsOnly(webBrowser1);

    //set browser eumulator to IE8
        var appName = Process.GetCurrentProcess().ProcessName + ".exe";
        SetIE8KeyforWebBrowserControl(appName);


        webBrowser1.Navigate("http://youtu.be/e-ORhEE9VVg?list=PLFgquLnL59alLAsVmUulfe3X-BrPzoYAH");
        webBrowser1.ScriptErrorsSuppressed = true;
        //Console.WriteLine("loading new new");
    }

public void takeScreenShoot()
    {           

        DateTime startTime = DateTime.Now;
        int i = 0;
        string location = @"F:\Twitch Screenshoot\testing\";

            while (true)
            {

                //If time is greater than 30 seconds start taking picture
                if (DateTime.Now.Subtract(startTime).TotalSeconds > 1) 
                {
                    if (DateTime.Now.Subtract(startTime).TotalSeconds > i + 1)
                    {
                        Console.WriteLine("Taking Picture\n");
                        // The size of the browser window when we want to take the screenshot (and the size of the resulting bitmap)
                        Bitmap bitmap = new Bitmap(1024, 768);
                        Rectangle bitmapRect = new Rectangle(0, 0, 1024, 768);
                        // This is a method of the WebBrowser control, and the most important part

                        this.webBrowser1.DrawToBitmap(bitmap, bitmapRect);


                        // Generate a thumbnail of the screenshot (optional)
                        System.Drawing.Image origImage = bitmap;
                        System.Drawing.Image origThumbnail = new Bitmap(120, 90, origImage.PixelFormat);

                        Graphics oGraphic = Graphics.FromImage(origThumbnail);
                        oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                        oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                        oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Rectangle oRectangle = new Rectangle(0, 0, 120, 90);
                        oGraphic.DrawImage(origImage, oRectangle);

                        // Save the file in PNG format

                        origThumbnail.Save(location + "Screenshot" + i + ".png", ImageFormat.Png);

                        origImage.Dispose();
                        i++;
                    }
                }

                //stop taking picture when time is greater than 3.
                if (DateTime.Now.Subtract(startTime).TotalMinutes > 1)
                {
                    //should close form here
                    this.Close();
                    break;
                }               
            }        
    }

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

    if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
        {
            var webBrowser = sender as WebBrowser;
            webBrowser.DocumentCompleted -= webBrowser1_DocumentCompleted;
            Console.WriteLine("loading {0} \n");
            this.takeScreenShoot();

        }          

    }

1 个答案:

答案 0 :(得分:1)

(发表评论作为回答,让问题关闭)

我非常确定您关联的视频是Adobe Flash的问题。但是taking the screenshot with selenium可能有用。