C # screen recording control code

Source: Internet
Author: User

Download the video control Demo:
Http://hocor.cn/ SC .rar

The following is the main code,

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using wmencoderlib;
Using system. runtime. interopservices;
Using Microsoft. Win32;
Using system. Threading;
Using system. IO;
Using system. Drawing. drawing2d;

Namespace F. screencamera
{
Public partial class mainform: Form
{

///// // Related System Tray

//// // Win32

/// // Member

//////////////////////////////////////// //////////////////////////////////////// ///////////////
///// Program Initialization
/// Private void appinit ()
{
ENC = new wmencoderclass ();
Loadseting ();
}
///// Set the skin
///// Private void setskin (string skinname)
{
This. Skin. serialnumber = "";
This. Skin. skinfile = "skin \" + skinname + ". SSK ";
}
///// List of initial compression options
/// Private void initcompression ()
{
//
Configmanager. receivetings meeting = new configmanager. receivetings ();
String compression = paieting. readkeyvalue ("compression ");
//
ENC = new wmencoderclass ();
Wspim = enc. sourceplugininfomanager;
Wspim. Refresh ();
Iwmencprofilecollection wpfc = enc. profilecollection;
Iwmencprofile WP;
This. compressionoptionlistbox. Items. Clear ();
For (INT I = 0; I <wpfc. Count; I ++)
{
WP = wpfc. item (I );
This. compressionoptionlistbox. Items. Add (WP. Name );
If (WP. Name = compression)
This. compressionoptionlistbox. selectedindex = I;
}

}
///// Load settings
/// Private void loadseting ()
{
Configmanager. receivetings meeting = new configmanager. receivetings ();
// Whether to record the sound
This. chksound. Checked = convert. toboolean (meeting. readkeyvalue ("chksound "));
// Whether to hide the main form
This. chkhidemainform. Checked = convert. toboolean (meeting. readkeyvalue ("chkhidemainform "));
// Shortcut key settings
This. skstartandpause. Text = paieting. readkeyvalue ("skey_startandpause ");
This. skstop. Text = paieting. readkeyvalue ("skey_stop ");
This. skshowandhide. Text = paieting. readkeyvalue ("skey_showandhide ");
// Recording area
Int rect = convert. toint32 (meeting. readkeyvalue ("camerarect "));
If (rect = 1)
This. rwindow. Checked = true;
Else
This. rscreen. Checked = true;
//
Size SZ = mainform. getscreensize ();
This. RW. Text = Sz. Width. tostring ();
This. RH. Text = Sz. Height. tostring ();
}
///// Obtain the selected compression Option
///// Private iwmencprofile2 getselectcompressionoption ()
{
Iwmencprofilecollection wpfc = enc. profilecollection;
Iwmencprofile WP;
Iwmencprofile2 WP2 = new wmencprofile2class ();
If (this. compressionoptionlistbox. selectedindex =-1)
{
Return NULL;
}
For (INT I = 0; I <wpfc. Count; I ++)
{
WP = wpfc. item (I );

If (this. compressionoptionlistbox. selecteditem. tostring () = WP. Name)
{
Wp2.loadfromiwmprofile (WP );
Return WP2;
}
}
Return NULL;
}
//// Start recording
/// Private void startcamera ()
{
Iwmencsourcegroupcollection srckgcoll;
Iwmencsourcegroup2 srmrm;
Iwmencaudiosource srcaud;
Iwmencvideosource2 srcvid;
Iwmencprofile2 pro;
ENC = new wmencoderclass ();
//-------------------------------------------
Try
{
Srckgcoll = enc. sourcegroupcollection;
Srmrm = (iwmencsourcegroup2) srcgr. Add ("sg_1 ");
Srcvid = (iwmencvideosource2) srmrm. addsource (wmenc_source_type.wmenc_video );

// Whether to record the sound
If (this. chksound. Checked)
{
Srcaud = (iwmencaudiosource) srmrm. addsource (wmenc_source_type.wmenc_audio );
Srcaud. setinput ("default_audio_device", "device ","");
}
Srcvid. setinput ("screencapture1", "screencap ","");

Recording in the specified screen area

// Determine the compression mode
Pro = getselectcompressionoption ();
If (PRO = NULL)
{
MessageBox. Show ("error! Select a video compression template! ");
Return;
}
Srmrm. set_profile (Pro );

// Check whether the output file name is empty
If (this. outfilepathtextbox. Text. Length <1)
{
MessageBox. Show ("Please specify the Save path! ");
Return;
}

Output file name

ENC. Start ();
}
Catch (exception E1)
{
MessageBox. Show (e1.message );
}

}
//////////////////////////////////////// //////////////////////////////////////// ///////////////
/// // Structure
/// Public mainform ()
{
Initializecomponent ();
// Control. checkforillegalcrossthreadcils = false;
// Load skin
Configmanager. receivetings meeting = new configmanager. receivetings ();
String Skin = paieting. readkeyvalue ("skin ");
This. setskin (skin );
//
// Initial Tray
Icationicationicon ();
//
// Fixed form size
This. minimumsize = This. size;
This. maximumsize = This. size;
}
///// Main form loading event
//////// Private void mainform_load (Object sender, eventargs E)
{
Appinit ();
Initcompression ();
}
///// Select the storage path of the output file
//////// Private void selectoutpathbutton_click (Object sender, eventargs E)
{
Savefiledialog SFD = new savefiledialog ();
SFD. Filter = "WMV file (*. wmv) | *. wmv ";
SFD. restoredirectory = true;
If (SFD. showdialog () = dialogresult. OK)
{
This. outfilepathtextbox. Text = SFD. filename;
If (file. exists (SFD. filename ))
File. Delete (SFD. filename );
}
}
//// Start recording
//////// Private void startbutton_click (Object sender, eventargs E)
{
If (this. compressionoptionlistbox. selectedindex>-1 & this. outfilepathtextbox. Text. Trim ()! = "")
{

Th = new thread (New threadstart (start ));
Th. isbackground = true;
Th. Start ();
// Disable the start button
This. startbutton. Enabled = false;
// The pause button is available.
This. pausebutton. Enabled = true;
//
// Hide the form
If (this. chkhidemainform. Checked)
{
This. windowstate = formwindowstate. minimized;
This. Visible = false;
}
}
Else
{
If (this. outfilepathtextbox. Text. Trim () = "")
MessageBox. Show ("select the location where the video file is saved ");
If (this. compressionoptionlistbox. selectedindex =-1)
MessageBox. Show ("select a compression solution ");
}
}
Private void start ()
{
Voiddelegate dstart = new voiddelegate (startcamera );
This. Invoke (dstart );
}
///// Stop the recording button
//////// Private void stopbutton_click (Object sender, eventargs E)
{
Th. Abort ();
ENC. Stop ();
This. startbutton. Enabled = true;
This. pausebutton. Enabled = false;

}
///// Pause the recording button
//////// Private void pausebutton_click (Object sender, eventargs E)
{
If (this. pausebutton. Text! = "Continue recording ")
{
ENC. Pause ();
This. pausebutton. Text = "Continue recording ";
}
Else
{
ENC. Start ();
This. pausebutton. Text = "Pause recording ";
}
}
///// Click the close button of the main form
/////// Private void mainform_formclosing (Object sender, formclosingeventargs E)
{
If (E. closereason! = Closereason. applicationexitcall & E. closereason! = Closereason. windowsshutdown)
{
E. Cancel = true;
This. windowstate = formwindowstate. minimized;
This. Visible = false;
}
Else
{
// None
}
}
///// Compression option change
/////// Private void compressionoptionlistbox_selectedindexchanged (Object sender, eventargs E)
{
ListBox lB = (ListBox) sender;
Configmanager. receivetings meeting = new configmanager. receivetings ();
Meeting. updatekey ("compression", LB. items [lb. selectedindex]. tostring ());
}
///// Compression option change
//////// Private void compressionoptionlistbox_mouseclick (Object sender, mouseeventargs E)
{
ListBox lB = (ListBox) sender;
Configmanager. receivetings meeting = new configmanager. receivetings ();
Meeting. updatekey ("compression", LB. items [lb. selectedindex]. tostring ());
}

Shortcut Key

Recording Area
///// Specifies whether to record the sound.
//////// Private void chksound_checkedchanged (Object sender, eventargs E)
{
Configmanager. receivetings meeting = new configmanager. receivetings ();
Meeting. updatekey ("chksound", this. chksound. Checked. tostring ());
}
// Whether to hide the main form
Private void chkhidemainform_checkedchanged (Object sender, eventargs E)
{
Configmanager. receivetings meeting = new configmanager. receivetings ();
Meeting. updatekey ("chkhidemainform", this. chkhidemainform. Checked. tostring ());
}

//////////////////////////////////////// //////////////////////////////////////
///// Obtain the screen size
///// Public static size getscreensize ()
{
// Obtain the resolution of the current screen
Screen scr = screen. primaryscreen;
Rectangle rc = scr. bounds;
Size size = new size (RC. Width, RC. Height );
Return size;
}

 

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.