Ron
Ron

Reputation: 4095

Media Player Control stretch to fit window size

I have the following code to load media and display it using Media Player Control:

MediaPlayer Player = new MediaPlayer();
Player.Open(new Uri(videoURI, UriKind.Absolute));
VideoDrawing aVideoDrawing = new VideoDrawing();
aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
aVideoDrawing.Player = Player;
DrawingBrush brush = new DrawingBrush(aVideoDrawing);
this.Background = brush;

No matter what is the size of the movie I play, it stretches to 1920x1080 (the window size).
I want it to be in the original full size and if the height/width is less then 1920/1080 it will center the video.

Since there's no physical control, I have no Idea how to do it...

Will appreciate your help.

Upvotes: 1

Views: 4279

Answers (1)

kmatyaszek
kmatyaszek

Reputation: 19296

Try this:

Add two fields to class level:

MediaPlayer Player;
VideoDrawing aVideoDrawing;

In constructor add following code:

Player = new MediaPlayer();
Player.MediaOpened += Player_MediaOpened;
Player.Open(new Uri(videoURI, UriKind.Absolute));
aVideoDrawing = new VideoDrawing();                    
aVideoDrawing.Player = Player;
DrawingBrush brush = new DrawingBrush(aVideoDrawing);
brush.Stretch = Stretch.None;
this.Background = brush;

In MediaOpened event handler set appropriate size:

void Player_MediaOpened(object sender, EventArgs e)
{
    if (Player.NaturalVideoWidth <= 1920 && Player.NaturalVideoHeight <= 1080)
        aVideoDrawing.Rect = new Rect(0, 0, Player.NaturalVideoWidth, Player.NaturalVideoHeight);
    else
        aVideoDrawing.Rect = new Rect(0, 0, 1920, 1080);
}

You must set size in MediaOpened event handler because NaturalVideoWidth and NaturalVideoHeight are not accurate until the MediaOpened event has been raised.

Upvotes: 2

Related Questions