一、目标
创建一个Demo,使用C#创建ActiveX达到屏幕截图并展示图片的效果。
二、步骤
1. 使用Visual Studio创建一个Class Libary项目
2. 向Project中Add一个New Item -> Interface,命名为IObjectSafety.cs,代码如下
namespace IEActiveX { //为了让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); } }
3. 向Project中Add一个User Control...,命名为ScreenCapture.cs,代码如下
注意使用Guid工具生成下面的Guid
namespace IEActiveX { [Guid("84F44957-3A18-4B1B-96BB-9D7D1D7B577E")] //使用VS工具里的GUID生成器生成唯一码 public partial class ScreenCapture : UserControl,IObjectSafety { public ScreenCapture() { InitializeComponent(); } #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, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions) { int result = E_FAIL; string strGuid = riid.ToString("B"); pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; switch (strGuid) { case _IID_IDispatch: case _IID_IDispatchEx: result = S_OK; pdwEnabledOptions = 0; if (_fSafeForScripting) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: result = S_OK; pdwEnabledOptions = 0; if (_fSafeForInitializing) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; break; default: result = E_NOINTERFACE; break; } return result; } public int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions) { int result = E_FAIL; string strGuid = riid.ToString("B"); switch (strGuid) { case _IID_IDispatch: case _IID_IDispatchEx: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && _fSafeForScripting) result = S_OK; break; default: result = E_NOINTERFACE; break; } return result; } #endregion 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; } } }
4. 设置Project属性,
Application => "Make assembly COM-Visible"(使程序集COM可见)
Build =>"Register for COM interop"(为COM互操作注册)
5. 生成该解决方案,该ActiveX会注册到系统Register
6. 在Web中调用该ActiveX
注意下面的CLASSID="clsid:84F44957-3A18-4B1B-96BB-9D7D1D7B577E"> 必须和上面自己生成的一致
<!DOCTYPE html> <html> <head> <title>Testing</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("IEActiveX"); 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="IEActiveX" WIDTH="120" HEIGHT=20" CLASSID="clsid:84F44957-3A18-4B1B-96BB-9D7D1D7B577E"></OBJECT> <input type="button" value="ScreenCapture" onclick="jt();"> <Image id ="img" /> </body> </html>
三、运行结果
时间: 2024-10-06 00:22:40