C#編寫ActiveX網(wǎng)頁截圖控件
故事背景:Java組的小伙伴需要一個(gè)能在IE(還是6。。。)下截圖并返回給網(wǎng)頁的功能,但是IE做起來很麻煩(可能根本做不到),于是找到我寫一個(gè)ActiveX控件實(shí)現(xiàn)此功能,想著可能還有其他小伙伴需要這個(gè)功能,于是就PO出來,供需要的人使用,當(dāng)然也可以作為學(xué)習(xí)C#編寫ActiveX的一個(gè)簡單入門教程(VC++效果更好)。
功能截圖如下:

代碼分為兩個(gè)核心部分:1、C#屏幕截圖;2、C#開發(fā)ActivX控件。
1、屏幕截圖,這個(gè)在網(wǎng)上找到了一個(gè)只需要5行代碼的實(shí)現(xiàn)(超級(jí)精簡),當(dāng)然你也可以費(fèi)點(diǎn)功夫?qū)崿F(xiàn)自由區(qū)域截取圖片,截圖后保存到本地,然后使用二進(jìn)制讀取jpg文件,并編碼為base64返回給網(wǎng)頁
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#開發(fā)ActiveX控件,網(wǎng)上示例比較多
先新建一個(gè)類庫,并設(shè)置項(xiàng)目屬性,COM可見

并為COM互注冊,這樣編譯完了會(huì)自動(dòng)注冊COM控件

完成設(shè)置后,編寫代碼,如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace PrintScreenLib
{
//為了讓ActiveX控件獲得客戶端的信任,控件類需要實(shí)現(xiàn)一個(gè)名為“IObjectSafety”的接口。先創(chuàng)建該接口(注意,不能修改該接口的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接口,接口實(shí)現(xiàn)是固定內(nèi)容
[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調(diào)用ActiveX控件:
<!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>
一個(gè)完整的ActvieX控件就完成了,這里沒有使用到事件,如果使用事件的話會(huì)更加麻煩一點(diǎn)。
下載:屏幕截圖ActivX控件
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C# 實(shí)現(xiàn)SDL2進(jìn)行視頻播放窗口截圖和字幕添加
- C#實(shí)現(xiàn)QQ截圖功能及相關(guān)問題
- C#實(shí)現(xiàn)的滾動(dòng)網(wǎng)頁截圖功能示例
- C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼
- C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼
- C#實(shí)現(xiàn)屬于自己的QQ截圖工具
- C#實(shí)現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法
- C#實(shí)現(xiàn)通過ffmpeg從flv視頻文件中截圖的方法
- C#實(shí)現(xiàn)網(wǎng)頁截圖功能
- c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
- 解決C#全屏幕截圖的實(shí)現(xiàn)方法
- C# 使用BitBlt進(jìn)行窗口抓圖的示例
相關(guān)文章
C#中的Task.WaitAll和Task.WaitAny方法介紹
這篇文章介紹了C#中的Task.WaitAll和Task.WaitAny方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了C#潤滑利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-07-07
c#實(shí)現(xiàn)獲取字符串陣列中元素最長或最短的長度
下面小編就為大家分享一篇c#實(shí)現(xiàn)獲取字符串陣列中元素最長或最短的長度方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12
C# 實(shí)現(xiàn)Table的Merge,Copy和Clone
這篇文章主要介紹了C# 實(shí)現(xiàn)Table的Merge,Copy和Clone,幫助大家更好的利用c#處理文件,感興趣的朋友可以了解下2020-12-12
c#使用htmlagilitypack解析html格式字符串
這篇文章主要介紹了c#使用htmlagilitypack解析html格式字符串的示例,需要的朋友可以參考下2014-03-03
C#實(shí)現(xiàn)身份證驗(yàn)證功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)身份證驗(yàn)證功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
初步認(rèn)識(shí)C#中的Lambda表達(dá)式和匿名方法
這篇文章主要介紹了初步認(rèn)識(shí)C#中的Lambda表達(dá)式和匿名方法,本文著重講解Lambda表達(dá)式和匿名方法的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-01-01
在WPF中合并兩個(gè)ObservableCollection集合
這篇文章介紹了在WPF中合并兩個(gè)ObservableCollection集合的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

