[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 ...
随机推荐
- IntelliJ idea学习资源
工作需要, 最近得从Eclipse转战到Idea, 找了些不错的学习资料: 1, 从eclipse上迁移过来的用户说明: https://www.jetbrains.com/help/idea/201 ...
- Java基础之String类
String类 字符串是不可变的,对其做的任何改变,会生成一个对象,不会改变有原有对象. ==和equals() String s1 = "good"; String s2 = & ...
- 6. leetcode 136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- 压缩[SCOI2007]
题目描述 给一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复信息.压缩后的字符串除了小写字母外还可以(但不必)包含大写字母R与M,其中M标记重复串的开始,R重复从上一个M(如果当前 ...
- C++STL之String
本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...
- Java String字符串深入详解
Java中字符串对象创建有两种形式,一种为字面量形式,如String str = "hello";,另一种就是使用new这种标准的构造对象的方法,如String str = new ...
- Cognos配置管理
--Cognos配置管理 --------------------------2014/03/19 进入配置管理界面: /washome/cognos/c10/bin64 ./cogconfig.sh ...
- python基础笔记(一)
python解释器执行python程序的过程:python3 C:\test.py 1.启动python解释器(内存中) 2.将C:\test.py 内容从硬盘读入内存(这一步与文本编辑器是一样的) ...
- java代码_按要求输出相应沙漏
package Callatz;/* 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印******* ***** *** * ...
- Android 5.1.1在外置SD卡中创建文件夹
Android 4.4之后WRITE_MEDIA_STORAGE 权限仅提供给系统应用,不再授予第三方App,WRITE_EXTERNAL_STORAGE 权限,仅仅用于授权用户写 primary e ...