0

I am using hangfire to update the screen shot on every 3 minute. There are multiple URL for getting screen shot

For updating screenshot i am using WebBrowser

My hangfire code is as bellow:

public ActionResult Recurring()
{
  RecurringJob.AddOrUpdate(() => JobExicute(), Cron.MinuteInterval(3));
}
public void JobExicute()
{
Thread _t1 = new Thread(new ThreadStart(MPLS1));
            _t1.SetApartmentState(ApartmentState.STA);
            _t1.Start();
            _t1.Join();

            Thread _t2 = new Thread(MPLS2);
            _t2.SetApartmentState(ApartmentState.STA);
            _t2.Start();

            Thread _t3 = new Thread(MPLS3);
            _t3.SetApartmentState(ApartmentState.STA);
            _t3.Start();

}

public static void MPLS1()
        {

            Logger.Log("Services is strated on " + DateTime.Now + "", 8);

            try
            {
                using (WebBrowser Browser = new WebBrowser())
                {
                    Browser.ScrollBarsEnabled = false;
                    Browser.ScriptErrorsSuppressed = true;
                    //sw.WriteLine("On WebBrowser");

                    DateTime start = DateTime.Now;

                    Browser.Navigate("http://myURL1 ");
                    while (Browser.ReadyState != WebBrowserReadyState.Complete || start > DateTime.Now.AddSeconds(-30))
                    {
                        Application.DoEvents();
                        System.Threading.Thread.Sleep(100);
                    }
                    //Browser.Width=Browser.Document.Body.ScrollRectangle.Width;
                    //Browser.Height=Browser.Document.Body.ScrollRectangle.Height;

                    Browser.Width = 4000;
                    Browser.Height = 3000;//Browser.Document.Body.ScrollRectangle.Height;


                    using (Bitmap bitmap = new Bitmap(3000, 2000))
                    {
                        string MapPath = ConfigurationManager.AppSettings["MapPath"].ToString();
                        Browser.DrawToBitmap(bitmap, new Rectangle(0, 0, 3000, 2000));
                        Image img = bitmap;
                        bitmap.Save(@"" + MapPath + "screeshot1.bmp", ImageFormat.Png);

                    }
                    //sw.WriteLine("Services is completed on " + DateTime.Now + "");

                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error Message: " + ex.Message, 8);
                Logger.Log("Stack Trace: " + ex.StackTrace, 8);
            }
            finally
            {
                Logger.Log("Services is End on " + DateTime.Now + "", 8);
            }
        }

Same like MPLS2, MPLS3 are same as MPLS1 but URL is different.
But in log file i am getting error as bellow:

Error Message: Error HRESULT E_FAIL has been returned from a call to a COM component. Stack Trace: at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers) at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers) at System.Windows.Forms.WebBrowser.Navigate(String urlString) at HangfireCronJobs.Controllers.HomeController.MPLS1()


Please give me better solution.
Thanks for advance.

  • ``Application.DoEvents();`` in C# something seems odd. – Rand Random Jun 28 '17 at 07:30
  • Whats your URL? Is that just a plain Image or some HTML? – Rand Random Jun 28 '17 at 07:33
  • It is a MAP in HTML format. The map is updating every a minute. I want to get a screen shot as a bitmap format and save in directory. – Rahmany Usama Jun 28 '17 at 07:42
  • Instead of the browser control you could try getting the HTML with WebRequest and than use HtmlRender to render the html as Image. – Rand Random Jun 28 '17 at 07:46
  • Some user posted a question about HTML to Image not long ago: https://stackoverflow.com/questions/44795918/how-to-convert-html-code-into-images-using-code-behind-c-sharp – Rand Random Jun 28 '17 at 07:47
  • Thank you for a better suggestion. I really appreciate you. – Rahmany Usama Jun 28 '17 at 07:51
  • HtmlRenderer.HtmlRender.Render is license version or free? – Rahmany Usama Jun 29 '17 at 06:10
  • https://github.com/ArthurHub/HTML-Renderer/blob/master/LICENSE – Rand Random Jun 29 '17 at 06:16
  • some similar discussion here : https://social.msdn.microsoft.com/Forums/windows/en-US/3ab03be2-5644-41af-89e2-323b07561628/windows-service-and-webbrowser-control?forum=winforms .. not sure it will be of much help, but worth having a look at, as the discussion is about the same issue but in Windows Service – Sujith Jun 29 '17 at 08:40

0 Answers0