[C#] 如何截取完整的网页图片

前言

有时候浏览到非常有用的网页时,我们会选择将它加入到收藏夹中,但是网站一旦过期,以后就看不到这个网页了。当然也可以将网页打印成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地址了,直接一键截图~

感谢您的阅读~ 代码点击这里下载。

时间: 2024-08-24 18:07:18

[C#] 如何截取完整的网页图片的相关文章

一个咸鱼的Python爬虫之路(三):爬取网页图片

学完Requests库与Beautifulsoup库我们今天来实战一波,爬取网页图片.依照现在所学只能爬取图片在html页面的而不能爬取由JavaScript生成的图.所以我找了这个网站http://www.ivsky.com 网站里面有很多的图集,我们就找你的名字这个图集来爬取 http://www.ivsky.com/bizhi/yourname_v39947/ 来看看这个页面的源代码: 可以看到我们想抓取的图片信息在<li> 里面然后图片地址在img里面那么我们这里可以用Beautifu

java 抓取网页图片

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

制作手机使用的网页图片查看器

这几天抽空在为项目开发一个量身的图片查看器,目前已初步完成需求 开发场景是:在一个多文件下载展示列表中,如检测某些文件为图片时,则点击该文件时打开图片查看器展示该图片,并将列表内其它图片同时展示查看器队列内,可供前后滑动查看及其它附带功能 乍一听功能点似乎有点多而且有些复杂,需要梳理一下 功能点整理 首先,我们要获得点击的图片文件对象及符合条件的图片文件对象集 其次,图片查看器的制作及图片队列展示 然后,图片友好加载方式 最后,图片查看器触摸滑动及滑动后相关功能的实现 简单整理了一下,好像也不多

托管到github上的网页图片在百度浏览器中显示不全

这几天做了个较完整的网页放到github上,上传后看网页效果. 在Firefox浏览器中,显示正常. 在百度浏览器中,空了一大块位置(图片位置),偏偏只空了这一块,其它地方的图片都好好的. 点击f12查看,源代码中的<img>也不见了. 将错误信息放到百度上搜,有网友解释是因为 有"ad" 或"ads", 而我那几张图片就放在"ad"文件夹中. 更改文件名后,正常显示了.我........ 应该是百度浏览器里面有个什么插件过滤掉&qu

Webdriver中实现区域截图的方式以及如何截取frame中的图片

import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import org.apache.commons.io.FileUtils;import org.openqa.selenium.OutputType;import org.openqa.selenium.Point;im

网页图片的尺寸、分辨率、物理尺寸的理解。

1.我们通常所说的网页图片大小500*270,是指500px*270px(即图片长和宽上的像素数): 2.分辨率,指的是单位长度(物理长度)上的像素数.(网页图片最常用的分辨率是72 像素/英寸) 3.物理尺寸指的是图片实际外显时的物理尺寸(常用单位厘米或英寸) 三者关系:图片(像素)尺寸=分辨率*物理尺寸: 图片大小:200kb,即图像所占内存大小. (备注:像素不是长度单位,它是虚单位,需要分辨率这个桥梁,才能和实际物理尺寸对应)

Python -- 网络编程 -- 抓取网页图片 -- 图虫网

字符串(str)编码成字节码(bytes),字节码解码为字符串 获取当前环境编码:sys.stdin.encoding url编码urllib.parse.quote() url解码urllib.parse.unquote() 列表去重:pages = list(set(pages)) 创建文件夹(可多级创建):os.makedirs(folder)  os.mkdir()只能单级创建 首先分析网页(图虫网)的URL规律: 根网页地址形如: http://tuchong.com/tags/人像/

Python3简单爬虫抓取网页图片

现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2),所以我用Python3的语法写了一个简单抓取网页图片的实例,希望能够帮助到大家,并希望大家批评指正. 1 import urllib.request 2 import re 3 import os 4 import urllib 5 #根据给定的网址来获取网页详细信息,得到的html就是网页的源代码 6 def getHtml(url): 7 page = urllib.r

Python爬虫 网页图片

一 概述 参考http://www.cnblogs.com/abelsu/p/4540711.html 弄了个Python捉取单一网页的图片,但是Python已经升到3+版本了.参考的已经失效,基本用不上.修改了下,重新实现网页图片捉取. 二 代码 #coding=utf-8 #urllib模块提供了读取Web页面数据的接口 import urllib #re模块主要包含了正则表达式 import re import urllib.parse import urllib.request #定义一