MediaControl as ElementHost.Child() and form size

allimbued

Member
Joined
Feb 23, 2012
Messages
11
Programming Experience
Beginner
I am using the System.Windows.Controls.MediaElement
in my ElementHost Control of the form called SplashScreen1.
In order to use the MediaElement, I have added the reference PresentationFramework.dll and imported the mediacontrol. Now I want to play the video called abc.wmv using my MediaElement object. The MediaElement object is instantiated as md and then set as the Child of the ElementHost object, elthst.
Following are the requirements:
1. I want to set the height and width of elthst to the height and width of md
2. The height and width of the SplashScreen1 should be as wide and long as the total width and height (+10)

Please see the code and please advise as to why the video is shrunk on the elthst and why the form is not centered at all.

Thanks

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SplashScreen1_Load([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] md [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Controls.MediaElement[/SIZE]
[SIZE=2]        md.Source = 
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Uri([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"C:\AppPath\abc.wmv"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] elthst [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ElementHost[/SIZE][SIZE=2] 
[SIZE=2]
        elthst.Location = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Location[/SIZE]
[SIZE=2]        elthst.Child = md[/SIZE][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'elthst.Size = New System.Drawing.Size(md.Width, md.Height)
'md.Width and md.Height are NaN when I try to display them on MsgBox[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Controls.Add(elthst)[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].CenterToScreen()[/SIZE]

[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'dynamically compute the size of the splash screen[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i, w, h [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]        w = 0
       h = 0        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i = 0 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Controls.Count - 1[/SIZE]
[SIZE=2]            w +=[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Controls(i).Width[/SIZE]
[SIZE=2]            h += 
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Controls(i).Height[/SIZE][SIZE=2]        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i[/SIZE]
[SIZE=2]      
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Size = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Drawing.Size(w + 10, h + 10)[/SIZE]
[SIZE=2]    
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE]
 
It looks like you need the actualwidth and actualheight of the mediaelement but these are only available when the media is playing unfortunatly, anything else might give you a false reading (unless someone knows differently). As far as centering the form, I have come across this myself and it is usualy because I have centered it and then resized it. The best thing to do is leave Me.CenterToScreen until last.

Hope this is helpfull :D
 
It looks like you need the actualwidth and actualheight of the mediaelement but these are only available when the media is playing unfortunatly, anything else might give you a false reading (unless someone knows differently). As far as centering the form, I have come across this myself and it is usualy because I have centered it and then resized it. The best thing to do is leave Me.CenterToScreen until last.

Hope this is helpfull :D

Thanks for your reply. I realized that the hard way (refer code)
VB.NET:
 Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        md.Source = New System.Uri("C:\AppPath\myVid.wmv")
        md.Stretch = Windows.Media.Stretch.None
        ElementHostMediaControl.Child() = md
       
        lblStatus.Location() = New Point(ElementHostMediaControl.Location.X, md.ActualHeight + 2)
        ProgressBar2.Location() = New Point(ElementHostMediaControl.Location.X, md.ActualHeight + 10)
              
        While md.IsBuffering 'this is ALWAYS FALSE
            md.Pause()
        End While

        If md.HasVideo & md.BufferingProgress = 1 Then 'ALWAYS FALSE
            ProgressBar2.Width = md.NaturalVideoWidth + 20
            ProgressBar2.Location = New Point(Me.Location.X, md.NaturalVideoHeight + 10)
            lblStatus.Location = New Point(Me.Location.X, md.NaturalVideoHeight + 2)
            Me.Size = New System.Drawing.Size(md.ActualWidth + 20, md.ActualHeight + ProgressBar2.Height + lblStatus.Height)
            md.Play()
            Timer1.Start()
        End If
    End Sub

In the code above, the while statement and the if condition is NEVER TRUE ... I am trying to arrange the locations of the form controls and the sizes of the form controls according to the video size and that is not happening. Please help.
 
As far as I can tell the only way you can get the correct width and height without starting the media is to get it from the extended properties of the file. I have done this myself using DSOFile.dll but never for anything like a media file. There must be a way, maybe somone else can help on that or maybe DSOFile.dll has the functionality.

Regards
 
Back
Top