C # Scripting ActiveX Web Capture Control _c# Tutorial

Source: Internet
Author: User
Tags base64

Story background: The Java Group's small partners need a can in IE (or 6 ...). The screenshot below and return to the Web page function, but IE is very cumbersome to do (may not be able to do), so found me to write an ActiveX control to implement this function, think that there may be other small partners need this function, so the PO out for the needs of people to use, of course, can also be used as learning C # A simple introductory tutorial for writing ActiveX (VC + + effect is better).
Function screenshot as follows:

Code is divided into two core parts:1, C # screen capture, 2, C # development Activx control .
1, screen capture , this online found a need for 5 lines of code to achieve (super concise), of course, you can also pay a little effort to achieve free area to intercept the picture, the screenshot saved to the local, and then use binary read JPG file, and encoded for Base64 return to the Web page

 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 # Development ActiveX control, online sample more
Create a new class library and set the project properties, COM visible

and register with COM so that the COM control is automatically registered when it is compiled

When you have finished setting up, write the code as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.InteropServices;

Namespace Printscreenlib
{
 //In order for the ActiveX control to gain trust from the client, the control class needs to implement an interface named "IObjectSafety". This interface is created first (note that the GUID value of the interface cannot be modified)
 [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);
 }
 

Creates a new user control and derives the Iobjectsafe interface, which is fixed content

 [GUID (' 61d7f413-a1b2-48a9-b851-5bfbcf50280c ')]//Use the GUID Builder in the VS tool to generate a unique code public partial class Pslib:usercontrol, IOBJEC
 tsafety {#region IObjectSafety member 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 & DW
   Optionsetmask) = = Interfacesafe_for_untrusted_data) && (_fsafeforinitializing = = true)) Rslt = S_OK;
  Break
   Default:rslt = E_nointerface;
  Break
 return rslt; 

 } #endregion

IE calls ActiveX controls:

<! DOCTYPE html>
 
 

A complete ACTVIEX control is complete, there is no use of the event, if the use of the event will be more cumbersome.

Download: Screen capture Activx control

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.