简体   繁体   中英

Calling my function in webBrowser1_DocumentCompleted does not finish loading the browser

I want to takeScreenShoot() for the first 30 seconds of the video. But when I call it in webBrowser1_DocumentCompleted it stops the video from loading. So the screenshoots will be blank. This is just a rough copy of my code. Is there a way to call takeScreenShoot() after everything is completely loaded and video is playing? or calling it after 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();

        }          

    }

(Making comment as answer to allow question to close)

I'm pretty sure the issue that the video you linked is Adobe Flash. However taking the screenshot with selenium might work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM