程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#編寫ActiveX網頁截圖控件

C#編寫ActiveX網頁截圖控件

編輯:C#入門知識

C#編寫ActiveX網頁截圖控件。本站提示廣大學習愛好者:(C#編寫ActiveX網頁截圖控件)文章只能為提供參考,不一定能成為您想要的結果。以下是C#編寫ActiveX網頁截圖控件正文


故事配景:Java組的小同伴須要一個能在IE(照樣6。。。)下截圖並前往給網頁的功效,然則IE做起來很費事(能夠基本做不到),因而找到我寫一個ActiveX控件完成此功效,想著能夠還有其他小同伴須要這個功效,因而就PO出來,供須要的人應用,固然也能夠作為進修C#編寫ActiveX的一個簡略入門教程(VC++後果更好)。
功效截圖以下:

 

代碼分為兩個焦點部門:1、C#屏幕截圖;2、C#開辟ActivX控件。
 1、屏幕截圖,這個在網上找到了一個只須要5行代碼的完成(超等精簡),固然你也能夠費點工夫完成自在區域截取圖片,截圖後保留到當地,然後應用二進制讀取jpg文件,並編碼為base64前往給網頁       

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

完成設置後,編寫代碼,以下: 

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接口,接話柄現是固定內容 

 [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控件: 

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

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved