Click here to Skip to main content
Click here to Skip to main content

Transparent Desktop Video

By , 26 May 2005
 

Sample screenshot

Introduction

This simple C# WinForms project allows you to playback video from a live capture device or from an existing video file and blend the video using transparency with the existing Windows desktop. The user can still interact with all other application windows "through" the playing video stream.

Background

A while ago, a group from the University of North Carolina published a paper detailing a system they called FaceTop that makes use of transparent video streams for use in collaboration environments. By using transparent video over the desktop, the participant can view a video stream of the other participant while at the same time being able to read the contents of their desktop and look at items on their desktop that the other participant is referring to.

FaceTop makes use of MacOS X's Quartz composition engine to compose the video stream with the other desktop windows and apply the relevant transparency during composition. Windows has offered support for transparent top-level windows since the release of Windows 2000 using layered windows. For more details on this support for layered windows, see the following MSDN article.

At the time that the FaceTop paper was published, I was working on a video capture and playback system for aerial surveillance work. So my plan was to see how easy it would be to implement some of the basic features of FaceTop, i.e., transparent video playback that would allow you to watch the video and still allow you to see and interact with your desktop at the same time.

Code Overview

The basic setup consists of a C# WinForms application hosting the Windows Media Player control with a couple of specific properties set on the main form and the Media Player control.

To start off the Windows Media Player, the Primary Interop Assembly needs to be installed and referenced. See this MSDN article for details on downloading and installing the Windows Media Player's PIA.

After inserting the Windows Media Player control onto the form, the following three properties need to be set on the control:

  • uiMode - None since we don't want the Media Player's controls visible, we only want to see the video stream.
  • windowlessVideo - true since we want the Media Player to use the control host's (our main form's) window and not create its own child window in which to render the video. The reason for this is that the layered windows transparency support in the OS that we're making use of only works for top-level windows and not for child windows. See the MSDN article for more details.
  • URL - source of the video. This can refer to an existing video, e.g. file:///c:/somevideo.wmv or to a video being streamed, e.g. http://somehost/.

Then the following properties need to be set on the form:

  • FormBorderStyle - None
  • Topmost - true
  • WindowState - Maximized
  • Opacity - 0.55 or whatever amount of transparency you'd like to use.

In the Form's Load event we need to add the WS_EX_TRANSPARENT extended window style to our form's window in order for input events from the mouse and keyboard to be passed through to the underlying windows.

        [DllImport("user32.dll", SetLastError=true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll", SetLastError=true)]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        private const int  GWL_EXSTYLE = -20;
        private const int  WS_EX_TRANSPARENT = 0x20;

        private void VideoPlayer_Load(object sender, System.EventArgs e)
        {
            // Add WS_EX_TRANSPARENT style so that mouse, keyboard etc. events pass
            // thru us.
            int exstyle = GetWindowLong(this.Handle, GWL_EXSTYLE);
            exstyle |= WS_EX_TRANSPARENT;
            SetWindowLong(this.Handle, GWL_EXSTYLE, exstyle);
        }

Summary

The sample screenshot at the top of the article shows a live video stream being captured by a DV camera attached to my system which is then being encoded to WMV using Microsoft's Media Encoder software and then being displayed as a transparent video over the desktop using my application by pointing the Media Player to http://localhost:8080/ to pick up the video stream.

Overall there was very little effort required to enable basic transparent video playback over the desktop.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sean McLeod
Web Developer
South Africa South Africa
Member
Software developer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanksmemberKOUKKILLER15 Sep '12 - 22:12 
REALLY USEFULL PROJECT TY!!!
GeneralThanksmemberMarkJoel6029 Mar '10 - 4:22 
Wow... such a simple thing... but I have been trying to create an overlay screen that passed the mouse and keyboard events through and couldn't find it until I ran across your article. Thanks!
GeneralHi Guysmemberdansaliveagain9 Feb '10 - 18:49 
Im glad to see this code project article as i have known this function is simple and available for a long time.... however, its not my area of specialty and don't have the time available to learn/do it
 
I have posted a job/project on guru.com offering somebody to make an image player that plays over the top of all other applications (with still photos from database/folder, or video)
 
Ideally a player compatible with both Mac OS and Windows and an opacity control to fade player behind all other apps or in front of all other open apps
if you would like to make some cash
refer guru.com / image player or
reply here or private email
 
cheers
 
DAN
Generalen...memberjosmoi29 Apr '09 - 20:43 
not good...
QuestionWorking on VS 2005 / 2008?membermyspaceebay0014 Dec '08 - 12:43 
I've run through all the documentation and all the examples and I simply can't get it to work.
 
Can any one get it to work in VS 2005 or VS 2008?
 
I will pay money to anyone who can help me get this working (being able to play a transparent video on my computer)
 
Thanks,
Phil
GeneralDual Monitorsmemberdbbtvlzfpz10 Sep '08 - 6:03 
I got it to run, but it displays on the second monitor instead of the primary monitor. This could be because of Nvidia Control Panel Settings, but since I do a lot of video work, I won't be changing that behavior.
 
Very cool program though!
 
For those with one monitor. ;o)
Generali have errormemberwebshot8 Aug '08 - 7:53 
i run it on C# 2008 and it return error
Error 1 The type or namespace name 'AxMicrosoft' could not be found (are you missing a using directive or an assembly reference?) E:\download temp\C#\TransparentDesktopVideo_src\VideoPlayer.cs 16 11 VideoPlayer
 
private AxMicrosoft.MediaPlayer.Interop.AxWindowsMediaPlayer axWindowsMediaPlayer1;
<->
how to fix it??
GeneralActivex problemmembermyceng21 Aug '07 - 22:21 
An unhandled exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.WMPLib.dll
this is the error that i have and i want your help please .
by the way very good program idea .
 
thank you

GeneralRe: Activex problemmembermyspaceebay0014 Dec '08 - 11:45 
I think he used an old version of the Windows Media Player. The new one doesn't work. As far as I've found...
Questionproblems with transfer to a different computermembernomism4 Nov '06 - 4:56 
I installed the WMP SDK, registered the pia and compiled the source with Visual Studio.
It works perfectly fine and it plays the video.
But when I try to run it on a different computer it doesn't work.
I copied the whole bin\Debug folder to another computer (including the AxInterop.WMPLib.dll). The Computer has the .Net Framework installed. But when I try to run the exe-file, windows tells me, the program had to be terminated.
It would be really great if s.o. could give me a hint on this.
 
Thanks a lot.
 
Simon
 
P.S.: On the second computer I run the same Version of WMP. I didn't install the WMP SDK nor did I register the pia on the second computer. But that's not necessary is it?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 26 May 2005
Article Copyright 2005 by Sean McLeod
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid