private void btnPrint_Click(object sender, RoutedEventArgs e) { mshtml.IHTMLDocument2 doc = webBrowser1.Document as mshtml.IHTMLDocument2; doc.execCommand("Print", true, 0); doc.close(); ViewModel.SetPrintControlsVisibility(false); }
private void File_Print(object sender, RoutedEventArgs e) { string printContents; printContents = ViewModel.GetPrintContents(); mshtml.IHTMLDocument2 doc = webBrowser1.Document as mshtml.IHTMLDocument2; doc.clear(); doc.write(printContents); doc.execCommand("Print", true, 0); doc.close(); }
private void File_PrintPreview(object sender, RoutedEventArgs e) { string printContents; printContents = ViewModel.GetPrintContents(); mshtml.IHTMLDocument2 doc = webBrowser1.Document as mshtml.IHTMLDocument2; doc.clear(); doc.write(printContents); doc.close(); ViewModel.SetPrintControlsVisibility(true); }
void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { doc = (IHTMLDocument2)b.Document.DomDocument; string result = "<html>"; IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)doc.all.tags("head")).item(null, 0); result += "<head>" + head.innerHTML + "</head>"; if (null != doc) { foreach (IHTMLElement element in doc.all) { if (element is mshtml.IHTMLDivElement) { dynamic div = element as HTMLDivElement; if (FindByID) { string id = div.id; if (id == Element) { result += "<body>" + div.IHTMLElement_innerHTML + "</body></html>"; break; } } else { string className = div.className; if (className == Element) { result += "<body>" + div.IHTMLElement_innerHTML + "</body></html>"; break; } } } } } doc.close(); this.Add(result); }
private async Task <bool> ParseHTML(HttpContent content) { UpdateStatus(">> Trying to parse a HTML document..."); //Stream st = content.ReadAsStreamAsync().Result; string s = await content.ReadAsStringAsync(); UpdateStatus(">> Loading a HTML document..."); mshtml.HTMLDocument hd = new mshtml.HTMLDocument(); mshtml.IHTMLDocument2 hdoc = (mshtml.IHTMLDocument2)hd; IPersistStreamInit ips = (IPersistStreamInit)hdoc; ips.InitNew(); hdoc.designMode = "On"; hdoc.clear(); hdoc.write(s); hdoc.close(); // In Delphi, it's just // hdoc:= CreateComObject(Class_HTMLDocument) as IHTMLDocument2; // (hdoc as IPersistStreamInit).Load(TStreamAdapter.Create(Response.Stream)); int i = 0; while (hdoc.readyState != "complete") { await Task.Delay(100); if (i > 500) { throw new Exception(string.Format("The document {0} timed out while loading", "IHTMLDocument2")); } i++; } if (hdoc.readyState == "complete") { UpdateStatus(">> Checking HTML link tags..."); IHTMLElementCollection ElementCollection = hdoc.all; foreach (var e in ElementCollection) { if ((e as IHTMLElement).tagName == "LINK") { string re = (e as IHTMLElement).getAttribute("rel", 0); if (!string.IsNullOrEmpty(re)) { if (re.ToUpper() == "EDITURI") { string hf = (e as IHTMLElement).getAttribute("href", 0); if (!string.IsNullOrEmpty(hf)) { _serviceDocKind = _serviceDocumentKind.RSD; _serviceDocUrl = hf; Debug.WriteLine("ServiceDocumentKind is: RSD " + _serviceDocUrl); UpdateStatus("Found a link to a RSD documnet."); } } else if (re.ToUpper() == "SERVICE") { string ty = (e as IHTMLElement).getAttribute("type", 0); if (!string.IsNullOrEmpty(ty)) { if (ty == "application/atomsvc+xml") { string hf = (e as IHTMLElement).getAttribute("href", 0); if (!string.IsNullOrEmpty(hf)) { _serviceDocKind = _serviceDocumentKind.AtomSrv; _serviceDocUrl = hf; Debug.WriteLine("ServiceDocumentKind is: AtomSrv " + _serviceDocUrl); UpdateStatus("Found a link to an Atom service documnet."); } } } } else if (re.ToUpper() == "ALTERNATE") { string ty = (e as IHTMLElement).getAttribute("type", 0); if (!string.IsNullOrEmpty(ty)) { if (ty == "application/atom+xml") { string hf = (e as IHTMLElement).getAttribute("href", 0); if (!string.IsNullOrEmpty(hf)) { Debug.WriteLine("Atom feed found."); try { _atomFeedUrl = new Uri(hf); } catch { } UpdateStatus("Found a link to an Atom feed."); } } } } } } } } return(true); }
public void GetPageImage(string url, string path) { //Test Is OK if (TestInternet(url)) { bool flag = firstScreen; m_browser.Navigate(url); //浏览器Dom载入完毕 while (m_browser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } mshtml.IHTMLDocument2 myDoc = (mshtml.IHTMLDocument2)m_browser.Document.DomDocument; //处理由于webbrowser上、左边框阴影带来的拼接bug int URLExtraHeight = 3; int URLExtraLeft = 3; (myDoc as HTMLDocumentClass).documentElement.setAttribute("scroll", "yes", 0); //document完整高度 int heightsize = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollHeight", 0); int widthsize = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollWidth", 0); ////Get Screen Height int screenHeight = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("clientHeight", 0); int screenWidth = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("clientWidth", 0); IntPtr myIntptr = (IntPtr)m_browser.Handle; //寻找IE对象句柄 IntPtr wbHandle = FindWindowEx(myIntptr, IntPtr.Zero, "Shell Embedding", null); wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Shell DocObject View", null); wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Internet Explorer_Server", null); IntPtr hwnd = wbHandle; Bitmap bm = new Bitmap(screenWidth, screenHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Bitmap bm2 = new Bitmap(widthsize, heightsize, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Graphics g = null; Graphics g2 = Graphics.FromImage(bm2); //切割用的临时对象 Bitmap tempbm = null; Graphics tempg = null; IntPtr hdc; Image screenfrag = null; Image firstScreenfrag = null; #region 拼接 int brwTop = 0; int brwLeft = 0; int myPage = 0; //Get Screen Height (for bottom up screen drawing) while ((myPage * screenHeight) < heightsize) { (myDoc as HTMLDocumentClass).documentElement.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0); ++myPage; } //Rollback the page count by one --myPage; int myPageWidth = 0; //screenWidth+ URLExtraLeft while ((myPageWidth * screenWidth) < widthsize) { (myDoc as HTMLDocumentClass).documentElement.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0); brwLeft = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollLeft", 0); for (int i = myPage; i >= 0; --i) { //Shoot visible window g = Graphics.FromImage(bm); hdc = g.GetHdc(); (myDoc as HTMLDocumentClass).documentElement.setAttribute("scrollTop", (screenHeight - 5) * i, 0); brwTop = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollTop", 0); PrintWindow(hwnd, hdc, 0); g.ReleaseHdc(hdc); g.Flush(); screenfrag = Image.FromHbitmap(bm.GetHbitmap()); //切除URLExtraLeft、URLExtraHeight长度的webbrowser带来的bug tempbm = new Bitmap(screenWidth - URLExtraLeft, screenHeight - URLExtraHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); tempg = Graphics.FromImage(tempbm); tempg.DrawImage(screenfrag, -URLExtraLeft, -URLExtraHeight); //拼接到g2 g2.DrawImage(tempbm, brwLeft + URLExtraLeft, brwTop + URLExtraLeft); } //是否得到第一屏 if (flag) { firstScreenfrag = (Image)tempbm.Clone(); flag = false; } ++myPageWidth; } int finalWidth = (int)widthsize; int finalHeight = (int)heightsize; Bitmap finalImage = new Bitmap(finalWidth, finalHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Graphics gFinal = Graphics.FromImage((Image)finalImage); gFinal.DrawImage(bm2, 0, 0, finalWidth, finalHeight); #endregion //Get Time Stamp DateTime myTime = DateTime.Now; String format = "yyyy_MM_dd_hh_mm_ss"; //Create Directory to save image to. Directory.CreateDirectory(path); //Write Image. EncoderParameters eps = new EncoderParameters(1); long myQuality = Convert.ToInt64(100); eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, myQuality); ImageCodecInfo ici = GetEncoderInfo("image/jpeg"); //保存 finalImage.Save(path + @"\Img_" + myTime.ToString(format) + ".jpg", ici, eps); //保存第一屏 if (firstScreen) { firstScreenfrag.Save(path + @"\Img_FirstScreen_" + myTime.ToString(format) + ".jpg", ici, eps); firstScreenfrag.Dispose(); } //Process.Start("explorer.exe", path); #region Clean //Clean Up. myDoc.close(); myDoc = null; g.Dispose(); g2.Dispose(); gFinal.Dispose(); bm.Dispose(); bm2.Dispose(); finalImage.Dispose(); tempbm.Dispose(); tempg.Dispose(); #endregion } }
public void GetPageImage(string url, string path) { bool flag = firstScreen; int URLExtraHeight = 3; int URLExtraLeft = 3; mshtml.IHTMLDocument2 myDoc = (mshtml.IHTMLDocument2)m_wbBrowser.Document.DomDocument; (myDoc as HTMLDocumentClass).documentElement.setAttribute("scroll", "yes", 0); //document high int heightsize = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollHeight", 0); int widthsize = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollWidth", 0); ////Get Screen Height int screenHeight = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("clientHeight", 0); int screenWidth = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("clientWidth", 0); IntPtr myIntptr = (IntPtr)m_wbBrowser.Handle; IntPtr wbHandle = FindWindowEx(myIntptr, IntPtr.Zero, "Shell Embedding", null); wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Shell DocObject View", null); wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Internet Explorer_Server", null); IntPtr hwnd = wbHandle; Bitmap bm = new Bitmap(screenWidth, screenHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); //.Format16bppRgb555); Bitmap bm2 = new Bitmap(widthsize, heightsize, System.Drawing.Imaging.PixelFormat.Format24bppRgb); //.Format16bppRgb555); Graphics g = null; Graphics g2 = Graphics.FromImage(bm2); Bitmap tempbm = null; Graphics tempg = null; IntPtr hdc; Image screenfrag = null; Image firstScreenfrag = null; int brwTop = 0; int brwLeft = 0; int myPage = 0; while ((myPage * screenHeight) < heightsize) { (myDoc as HTMLDocumentClass).documentElement.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0); ++myPage; } --myPage; int myPageWidth = 0; while ((myPageWidth * screenWidth) < widthsize) { (myDoc as HTMLDocumentClass).documentElement.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0); brwLeft = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollLeft", 0); for (int i = myPage; i >= 0; --i) { g = Graphics.FromImage(bm); hdc = g.GetHdc(); (myDoc as HTMLDocumentClass).documentElement.setAttribute("scrollTop", (screenHeight - 5) * i, 0); brwTop = (int)(myDoc as HTMLDocumentClass).documentElement.getAttribute("scrollTop", 0); PrintWindow(hwnd, hdc, 0); g.ReleaseHdc(hdc); g.Flush(); screenfrag = Image.FromHbitmap(bm.GetHbitmap()); tempbm = new Bitmap(screenWidth - URLExtraLeft, screenHeight - URLExtraHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);// Format16bppRgb555); tempg = Graphics.FromImage(tempbm); tempg.DrawImage(screenfrag, -URLExtraLeft, -URLExtraHeight); g2.DrawImage(tempbm, brwLeft + URLExtraLeft, brwTop + URLExtraLeft); System.Threading.Thread.Sleep(100); } if (flag) { firstScreenfrag = (Image)tempbm.Clone(); flag = false; } ++myPageWidth; } int finalWidth = (int)widthsize; int finalHeight = (int)heightsize; Bitmap finalImage = new Bitmap(finalWidth, finalHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);// Format16bppRgb555); Graphics gFinal = Graphics.FromImage((Image)finalImage); gFinal.DrawImage(bm2, 0, 0, finalWidth, finalHeight); //Get Time Stamp DateTime myTime = DateTime.Now; String format = "yyyyMMdd-hhmmss"; EncoderParameters eps = new EncoderParameters(1); long myQuality = Convert.ToInt64(100); eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, myQuality); ImageCodecInfo sICI = GetImageInfo("image/jpeg"); finalImage.Save(path + myTime.ToString(format) + ".jpg", sICI, eps); if (firstScreen) { firstScreenfrag.Save(path + @"First-" + myTime.ToString(format) + ".jpg", sICI, eps); firstScreenfrag.Dispose(); } /// Clean myDoc.close(); myDoc = null; g.Dispose(); g2.Dispose(); gFinal.Dispose(); bm.Dispose(); bm2.Dispose(); finalImage.Dispose(); tempbm.Dispose(); tempg.Dispose(); }