[C#] 如何截取完整的網頁圖片

前言

有時候瀏覽到很是有用的網頁時,咱們會選擇將它加入到收藏夾中,可是網站一旦過時,之後就看不到這個網頁了。固然也能夠將網頁打印成PDF文檔保存。最新的Windows 10中的Edge瀏覽器支持將網頁保存至OneNote中,但在OneNote中實際上是保存了一張當前頁面的完整圖片。這篇博客將介紹如何使用C#將完整的頁面保存成圖片。web

實現方式

使用WinForms中的WebBrowser來保存圖片,具體DrawToBitmap方法進行保存。新建一個Console程序(添加System.Windows.Forms),chrome

[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設置的大小。windows

上述代碼中,直接將須要截圖的頁面寫在代碼中,若是可以直接讀取當前IE/FireFox/Chrome打開的頁面,直接截圖就完美了。瀏覽器

改進

增長獲取當前IE/FireFox/Chrome打開的頁面,app

代碼參考自:https://stackoverflow.com/questions/5317642/retrieve-current-url-from-c-sharp-windows-forms-application網站

添加UIAutomationClient和UIAutomationTypes引用,url

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的頁面,spa

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地址了,直接一鍵截圖~firefox

感謝您的閱讀~ 代碼點擊這裏下載。code

相關文章
相關標籤/搜索