Link to home
Start Free TrialLog in
Avatar of Liveinteractive
Liveinteractive

asked on

DirectShow and Transparency

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 Const nStyleEx As Long = WS_EX_TOPMOST Or WS_EX_TOOLWINDOW Or WS_EX_LAYERED

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
    m_MediaControl.Run
End Sub
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

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 :

http://www.mvps.org/directx/faq.php#tpc20_1

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 :

http://www.pscode.com/vb/scripts/BrowseCategoryOrSearchResults.asp?txtCriteria=DirectShow&blnWorldDropDownUsed=TRUE&txtMaxNumberOfEntriesPerPage=10&blnResetAllVariables=TRUE&lngWId=1&optSort=Alphabetical

Avatar of Liveinteractive
Liveinteractive

ASKER

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
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 ?
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)
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 :)
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 Or WS_EX_TOOLWINDOW Or WS_EX_LAYERED
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
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 :)
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
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.
I just tried to explain you, sorry...
I have posted this issue en those sections (VB, C++, etc)

Thank you very much
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
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directshow/htm/backgroundpalettepropertyivideowindowobject.asp
https://www.experts-exchange.com/questions/21371139/How-can-set-FilgraphManager-do-NOT-use-hardware-overlay.html
Can you post me the code or a place to download it?
I want to levave the question as closed
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial