Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: CSharp,Point,Bitmap,Rectangle,WebBrowserDocumentCompletedEventArgs,WebBrowser,IHTMLDocument2, Created At: 10/24/2016 7:58:23 PMViews:

HTML view:
Copy Source | Copy HTML
  1. #region private event handlers
  2.  
  3.  
  4.     /// <summary>
  5.     /// События результата открытия страницы в браузере _webBrowser.Navigate(url.OriginalString);
  6.     /// </summary>
  7.     private unsafe void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  8.     {
  9.         WebBrowser browser = (sender as WebBrowser);
  10.         if (browser != null)
  11.         {
  12.             if(!browser.DocumentText.Contains("<!--Error Body-->"))//если открылось
  13.             {
  14.                 int width = browser.Document.Body.ScrollRectangle.Size.Width;
  15.                 int height = browser.Document.Body.ScrollRectangle.Size.Height;
  16.                 this.Size = browser.Document.Body.ScrollRectangle.Size;
  17.                 this.ClientSize = browser.Document.Body.ScrollRectangle.Size;
  18.  
  19.                 this.Height = 10;
  20.                 this.Width = 20;
  21.                 browser.Width = width;
  22.                 this.Top = -200;
  23.                 browser.Top = -100;
  24.                 browser.Height = 5000;//height;
  25.                 panel.AutoScrollPosition = new Point(300, 300);
  26.                 this.Refresh();
  27.                 //повторный рендеринг страницы тут нужен
  28.                 back.RunWorkerAsync();
  29.             }
  30.         }
  31.     }
  32.  
  33.     void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  34.     {
  35.         WebBrowser browser = this._webBrowser;
  36.         if (browser != null)
  37.         {
  38.             if (!browser.DocumentText.Contains("<!--Error Body-->"))
  39.             {
  40.                 int width = browser.Document.Body.ScrollRectangle.Size.Width;
  41.                 int height = browser.Document.Body.ScrollRectangle.Size.Height;
  42.                 //объект из IE7+
  43.                 mshtml.IHTMLDocument2 document = (browser.Document.DomDocument as mshtml.IHTMLDocument2);
  44.  
  45.                 if (document != null)
  46.                 {
  47.                     mshtml.IHTMLElement element = (document.body as mshtml.IHTMLElement);
  48.                     if (element != null)
  49.                     {
  50.                         IHTMLElementRender render = (element as IHTMLElementRender);
  51.                         if (render != null)
  52.                         {
  53.                             Image img = new Bitmap(width, height);
  54.                             using (Graphics graphics = Graphics.FromImage(img))
  55.                             {
  56.                                 IntPtr hdcDestination = graphics.GetHdc();
  57.                                 render.DrawToDC(hdcDestination);
  58.                                 graphics.ReleaseHdc(hdcDestination);
  59.                               //  browser.DrawToBitmap((Bitmap)img, new Rectangle(0, 0, width, height));
  60.                                 string FileName = wheretosave;
  61.                                 _webBrowser.DrawToBitmap((Bitmap)img, new Rectangle( 0,  0, width, height));
  62.                                 img.Save(FileName);
  63.                             }
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         this.Close();
  70.     }
  71.  
  72.     void back_DoWork(object sender, DoWorkEventArgs e)
  73.     {
  74.         Thread.Sleep(5000);
  75.     }
  76.  
  77.     #endregion

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2017