[C#] 如何截取完整的網頁圖片
阿新 • • 發佈:2017-09-07
main emp 保存圖片 app tac bit prop format 加粗
前言
有時候瀏覽到非常有用的網頁時,我們會選擇將它加入到收藏夾中,但是網站一旦過期,以後就看不到這個網頁了。當然也可以將網頁打印成PDF文檔保存。最新的Windows 10中的Edge瀏覽器支持將網頁保存至OneNote中,但在OneNote中其實是保存了一張當前頁面的完整圖片。這篇博客將介紹如何使用C#將完整的頁面保存成圖片。
實現方式
使用WinForms中的WebBrowser來保存圖片,具體DrawToBitmap方法進行保存。新建一個Console程序(添加System.Windows.Forms),
[STAThread] static void Main(string[] args) {int width = 800; int height = 600;using (WebBrowser browser = new WebBrowser()) { browser.Width = width; browser.Height = height; browser.ScrollBarsEnabled = false; browser.ScriptErrorsSuppressed = true; browser.DocumentCompleted += OnDocumentCompleted; browser.Navigate("http://www.cnblogs.com"); Application.Run(); } } private static void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser browser = (WebBrowser)sender; using (Graphics graphics = browser.CreateGraphics()) { int dWidth = browser.Document.Body.ScrollRectangle.Width;int dHeight = browser.Document.Body.ScrollRectangle.Height; browser.Height = dHeight; browser.Width = dWidth; using (Bitmap bitmap = new Bitmap(dWidth, dHeight, graphics)) { Rectangle bounds = new Rectangle(0, 0, dWidth, dHeight); browser.DrawToBitmap(bitmap, bounds); bitmap.Save("Screenshot1.png", ImageFormat.Png); } } Application.Exit(); }
註意加粗的代碼,當頁面加載完成後,需要根據網頁的大小來調整WebBrowser的大小,否則保存的頁面大小就是初始時給WebBrowser設置的大小。
上述代碼中,直接將需要截圖的頁面寫在代碼中,如果能夠直接讀取當前IE/FireFox/Chrome打開的頁面,直接截圖就完美了。
改進
增加獲取當前IE/FireFox/Chrome打開的頁面,
代碼參考自:https://stackoverflow.com/questions/5317642/retrieve-current-url-from-c-sharp-windows-forms-application
添加UIAutomationClient和UIAutomationTypes引用,
public static string GetChromeUrl(Process process) { if (process == null) throw new ArgumentNullException("process"); if (process.MainWindowHandle == IntPtr.Zero) return null; AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle); if (element == null) return null; AutomationElement edit = element.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); return ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string; } public static string GetInternetExplorerUrl(Process process) { if (process == null) throw new ArgumentNullException("process"); if (process.MainWindowHandle == IntPtr.Zero) return null; AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle); if (element == null) return null; AutomationElement rebar = element.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "ReBarWindow32")); if (rebar == null) return null; AutomationElement edit = rebar.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); return ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string; } public static string GetFirefoxUrl(Process process) { if (process == null) throw new ArgumentNullException("process"); if (process.MainWindowHandle == IntPtr.Zero) return null; AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle); if (element == null) return null; AutomationElement doc = element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)); if (doc == null) return null; return ((ValuePattern)doc.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string; }
根據瀏覽器進程查找當前Active的頁面,
string url = string.Empty; foreach (Process process in Process.GetProcessesByName("firefox")) { url = GetFirefoxUrl(process); if (url != null) { // Find the target Url break; } } foreach (Process process in Process.GetProcessesByName("iexplore")) { url = GetInternetExplorerUrl(process); if (url != null) { // Find the target Url break; } } foreach (Process process in Process.GetProcessesByName("chrome")) { url = GetChromeUrl(process); if (url != null) { // Find the target Url break; } }
此時就不需要手動的修改需要截圖的Url地址了,直接一鍵截圖~
感謝您的閱讀~ 代碼點擊這裏下載。
[C#] 如何截取完整的網頁圖片