[C#] 如何截取完整的网页图片
前言
有时候浏览到非常有用的网页时,我们会选择将它加入到收藏夹中,但是网站一旦过期,以后就看不到这个网页了。当然也可以将网页打印成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#] 如何截取完整的网页图片的更多相关文章
- 截取完整网页图片的方法 Chrome 浏览器
截取完整网页图片的方法:https://sspai.com/post/42193 要想使用截图功能,你需要首先确保 Chrome 已升级至 59 或更高版本.在想要截图的网页中,首先按下 ⌘Comma ...
- 托管到github上的网页图片在百度浏览器中显示不全
这几天做了个较完整的网页放到github上,上传后看网页效果. 在Firefox浏览器中,显示正常. 在百度浏览器中,空了一大块位置(图片位置),偏偏只空了这一块,其它地方的图片都好好的. 点击f12 ...
- node:爬虫爬取网页图片
代码地址如下:http://www.demodashi.com/demo/13845.html 前言 周末自己在家闲着没事,刷着微信,玩着手机,发现自己的微信头像该换了,就去网上找了一下头像,看着图片 ...
- JAVA多线程超时加载当网页图片
先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: “http://xxxx/xxxx/xxx.jpg” 'http: ...
- java 抓取网页图片
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Out ...
- Python爬虫 网页图片
一 概述 参考http://www.cnblogs.com/abelsu/p/4540711.html 弄了个Python捉取单一网页的图片,但是Python已经升到3+版本了.参考的已经失效,基本用 ...
- android通过Canvas和Paint截取无锯齿圆形图片
一个通过Canvas和Paint截取无锯齿圆形图片. /** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ public st ...
- 14种网页图片和文字特效的jQuery插件代码
1.网页图片3d旋转jQuery代码 演示和下载地址 2.存css3实现的tabl选项卡代码 演示和下载地址 3.jQuery标签旋转代码 演示和下载地址 4.鼠标悬浮的图片选项卡代码 演示和下载地址 ...
- Webdriver中实现区域截图的方式以及如何截取frame中的图片
import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOE ...
随机推荐
- 在Swift中实现 oc与swift的混编
在Swift中想要引用OC头文件(import),可采用混编的方法,这里以sqlite为例,采用OC-Swift桥的方式实现添加头文件1引入sqlite数据库的库文件 打开工程配置文件,在build ...
- 【JAVASCRIPT】ECMAScrip (转)
部分ECMAScript术语 ECMAScript Sun(现在的Oracle)公司持有着"Java"和"JavaScript"的商标.这就让微软不得不把自己的 ...
- [Tyvj 1952] Easy
P1952 Easy 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化一下 ...
- Easy DataGrid 实现动态列、行
Easy DataGrid 实现动态列.行 前端代码: <title>展示销售的实时数据</title> <script type="text/javascri ...
- YARN笔记——技术点汇总
目录 · 概况 · 原理 · 资源调度器分类 · YARN架构 · ResourceManager · NodeManager · ApplicationMaster · Container · YA ...
- 微信小程序开发-新闻列表之新闻列表绑定
微信小程序开发-新闻列表之新闻列表绑定开发教程: 1.效果图预览 2.准备工作 在拿到效果图后不要先急着去写代码,而是要去分析一下页面的整体结构,用什么方式定位和布局.小程序里建议使用flex布局,因 ...
- CocoaPods的一些略为高级一丁点的使用【转】
记得我刚开始做iOS开发的时候,是没有项目依赖管理工具.当需要引入第三方库的时候是相当麻烦的,不是直接拷贝库近来,就是添加依赖工程,直到CocoaPods出来才改变这个状况.项目依赖管理不是Cocoa ...
- TensorFlow学习笔记2——数据类型及简单运算
0. 小试牛刀 首先,激活tensorflow环境( source activate tensorflow ),随后在ipython里: import tensorflow as tf sess = ...
- Python模块----linecache
Python标准库提供了一个有趣的模块:linecache模块.该模块用来从文件中读取任何的行,并且将这些lines使用缓存进行优化,常见的情况是从个大文件中读取指定的行.不过由于此模块使用内存进行缓 ...
- python===基础,修改idle的输入风格
http://blog.csdn.net/aq_cainiao_aq/article/details/51701861