故事背景:java组的小伙伴需要一个能在ie(还是6。。。)下截图并返回给网页的功能,但是ie做起来很麻烦(可能根本做不到),于是找到我写一个activex控件实现此功能,想着可能还有其他小伙伴需要这个功能,于是就po出来,供需要的人使用,当然也可以作为学习c#编写activex的一个简单入门教程(vc++效果更好)。
功能截图如下:
代码分为两个核心部分:1、c#屏幕截图;2、c#开发activx控件。
1、屏幕截图,这个在网上找到了一个只需要5行代码的实现(超级精简),当然你也可以费点功夫实现自由区域截取图片,截图后保存到本地,然后使用二进制读取jpg文件,并编码为base64返回给网页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public string printscreen() { image baseimage = new bitmap(screen.primaryscreen.bounds.width, screen.primaryscreen.bounds.height); graphics g = graphics.fromimage(baseimage); g.copyfromscreen( new point(0, 0), new point(0, 0), screen.allscreens[0].bounds.size); g.dispose(); baseimage.save( "d:\\screen.jpg" , imageformat.jpeg); stream file = new filestream( "d:\\screen.jpg" , filemode.open); binaryreader bw = new binaryreader(file); var buffer = new byte [file.length]; bw.read(buffer, 0, buffer.length); bw.close(); string b64 = convert.tobase64string(buffer); return b64; } |
2、c#开发activex控件,网上示例比较多
先新建一个类库,并设置项目属性,com可见
并为com互注册,这样编译完了会自动注册com控件
完成设置后,编写代码,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using system; using system.collections.generic; using system.text; using system.runtime.interopservices; namespace printscreenlib { //为了让activex控件获得客户端的信任,控件类需要实现一个名为“iobjectsafety”的接口。先创建该接口(注意,不能修改该接口的guid值) [comimport, guid( "cb5bdc81-93c1-11cf-8f20-00805f2cd064" )] [interfacetype(cominterfacetype.interfaceisiunknown)] public interface iobjectsafety { [preservesig] int getinterfacesafetyoptions( ref guid riid, [marshalas(unmanagedtype.u4)] ref int pdwsupportedoptions, [marshalas(unmanagedtype.u4)] ref int pdwenabledoptions); [preservesig()] int setinterfacesafetyoptions( ref guid riid, [marshalas(unmanagedtype.u4)] int dwoptionsetmask, [marshalas(unmanagedtype.u4)] int dwenabledoptions); } } |
新建用户控件,并派生iobjectsafe接口,接口实现是固定内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
[guid( "61d7f413-a1b2-48a9-b851-5bfbcf50280c" )] //使用vs工具里的guid生成器生成唯一码 public partial class pslib : usercontrol, iobjectsafety { #region iobjectsafety 成员 private const string _iid_idispatch = "{00020400-0000-0000-c000-000000000046}" ; private const string _iid_idispatchex = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}" ; private const string _iid_ipersiststorage = "{0000010a-0000-0000-c000-000000000046}" ; private const string _iid_ipersiststream = "{00000109-0000-0000-c000-000000000046}" ; private const string _iid_ipersistpropertybag = "{37d84f60-42cb-11ce-8135-00aa004bb851}" ; private const int interfacesafe_for_untrusted_caller = 0x00000001; private const int interfacesafe_for_untrusted_data = 0x00000002; private const int s_ok = 0; private const int e_fail = unchecked (( int )0x80004005); private const int e_nointerface = unchecked (( int )0x80004002); private bool _fsafeforscripting = true ; private bool _fsafeforinitializing = true ; public int getinterfacesafetyoptions( ref guid riid, ref int pdwsupportedoptions, ref int pdwenabledoptions) { int rslt = e_fail; string strguid = riid.tostring( "b" ); pdwsupportedoptions = interfacesafe_for_untrusted_caller | interfacesafe_for_untrusted_data; switch (strguid) { case _iid_idispatch: case _iid_idispatchex: rslt = s_ok; pdwenabledoptions = 0; if (_fsafeforscripting == true ) pdwenabledoptions = interfacesafe_for_untrusted_caller; break ; case _iid_ipersiststorage: case _iid_ipersiststream: case _iid_ipersistpropertybag: rslt = s_ok; pdwenabledoptions = 0; if (_fsafeforinitializing == true ) pdwenabledoptions = interfacesafe_for_untrusted_data; break ; default : rslt = e_nointerface; break ; } return rslt; } public int setinterfacesafetyoptions( ref guid riid, int dwoptionsetmask, int dwenabledoptions) { int rslt = e_fail; string strguid = riid.tostring( "b" ); switch (strguid) { case _iid_idispatch: case _iid_idispatchex: if (((dwenabledoptions & dwoptionsetmask) == interfacesafe_for_untrusted_caller) && (_fsafeforscripting == true )) rslt = s_ok; break ; case _iid_ipersiststorage: case _iid_ipersiststream: case _iid_ipersistpropertybag: if (((dwenabledoptions & dwoptionsetmask) == interfacesafe_for_untrusted_data) && (_fsafeforinitializing == true )) rslt = s_ok; break ; default : rslt = e_nointerface; break ; } return rslt; } #endregion |
ie调用activex控件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<!doctype html> < html > < head > < title >测试</ title > < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" > < meta http-equiv = "description" content = "this is my page" > < meta http-equiv = "content-type" content = "text/html; charset=utf-8" > < script > function jt(){ var str=""; try { var obj = document.getelementbyid("myactivex"); str=obj.printscreen(); } catch(e) { alert(e); return; } var img=document.getelementbyid("img"); img.src="data:image/jpeg;base64,"+str;//接受base64编码后的图片 } </ script > </ head > < body > < object id = "myactivex" width = "120" height = 20 " classid = "clsid:61d7f413-a1b2-48a9-b851-5bfbcf50280c" ></ object > < input type = "button" value = "截图" onclick = "jt();" > < image id = "img" /> </ body > </ html > |
一个完整的actviex控件就完成了,这里没有使用到事件,如果使用事件的话会更加麻烦一点。
下载:屏幕截图activx控件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。