.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?
 
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
 
infotechelgAuthor Commented:
anyone?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.