.NET Webpage Screenshot is Not Rendering CSS

Hello,

I am wanting to create a screenshot of a Webpage using .NET to do so, and then save the image to a file.

I can do all of these things, but the problem is the code is taking the screenshot before any CSS renders on the page.

Below is what the Website is showing:
This is correct.
But this is what the screenshot code is rendering:
This, obviously, is not correct.
The CSS is moving and rotating images so they end up in the correct place. Obviously, the code is taking the snapshot before this happens, or just completely ignoring the CSS file.

Here's the code (found online)
    public class WebsiteToImage
    {
        private Bitmap m_Bitmap;
        private string m_Url;
        private string m_FileName = string.Empty;
        private int m_Width = 1000;
        private int m_Height = 1000;

        public WebsiteToImage(string url, string fileName, int width, int height)
        {
            // With file 
            m_Url = url;
            m_FileName = fileName;
            m_Width = width;
            m_Height = height;
        }

        public void Capture()
        {
            Thread thread = new Thread(delegate()
                {
                    using (WebBrowser browser = new WebBrowser())
                    {
                        browser.ScrollBarsEnabled = false;
                        browser.AllowNavigation = false;
                        browser.ScriptErrorsSuppressed = true;
                        browser.Navigate(m_Url);
                        browser.ClientSize = new Size(m_Width, m_Height);
                        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
                        while (browser.ReadyState != WebBrowserReadyState.Complete)
                        {
                            Application.DoEvents();
                        }
                    }
                });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }
        private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser browser = sender as WebBrowser;
            using (Bitmap bitmap = new Bitmap(m_Width, browser.Height))
            {
                browser.DrawToBitmap(bitmap, new Rectangle(0, 0, m_Width, browser.Height));
                bitmap.SaveJPG100(m_FileName);
            }
        }
    }
        public static void SaveJPG100(this Bitmap bmp, string filename)
        {
            var encoderParameters = new EncoderParameters(1);
            encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
            bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
        }

Open in new window


Any ideas on making this work (or completely different code altogether)?
infotechelgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

infotechelgAuthor Commented:
anyone?
0
infotechelgAuthor Commented:
Well, I fixed it. Here was the problem.

I commented out browser.ScriptErrorsSuppressed = true; from the code and noticed I was getting script errors.

There were occurring on my <script src=""> calls to the googleapi for JQUERY. I moved the files locally and those script errors went away. When I did that, I got a new script error that said "object doesn't support addEventlistener"

So then I added this to the top of the page and everything worked:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

Hopefully this will help anyone else having this problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.