DirectShow and Transparency

Posted on 2005-03-11
Medium Priority
Last Modified: 2013-12-13
I have this code to show a video transparent (test it with any video and select an according transparent color), but it doesn't work. This code is for Win 2K and XP (you have to iclude the ActiveMovie control typelibrary)

' Change this constans to test
Private Const sFileName As String = "c:\temp\pepe.avi"
Private Const nTranspColor As Long = &HFF00

Private m_MediaControl As IMediaControl
Private m_VideoWindow As IVideoWindow
Private m_hVideoWnd As Long

Private Const WS_POPUP As Long = &H80000000
Private Const WS_CLIPSIBLINGS As Long = &H4000000
Private Const nStyle As Long = WS_POPUP Or WS_CLIPSIBLINGS
Private Const WS_EX_TOPMOST As Long = &H8
Private Const WS_EX_TOOLWINDOW As Long = &H80
Private Const WS_EX_LAYERED As Long = &H80000

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Const LWA_COLORKEY As Long = &H1
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Long, ByVal dwFlags As Long) As Long

Private Sub Command1_Click()
    ' Clear previous instances
    Set m_MediaControl = Nothing
    Set m_VideoWindow = Nothing
    ' Create the object
    Set m_MediaControl = New FilgraphManager
    Set m_VideoWindow = m_MediaControl
    ' Select the file
    m_MediaControl.RenderFile sFileName
    ' Get the window handle
    m_VideoWindow.Caption = Rnd & "_" & Rnd
    m_hVideoWnd = FindWindow("VideoRenderer", m_VideoWindow.Caption)

    ' Set the styles
    m_VideoWindow.WindowStyle = nStyle
    m_VideoWindow.WindowStyleEx = nStyleEx

    ' Select the Transparent color
    SetLayeredWindowAttributes m_hVideoWnd, nTranspColor, 255, LWA_COLORKEY

    ' Run the video
End Sub
Question by:Liveinteractive
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 23

Expert Comment

ID: 13583434
I found this :

When loading a texture with the D3DX texture creation functions I specify a color key, but no transparency appears when I render the image.

This is often caused by specifying a key color that does not have opaque alpha. Pixels read from a file without an alpha channel are assumed to be opaque (alpha of 255), so comparison with a key color with an alpha other than 255 will automatically fail. You can readily check to see if this is the problem by ORing your key color with 0xff000000.

From this website :


What if you use a different move file ie a different AVI or a different MPEG video file ?

Also take a look at the search I did on pscode :


LVL 23

Expert Comment

ID: 13583446
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13590533
1.      thanks
2.      I don't understand what's "D3DX texture creation", can you post me an example?
3.      I’ve tried with others AVIs and MPGs
4.      What interesting thing did you found in pscode
5.      Ok, Alpha blending is another option instead of color key, but my problem is with the window where I play the video
6.      I’m not trying to render the video over other image / video, I’m trying to display it over the desktop or other window in a TOP MOST window with transparent parts
LVL 23

Expert Comment

ID: 13591064
You said this :

5.     Ok, Alpha blending is another option instead of color key, but my problem is with the window where I play the video

Can you ellaborate what your problem is with the window you play the video on, is it because you need to use the handle to that window before you use the transparancy key ?

Author Comment

ID: 13591090
If you execute my example, you can see the problem, select a sFileName (for example clock.avi) and set nTranspColor to the background color of the AVI (this parts must show transparent)
LVL 23

Expert Comment

ID: 13591278
Your best bet would be to download and use the Direct X SDK from there website as that would have a lot of examples and I am sure there is one in there that would suit your needs :)

Author Comment

ID: 13591366
I think you didn't understand my needs.

I’d like to create a control, like the office assistant that show a video on a topmost transparent layered window.

I render a file and assign those properties:
WS_EX_TOPMOST: Top most window
WS_EX_TOOLWINDOW: no title or task bar icon
WS_EX_LAYERED: Layered window

And then call SetLayeredWindowAttributes to set the transparent color
LVL 23

Expert Comment

ID: 13597238
This is for visual basic 6.0 right ? might be an idea to post a pointer question in the visual basic section and maybe also a pointer in the vb controls section to here or even get an admin to move the question to one or both of them areas. If that is possible, not sure though ? I have not done much with regards to controls so see if another expert can help you out :)

Author Comment

ID: 13600411
This example is in VB 6, but I have the same problem in C++, is a Windows (o direct show) problem, not a language issue
LVL 23

Expert Comment

ID: 13602756
I am not trying to dispute what type of problem it is , I am just suggesting that maybe if people from the vb components area or a different section (withint programming obviously ) would have a better chance of seeing your question then they will try to help you. I mean Idle_Mind, Erick37, Bingie and a few others would have a really good chance at helping you solve this as they are very experienced.

Author Comment

ID: 13602811
I just tried to explain you, sorry...
I have posted this issue en those sections (VB, C++, etc)

Thank you very much
LVL 23

Expert Comment

ID: 13602849
Go to erick37 or bingies profile(s) , I think one of them has an email address and email them the URL's to your questions

Expert Comment

ID: 13703303
I have personally acomplished video transparency. From memory I think I used the Windows Media Player OCX (pre-9.x), and turned the overlay feature OFF. This menat software rendered the vid (and PrntSc could capture it). I then set the Form's TransparencyKey to the background colour of the vid.

It didn't work perfectly:
* annoyingly, the software rendering did it in 8 bit colour!!! and about 5% of the pallette was wrong (made vid look ugly)
* the vid was compressed so background colour wasnt 100% the same color. i needed a threshold feature, which there isn't. use fractal codec instead (pricey).

Maybe interesting URLs:

Author Comment

ID: 13707031
Can you post me the code or a place to download it?

Author Comment

ID: 14321796
I want to levave the question as closed

Accepted Solution

modulo earned 0 total points
ID: 14394968
PAQed with points refunded (500)

Community Support Moderator

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question