[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 ...
随机推荐
- java调用wkhtmltopdf生成pdf文件,美观,省事
最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...
- Ant Design UI组件
Ant Design 是面向中台的 UI 设计语言. http://ant.design/
- css简单实现五角星评分、点赞收藏、展示评分(半颗星、1/3颗星)
1.前言 之前做的好几个项目中,都会遇到打分,评分,点赞这样的需求,写了很多次,每次需要再写的时候,就会翻出之前写过的代码,然后copy过来.总觉得这样的话没有进步,没有把知识放进脑袋里,所以,自己花 ...
- 阿里云直播SDK - .NET
阿里云sdk:https://develop.aliyun.com/sdk/csharp?spm=5176.doc27234.2.4.QiJb9l Github:https://github.com/ ...
- git入门大全
前言 以前写个一个git小结,但是实际上并不够用.于是结合实际工作上碰到的一些情况,参考了一些资料,重新总结了一下.目标是在日常工作中不用再去查阅其他的资料了,如果有什么遗漏或者错误的地方,请评论指出 ...
- nio系列(一)---nio重要组成
nio重要组成部分 前言:通过本文可以了解nio的重要组成部分,了解完基础的内容后后面理解才会简单一点.下一篇会讲讲nio的应用和io的对比.如果有不正确的地方还望指正. channel chanel ...
- 理解LGWR,Log File Sync Waits以及Commit的性能问题[转]
理解LGWR,Log File Sync Waits以及Commit的性能问题 一.概要: 1. Commit和log filesync的工作机制 2. 为什么log file wait太久 3. ...
- 这是一个简单的前台短信验证码功能 ajax实现异步处理 (发送和校验)
<script type="text/javascript"> var InterValObj; //timer变量,控制时间 var count = 60; //间隔 ...
- fs模块(一)
模块的使用,必须先引入模块 var fs = require('fs'); 1. readFile 和 readFileSync var fs = require('fs'); //01 readFi ...
- Python第一天---第一个Python程序
1.我的环境是windows下,需要安装notepad++,安装Python2,配置环境变量(百度下可以见) 2.打开cmd窗口-----输入I: [输入要在哪个磁盘存储python代码(我的在I: ...