Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:51:26+00:00 2026-05-15T07:51:26+00:00

I’m using the WebBrowser control in an ASP.NET MVC 2 app (don’t judge, I’m

  • 0

I’m using the WebBrowser control in an ASP.NET MVC 2 app (don’t judge, I’m doing it in an admin section only to be used by me), here’s the code:

public static class Screenshot
    {
        private static string _url;
        private static int _width;
        private static byte[] _bytes;

        public static byte[] Get(string url)
        {
            // This method gets a screenshot of the webpage
            // rendered at its full size (height and width)
            return Get(url, 50);
        }

        public static byte[] Get(string url, int width)
        {
            //set properties.
            _url = url;
            _width = width;
            //start screen scraper.
            var webBrowseThread = new Thread(new ThreadStart(TakeScreenshot));
            webBrowseThread.SetApartmentState(ApartmentState.STA);
            webBrowseThread.Start();
            //check every second if it got the screenshot yet.
            //i know, the thread sleep is terrible, but it's the secure section, don't judge...
            int numChecks = 20;
            for (int k = 0; k < numChecks; k++)
            {
                Thread.Sleep(1000);
                if (_bytes != null)
                {
                    return _bytes;
                }
            }
            return null;
        }

        private static void TakeScreenshot()
        {
            try
            {
                //load the webpage into a WebBrowser control.
                using (WebBrowser wb = new WebBrowser())
                {
                    wb.ScrollBarsEnabled = false;
                    wb.ScriptErrorsSuppressed = true;
                    wb.Navigate(_url);
                    while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
                    //set the size of the WebBrowser control.
                    //take Screenshot of the web pages full width.
                    wb.Width = wb.Document.Body.ScrollRectangle.Width;
                    //take Screenshot of the web pages full height.
                    wb.Height = wb.Document.Body.ScrollRectangle.Height;
                    //get a Bitmap representation of the webpage as it's rendered in the WebBrowser control.
                    var bitmap = new Bitmap(wb.Width, wb.Height);
                    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
                    //resize.
                    var height = _width * (bitmap.Height / bitmap.Width);
                    var thumbnail = bitmap.GetThumbnailImage(_width, height, null, IntPtr.Zero);
                    //convert to byte array.
                    var ms = new MemoryStream();
                    thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    _bytes = ms.ToArray();
                }
            }
            catch(Exception exc)
            {//TODO: why did screenshot fail?
                string message = exc.Message;
            }
        }

This works fine for the first screenshot that I take, however if I try to take subsequent screenshots of different URL’s, it saves screenshots of the first url for the new url, or sometimes it’ll save the screenshot from 3 or 4 url’s ago. I’m creating a new instance of WebBrowser for each screenshot and am disposing of it properly with the “using” block, any idea why it’s behaving this way?

Thanks,
Justin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:51:27+00:00Added an answer on May 15, 2026 at 7:51 am

    You’re using a static field to store the current URL so multiple requests (and so multiple threads) have a chance to navigate to the same URL since you’re not synchronizing any access. You should either use a synchronized queue that will process one screenshot request by one screenshot request, or consider making your whole class non-static so that you can instantiate one per request.

    On a side note, use an AutoResetEvent or another signaling mechanism to perform the waiting, rather than sleeping in a loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.