[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 ...
随机推荐
- 【CSS】伪类与伪元素
一伪类 语法: selector : pseudo-class {property: value} 提示:伪类名称对大小写不敏感. 与 CSS类搭配使用 selector.class : pse ...
- 用java写一个用户登陆界面
一.课堂测试源代码及其结果截图 用java的swing写一个用户登录界面,采用网格布局.源代码如下: /** * */package LiuLijia; import java.awt.CardLay ...
- Redis作为消息队列服务场景应用案例(入队和出队)
http://www.cnblogs.com/leo_wl/p/3831349.html
- RabbitMQ入门-高效的Work模式
扛不住的Hello World模式 上篇<RabbitMQ入门-从HelloWorld开始>介绍了RabbitMQ中最基本的Hello World模型.正如其名,Hello World模型 ...
- 如何快速查看github代码库中第一次commit的记录
发现一个别人推荐的代码库用来学习源码, star星还不少,别人推荐从第一次commit开始阅读,于是试着去找commits的第一次 问题来了,这个代码库commits7855次,点击进入commits ...
- 12. leetcode 455.Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- css3鼠标悬停图片抖动效果
提供一个参考的链接 http://demo.lanrenzhijia.com/2015/pic0113/
- jQuery 去空
//去左右空格; function trim(s){ return s.replace(/(^\s*)|(\s*$)/g, ""); } //去掉字符串中所有空格(包括中间 ...
- TP3.2 配置最新的阿里大于sdk
TP3.2 配置最新的阿里大于sdk 最近公司买了阿里云的阿里大于短信验证 ,这里记录下本人接入短信验证的过程和心得. 大家是不是一开始都是和本人一样直接去百度下怎么有没有现成的demo 或者是封装好 ...
- 求m区间内的最小值
洛谷P1440 求m区间内的最小值 ............................................................................... 以上 ...