前言

有时候浏览到非常有用的网页时,我们会选择将它加入到收藏夹中,但是网站一旦过期,以后就看不到这个网页了。当然也可以将网页打印成PDF文档保存。最新的Windows 10中的Edge浏览器支持将网页保存至OneNote中,但在OneNote中其实是保存了一张当前页面的完整图片。这篇博客将介绍如何使用C#将完整的页面保存成图片。

实现方式

使用WinForms中的WebBrowser来保存图片,具体DrawToBitmap方法进行保存。新建一个Console程序(添加System.Windows.Forms),

[STAThread]
static void Main(string[] args)
{
int width = ;
int height = ;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(, , 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#] 如何截取完整的网页图片的更多相关文章

  1. 截取完整网页图片的方法 Chrome 浏览器

    截取完整网页图片的方法:https://sspai.com/post/42193 要想使用截图功能,你需要首先确保 Chrome 已升级至 59 或更高版本.在想要截图的网页中,首先按下 ⌘Comma ...

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

    这几天做了个较完整的网页放到github上,上传后看网页效果. 在Firefox浏览器中,显示正常. 在百度浏览器中,空了一大块位置(图片位置),偏偏只空了这一块,其它地方的图片都好好的. 点击f12 ...

  3. node:爬虫爬取网页图片

    代码地址如下:http://www.demodashi.com/demo/13845.html 前言 周末自己在家闲着没事,刷着微信,玩着手机,发现自己的微信头像该换了,就去网上找了一下头像,看着图片 ...

  4. JAVA多线程超时加载当网页图片

    先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: “http://xxxx/xxxx/xxx.jpg” 'http: ...

  5. java 抓取网页图片

    import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Out ...

  6. Python爬虫 网页图片

    一 概述 参考http://www.cnblogs.com/abelsu/p/4540711.html 弄了个Python捉取单一网页的图片,但是Python已经升到3+版本了.参考的已经失效,基本用 ...

  7. android通过Canvas和Paint截取无锯齿圆形图片

    一个通过Canvas和Paint截取无锯齿圆形图片. /** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ public st ...

  8. 14种网页图片和文字特效的jQuery插件代码

    1.网页图片3d旋转jQuery代码 演示和下载地址 2.存css3实现的tabl选项卡代码 演示和下载地址 3.jQuery标签旋转代码 演示和下载地址 4.鼠标悬浮的图片选项卡代码 演示和下载地址 ...

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

    import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOE ...

随机推荐

  1. Ubuntu16.04配置Mac主题

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7072878.html 觉得有帮助?欢迎来打赏 Ubuntu配置Mac主题 下 ...

  2. HA分布式集群二hive配置

    一,概念 hive:是一种数据仓库,数据储存在:hdfs上,hsql是由替换简单的map-reduce,hive通过mysql来记录映射数据 二,安装 1,mysql安装: 1,检测是否有mariad ...

  3. 数据库中间件 Sharding-JDBC 源码分析 —— SQL 解析(三)之查询SQL

  4. Solr(四)Solr实现简单的类似百度搜索高亮功能-1.配置Ik分词器

    配置Ik分词器 一 效果图 二 实现此功能需要添加分词器,在这里使用比较主流的IK分词器. 1 没有配置IK分词器,用solr自带的text分词它会把一句话分成单个的字. 2 配置IK分词器,的话它会 ...

  5. geotrellis使用(三十)使用geotrellis读取PostGIS空间数据

    前言 最近事情很多,各种你想不到的事情--such as singing and dancing--再加上最近又研究docker上瘾,所以geotrellis看上去似乎没有关注,其实我一直在脑中思考着 ...

  6. mysql中varchar和char区别(思维导图整理)

    由于mysql一直是我的弱项(其实各方面我都是很弱的),所以最近在看msyql,正好看到varchar和char区别,所以整理一下,便于以后遗忘. 0.0图片已经说明一切,但是系统说我字数不够,我真能 ...

  7. Hibernate_01

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  8. Handlebars模板引擎渲染页面

    基本使用 js: var testTpl = Handlebars.compile($('#test').html()); //模板 var arr = [1,2,3] //数据 $('#box'). ...

  9. 【性能测试工具】- Siege

    优点:比http_load好用,因为相同的url.txt文件在httpload不好使,所以在易用性上httpload不如siege,仍需进一步调研. 缺点:安装过程中,不能自动创建siege.log文 ...

  10. android学习Gallery和ImageSwitch的使用

    Gallery组件被称之为画廊,是一种横向浏览图片的列表,在使用android API 19 Platform 时会发现Gallery被画上了横线,表明谷歌已经不推荐使用该组件了, * @deprec ...