服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#编写ActiveX网页截图控件

C#编写ActiveX网页截图控件

2021-11-25 14:26大萝卜控 C#

这篇文章主要介绍了C#编写ActiveX网页截图控件,作为学习C#编写ActiveX的一个简单入门教程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

故事背景:java组的小伙伴需要一个能在ie(还是6。。。)下截图并返回给网页的功能,但是ie做起来很麻烦(可能根本做不到),于是找到我写一个activex控件实现此功能,想着可能还有其他小伙伴需要这个功能,于是就po出来,供需要的人使用,当然也可以作为学习c#编写activex的一个简单入门教程(vc++效果更好)。
功能截图如下:

C#编写ActiveX网页截图控件

代码分为两个核心部分: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可见 

C#编写ActiveX网页截图控件

并为com互注册,这样编译完了会自动注册com控件

C#编写ActiveX网页截图控件

完成设置后,编写代码,如下: 

?
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控件

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28