ARTICLE

Enhanced Desktop Recorder in .NET using C# and Windows Forms

Posted by Sateesh Arveti Articles | Visual C# July 22, 2008
This application provides features that will allow Desktop recording, sharing and broadcasting etc easier.
Reader Level:
Download Files:
 

Nowadays, Desktop or Application sharing is common to coordinate work properly in IT as well as other fields also. Sometimes, it may require to record the Operations done by us on Desktop for future reference. I have not seen many applications which support Desktop recording, sharing and broadcasting of it. I think it's better to design an application that will do above operations little bit easier. So, I design this application in VS.NET 2005 using C# and windows forms. I will explain features provided by this application, one sample scenario where we can use this application followed by its design and coding.

Features present in this application:

  • It allows us to record the Desktop Operations.
  • It allows us to view the recordings with an inbuilt Media Player.
  • It allows us to view the List of recent recordings.
  • Inbuilt Functionality to add Audio to the Desktop recordings.
  • Inbuilt Functionality to broadcast Desktop recordings.
  • Inbuilt Functionality to see remote Desktop.
  • Inbuilt Functionality to play, pause and stop recordings.
  • Inbuilt Functionality to start, pause and stop recording.
  • Inbuilt Functionality to show recording Duration, number of users connected to broadcast (desktop sharing).
  • Easy to use UI.
  • Now, Desktop recording and sharing is just a click away from us.

Sample Scenario for using this application:

Normally, software Testers use to prepare lot of descriptive documents for a bug to explain steps to be followed in reproducing it. I believe an image is equal to 1000's word description. So, why can't a tester record the steps for reproducing the bug in a video, instead of complex documents? Around of 50 - 60% of our effort in fixing the bug will be spent in analyzing, reproducing, get clarifications from testers regarding bug. So, in order to compensate it, the best solution is to record the bug's details in a video with audio support for extra information. In this kind of situations, this application will be very handy to use. There are other cases also, where this application will be useful like analyzing complex application's functionalities etc.

Prerequisites:
 
In order to use this application, we require Media Player Encoder 9. It is an plugin for Windows media player. It can be downloaded from here:

Now, create a new Windows Application using C# in VS.NET 2005 and name it as DesktopRecorder. Add controls to Main Form (DesktopRecorder) as shown below:

Now, I will outline functionality of few main controls in the application:

ContextMenu (cxtRecordingOpts):

This Control having few menu items for following operations:

  • Start Recording --> To start recording the Desktop.
  • Pause Recording --> To pause recording the Desktop.
  • Stop Recording --> To stop & save recording the Desktop.
  • Enter Audio File Path -->  To enter Path of audio file to be used while recording as an input to it.
  • Save Current Recording --> To save current recording.
  • Broadcast --> To enable broadcast, set Port Number for broadcasting.
  • Recent Recordings --> To see list of recent recordings.

menuStrip1 (Menu Strip):

This control is having an Option Menu with following items in it:

  • Open Broadcast URL Recording --> To Set URL of broadcast for viewing in Media player.
  • Play, Pause and Stop menuitems are used in changing the state of the player.
  • Show Media Player UI --> To show/hide inbuilt media player UI.
  • Exit --> To Exit the application.

Apart from this, I added few other controls to improve UI & Functionality.

Now, add a reference to COM dll (WMEncoderLib).If not, you can access this dlls from bin folder of code attached here. This will allows us to access functionality provided by Windows Media Player Encoder.

The main functionality of this application is it will record all desktop operations in wmv format for future reference along with sharing capability at a time.

Now, I will explain coding part of this application.

On Click of Start Recording --> Here, we are creating an instance media player encoder and setting its properties like input audio, video and broadcast path etc.
Finally, Encoder will be started to start recording. I will explain functionality of this event, little bit deeper. Since, this is the core for this application:

IWMEncProfile SelProfile;

IWMEncSource AudioSrc;

try

{

    if (DesktopEncoder != null)

    {

        //Checks Whether Encoder in is paused state or not.If it paused, it //will just starts it and returns.

        if (DesktopEncoder.RunState ==  MENC_ENCODER_STATE.WMENC_ENCODER_PAUSED)

        {

            DesktopEncoder.Start();

            return;

        }

    }

    #region Default settings for Encoder.

    DesktopEncoderAppln = new WMEncoderApp();

    DesktopEncoder = DesktopEncoderAppln.Encoder;

    IWMEncSourceGroupCollection SrcGroupCollection =     DesktopEncoder.SourceGroupCollection;

    IWMEncSourceGroup SrcGroup = SrcGroupCollection.Add("SG_1");

    IWMEncVideoSource2 VideoSrc = (IWMEncVideoSource2)SrcGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);

    #endregion

    //Set Audio Source,if Add Audio Checkbox is checked.

    if (addAudio.Checked)

    {

        AudioSrc = SrcGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);

        if (txtAudioFile.Text.Trim() != "")

        {

            if (File.Exists(txtAudioFile.Text.Trim()))

            {

                //Set audio file path to be used while encoding or recording.

                AudioSrc.SetInput(txtAudioFile.Text.Trim(), "", "");

            }

            else

            {

                //Set to use Default audio device as input,if file does not exist.

                AudioSrc.SetInput("Default_Audio_Device", "Device", "");

            }

        }

        else

        {

            AudioSrc.SetInput("Default_Audio_Device", "Device", "");

        }

    }

    //Set Video Source:Desktop.

    VideoSrc.SetInput("ScreenCapture1", "ScreenCap", "");

    IWMEncProfileCollection ProfileCollection = DesktopEncoder.ProfileCollection;

    ProfileCollection = DesktopEncoder.ProfileCollection;

    int lLength = ProfileCollection.Count;

    //Set Profile.

    if (toolstripEnableBroadcast.Checked && txtPortNbr.Text.Trim() != "")

    {

        IWMEncBroadcast broadcast = DesktopEncoder.Broadcast;                                       

broadcast.set_PortNumber(WMENC_BROADCAST_PROTOCOL.WMENC_PROTOCOL_HTTP, Convert.ToInt32(txtPortNbr.Text.Trim()));

        for (int i = 0; i <= lLength - 1; i++)

        {

            SelProfile = ProfileCollection.Item(i);

            //Set selected profile as Windows Media Video 8 for Local Area //Network,if broadcasting is enabled.

            if (SelProfile.Name == "Windows Media Video 8 for Local Area Network (768 Kbps)")

            {

                SrcGroup.set_Profile((IWMEncProfile)SelProfile);

                break;

            }

        }

    }

    else

    {

        for (int i = 0; i <= lLength - 1; i++)

        {

            SelProfile = ProfileCollection.Item(i);

            if (SelProfile.Name == "Screen Video/Audio High (CBR)")

            {

                SrcGroup.set_Profile((IWMEncProfile)SelProfile);

                break;

            }

        }

    }

    //Local File to Store Recording temporarily.

    IWMEncFile inputFile = DesktopEncoder.File;

    inputFile.LocalFileName = "C:\\TempRecording.wmv";

    DesktopEncoder.PrepareToEncode(true);

    DesktopEncoder.Start();

    tmrRcCounter.Enabled = true;

    recordStarttime = DateTime.Now;

    if (toolstripEnableBroadcast.Checked && txtPortNbr.Text.Trim() != "")

    {

        //Start Timer to Count Viewers connected to Broadcast.if broadcasting is enabled.

        tmrViewerCount.Enabled = true;

    }

}

catch (Exception ex)

{

    MessageBox.Show(ex.Message);

}

On Click of Stop Recording --> Here, Encoder will be stopped for stopping recording. Later, it will popup save dialog to store the recording to a file. The path of the file, where recording is saved will be added to Recent Recordings menu.

On Click of Exit (application closing) --> Here, all recent recording's path will be added to a file for loading it later.

Steps to be followed in using this application:

  1. Start the application, than click on Start Recording to start recording of desktop operations.
  2. If you want to broadcast (to be viewed by others) this recording, than goto Broadcast menu and click Enable Broadcast, set port number to be used for broadcasting. It will use HTTP protocol for broadcasting. So, it won't be blocked by firewalls. Than, start the recording. So that, at a time you can record your desktop operations and broadcast it also.
  3. If you want to add Audio to your recording, than goto Enter Audio File Path and set audio filename along with its path. Than, start the recording. So that, at a time you can record your desktop operations along with audio appended to it.
  4. Then, Click on Stop Recording when you are done with it. It will popup a save dialog to select filename for saving the recording.
  5. Then, you can play those recordings by going to Recent Recordings and selecting that filename from it.
  6. If you want to watch broadcasting of the desktop, than goto Options Menu and select set URL of the Broadcast ( like http://10.100.1.100:8000) and hit Enter. It will start playing the broadcast.
  7. In Status bar, you can see recording duration followed by number of users connected to your broadcast.

Finally, I added some code to enhance UI of the application. And, the final output will be like this:

We can still enhance this application by improving UI, Broadcast Quality etc.

By using this application, we can record all your Desktop activities. I am attaching source code for further reference. I hope this code will be useful for all.

Login to add your contents and source code to this article
Article Extensions
Contents added by K V Reddy on Apr 10, 2012

very good one.

thank you so much...

post comment
     

Hi could you please explain the broadcast feature in you application, Right now as setting the BroadCast url like here " http://10.100.1.100:8000 " Finally i browsed the given url , nothing happened, Please give me an suggestion on Broadcasting..

Posted by shanmuga vijay Mar 27, 2013

hai any one have sample code for desktop recorder .

Posted by david lee Jun 23, 2012

Windows media encoder 9 is installed allready?

Posted by Musa Fatih CAGLAR Mar 27, 2012

sir it will give following exception_ System.Runtime.InteropServices.COMException was unhandled Message="Exception from HRESULT: 0xC00D0072" Source="Interop.WMEncoderLib" ErrorCode=-1072889742 StackTrace: at WMEncoderLib.IWMEncoder.PrepareToEncode(Boolean bPrepare) at DesktopRecorder.frmRecorder.startRecordingToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Documents and Settings\user2\Desktop\DesktopRecoder\DesktopRecorder\DesktopRecorder\DesktopRecorder.cs:line 196 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at DesktopRecorder.Program.Main() in C:\Documents and Settings\user2\Desktop\DesktopRecoder\DesktopRecorder\DesktopRecorder\Program.cs:line 17 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: --- Can u plz tell wht is going wrong....

Posted by Bhutesh Kumar Mar 27, 2012

Hello,your good application is helpful to me for my current screen recorder module. At the same time, i have a vlc media player embeded in my project, i'd like to use libvlc to resolve streaming video over lan. my vlc can play video with black screen. How can i resolve the bug? Is it possible to convert wmv(mss2) to in a regular wmv file? Thanks in advance.

Posted by Mason Chan May 10, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.