Pfilips Mor

Pfilips Mor

  • NA
  • 8
  • 491

Unable waiting for web page loading using WebBrowser control

Jan 28 2019 5:39 AM
I am writing a service with screenshots from the web pages of local sites.
I am using WebBrowser control to capture a webpage as an image.
Below is the code I am using:
 
 
 
  1. namespace MakeScreenShotURL  
  2. {  
  3.     class Program  
  4.     {  
  5.         private static Config config = new Config(System.Reflection.Assembly.GetExecutingAssembly().Location);  
  6.         [STAThread]  
  7.         static void Main()  
  8.         {  
  9.             int width = 1700;  
  10.             int height = 1700;  
  11.             string username = "userdata";  
  12.             string password = "passworddata";  
  13.             int currentmonth = DateTime.Now.Month;  
  14.             int nextmonth = DateTime.Now.Month + 1;  
  15.             int year = DateTime.Now.Year;  
  16.             screen(width, height, year, nextmonth, currentmonth, username, password);  
  17.         }  
  18.   
  19.         static void screen(int width, int height, int year, int nextmonth, int currentmonth, string username, string password)  
  20.         {  
  21.             string PlaceCode = config.GetAppSetting("place");  
  22.             string[] Places = PlaceCode.Split(';'); //contains a list of sites for example: Berlin; Munich; Dresden  
  23.             foreach (string pl in Places)    
  24.             {  
  25.                 using (WebBrowser browser = new WebBrowser())  
  26.                 {  
  27.                     browser.Width = width;  
  28.                     browser.Height = height;  
  29.                     browser.ScrollBarsEnabled = true;  
  30.                     Uri uri = new Uri(" http://localsite.php?y=" + year + "&m=" + nextmonth + "&p=" + pl + " ");  
  31.                     string additionalHeaderInfo = "Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)) + System.Environment.NewLine;  
  32.                     browser.Navigate(uri, nullnull, additionalHeaderInfo);  
  33.                     Wait(2);  //waiting for pages to load 2 seconds  
  34.                     using (Graphics graphics = browser.CreateGraphics())  
  35.                     using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))  
  36.                     {  
  37.                         Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);  
  38.                         browser.DrawToBitmap(bitmap, bounds);  
  39.                         bitmap.Save("C:/Users/user/image/screenshot" + pl + ".jpeg", ImageFormat.Jpeg);   
  40.                     }  
  41.                     Application.Run();  
  42.                 }  
  43.             }  
  44.             Application.Exit();  
  45.         }  
  46.         static void Wait(int number)  
  47.         {  
  48.             DateTime time = DateTime.Now;  
  49.             do  
  50.             {  
  51.                 Application.DoEvents();  
  52.             }  
  53.             while (time.AddSeconds(number) > DateTime.Now);  
  54.         }  
  55.     }  
  56. }  
 
 
The program creates a screenshot of the first element (Berlin) from the array of Places and saves it to disk.
But when selecting the second element from the array, the program hangs on the execution after the line:
 
Application.Run ();
 
Perhaps the program expects something but I cannot understand it.
Can someone help me to?
  

Answers (2)