截取网页快照的dll
飘飘白云 (http://www.cppblog.com/kesalin)
这个dll的功能是将在后台(也可指定参数是否显示)用安静模式(也就是不执行JavaScript,ActiveX以及Java程序)打开指定网址,并将网页截屏保存成png格式的图片。
dll下载:点击这里
dll src以及测试程序下载:点击这里
如下测试程序所示:
将得到我的cppblog首页的快照图:
下面来说说如何使用这个dll,我写了一个装载dll的帮助文件ppbyModLoader.h,只需要include这个头文件就很可以很方便地使用了。使用步骤如下:
Step 1: 包含相关头文件,载入dll文件
1
2
#include "ppbySnapShot.h"
3
#include "ppbyModLoader.h"
4
5
const UINT WM_SNAPSHOT_CALLBACK = RegisterWindowMessageW(L"WM_SNAPSHOT_CALLBACK");
6
7
#ifdef _DEBUG
8
PPBYMOD_LOADER_DEBUG(ppbySnapShot)
9
#else
10
PPBYMOD_LOADER(ppbySnapShot)
11
#endif
12
Step 2: 创建snapshot对象,并初始化
ppbySnapShot::UActivator * snapShot;
1
void loadSnapShotLib()
2
{
3
HRESULT result = ppbySnapShotLoader::Activate(L"activator", (void**) &snapShot);
4
if (SUCCEEDED(result))
{
5
result = snapShot->Initialize();
6
if (FAILED(result))
{
7
snapShot->Uninitialize();
8
snapShot = 0;
9
}
10
}
11
}
Step 3: 调用snapShot的AddWebItem函数,截取网页快照。参数WebItem的详细说明如下:
Uri -- 以http://或https://打头的网址
Path -- 保存快照图片的绝对路径
ImageWidth -- 保存快照图片的宽度
ImageHeight -- 保存快照图片的高度
Target -- 消息回调窗口(默认为NULL)
Message -- 回调消息(默认为)
Cookie -- 此次截屏的辨识符号(默认为0)
ShotDelay -- 截屏延迟时间
NavigateTimeout -- 网页打开超时时间
Show -- 截屏的时候是否显示网页(调试用)
PageWidth -- 如果Show 为true,前台显示网页的宽度
PageHeight -- 如果Show 为true,前台显示网页的高度
1
void capture(const LPCWSTR url, const LPCWSTR savePath)
2
{
3
if (snapShot)
{
4
ppbySnapShot::WebItem item;
5
item.Uri = url;
6
item.Path = savePath;
7
item.Show = true;
8
9
item.ImageWidth = 640;
10
item.ImageHeight = 480;
11
12
item.Target = *this;
13
item.Message = WM_SNAPSHOT_CALLBACK;
14
item.Cookie = ++count;
15
16
item.PageWidth = 1024;
17
item.PageHeight = 768;
18
19
item.ShotDelay = 2 * 1000;
20
item.NavigateTimeout = 20 * 1000;
21
22
snapShot->AddWebItem(&item);
23
}
24
}
Step 4:释放snapshot对象
1
void unloadSnapShotLib()
2
{
3
if (snapShot != 0)
{
4
snapShot->Uninitialize();
5
snapShot = 0;
6
}
7
}
装载dll的帮助文件ppbyModLoader.h源码如下:
1
// ppbyModLoader.h
2
// 2008/11/26
3
#pragma once
4
5
/**//* */
6
namespace ppbyLoader
{
7
#define PPBYMOD_LOADER(N) \
8
struct N##Loader
{ \
9
typedef ppbyLoader::DllLoader<N##Loader> L; \
10
static const char* DLNM() \
11
{ \
12
return #N; \
13
} \
14
\
15
static const char* NAME() \
16
{ \
17
return #N; \
18
} \
19
\
20
static HRESULT Activate(LPCWSTR t, void ** o) \
21
{ \
22
static L s; \
23
return s.Activate(t, o); \
24
} \
25
};
26
27
#define PPBYMOD_LOADER_DEBUG(N) \
28
struct N##Loader
{ \
29
typedef ppbyLoader::DllLoader<N##Loader> L; \
30
static const char* DLNM() \
31
{ \
32
return #N "-Debug"; \
33
} \
34
\
35
static const char* NAME() \
36
{ \
37
return #N; \
38
} \
39
\
40
static HRESULT Activate(LPCWSTR t, void ** o) \
41
{ \
42
static L s; \
43
return s.Activate(t, o); \
44
} \
45
};
46
47
// DllLoader
48
template<class T>
49
class DllLoader
{
50
HMODULE m_hDLL;
51
52
typedef HRESULT (STDCALL *Activate_t) (LPCWSTR token, void ** obj);
53
54
Activate_t m_func;
55
public:
56
DllLoader() :
57
m_hDLL(0),
58
m_func(0)
59
{
60
}
61
62
~DllLoader()
63
{
64
if (m_hDLL != 0)
{
65
FreeLibrary(m_hDLL);
66
}
67
}
68
69
HRESULT Activate(LPCWSTR token, void ** obj)
70
{
71
if (m_hDLL == 0)
{
72
WCHAR name[MAX_PATH];
73
wsprintfW(name, L"%S.dll", T::DLNM());
74
75
m_hDLL = LoadLibraryW(name);
76
if (m_hDLL == 0)
{
77
DWORD error = GetLastError();
78
79
WCHAR buf[MAX_PATH];
80
wsprintfW(buf, L"ERROR: Missing '%S.dll'\n", T::DLNM());
81
OutputDebugStringW(buf);
82
83
return HRESULT_FROM_WIN32(error);
84
}
85
}
86
87
if (m_func == 0)
{
88
CHAR name[MAX_PATH];
89
wsprintfA(name, "_%s_Activate@8", T::NAME());
90
91
m_func = (Activate_t) GetProcAddress(m_hDLL, name);
92
if (m_func == 0)
{
93
DWORD error = GetLastError();
94
return HRESULT_FROM_WIN32(error);
95
}
96
}
97
98
HRESULT hRslt = m_func(token, obj);
99
if (FAILED(hRslt))
{
100
return hRslt;
101
}
102
103
return S_OK;
104
}
105
};
106
107
// DllLoader
108
} // namespace ppbyLoader
109
/**//* */
110