zoukankan
html css js c++ java
C# 截取webBrowser网页存为图片
代码
Code
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Data;
5
using
System.Windows.Forms;
6
using
System.Drawing;
7
8
namespace
EcsinoBrowser.Common
9
{
10
/**/
///
<summary>
11
///
WebSnap :网页抓图对象
12
///
</summary>
13
public
class
WebSnap
14
{
15
public
WebSnap()
16
{
17
//
18
//
TODO: 在此处添加构造函数逻辑
//
19
}
20
21
/**/
///
<summary>
22
///
开始一个抓图并返回图象
23
///
</summary>
24
///
<param name="Url">
要抓取的网页地址
</param>
25
///
<returns></returns>
26
public
Bitmap StartSnap(
string
Url)
27
{
28
WebBrowser myWB
=
this
.GetPage(Url);
29
Bitmap returnValue
=
this
.SnapWeb(myWB);
30
myWB.Dispose();
31
return
returnValue;
32
}
33
34
private
WebBrowser GetPage(
string
Url)
35
{
36
WebBrowser myWB
=
new
WebBrowser();
37
myWB.ScrollBarsEnabled
=
false
;
38
myWB.Navigate(Url);
39
while
(myWB.ReadyState
!=
WebBrowserReadyState.Complete)
40
{
41
System.Windows.Forms.Application.DoEvents();
42
}
43
return
myWB;
44
}
45
46
private
Bitmap SnapWeb(WebBrowser wb)
47
{
48
HtmlDocument hd
=
wb.Document;
49
int
height
=
Convert.ToInt32(hd.Body.GetAttribute(
"
scrollHeight
"
))
+
10
;
50
int
width
=
Convert.ToInt32(hd.Body.GetAttribute(
"
scrollWidth
"
))
+
10
;
51
wb.Height
=
height;
52
wb.Width
=
width;
53
Bitmap bmp
=
new
Bitmap(width, height);
54
Rectangle rec
=
new
Rectangle();
55
rec.Width
=
width;
56
rec.Height
=
height;
57
wb.DrawToBitmap(bmp, rec);
58
return
bmp;
59
}
60
}
61
}
62
代码使用
Code
1
Common.WebSnap websnap
=
new
EcsinoBrowser.Common.WebSnap();
2
Bitmap bmp
=
websnap.StartSnap(webBrowser1.Url.ToString());
3
bmp.Save(Application.StartupPath
+
@"
\crl.jpg
"
);
4
bmp.Dispose();
查看全文
相关阅读:
MyBatis 数据库字段排序问题(一)
MySQL 函数
Google 浏览器设置打开超链接到新窗口标签页
Linux 命令整理 vim
IDEA比较实用的插件之翻译插件(Translation)
Dubbo-本地Bean测试
Spring+dubbo错误(二)
Spring+dubbo错误(一)
Dubbo-本地测试直连
上架app被拒原因总结
原文地址:https://www.cnblogs.com/haver/p/1432475.html
最新文章
自定义圆的半径(主类)
自定义圆的半径attr.xml
自定义圆的半径layout
xlistview的(java)
xlistview的java(头)
xlistview的java(脚)
xlistview的XML(脚)xlistview_footer
XMl的解析
HorizontalScrollView的配置
夜间模式
热门文章
侧滑
数据库的简单语句
读取文件,截取字符串
viewpage图片轮播
文字加横线效果
Android的适配器
扫描指定文件下的图片或歌曲
httpurlconectiondopost的方法向服务器中获取json文件
一个Aactivity跳到另一个Bactivity的fragment中去
MyBatis Plus 自带BaseMapper解析(二)
Copyright © 2011-2022 走看看