Stretch="uniform" not behaving as expected on Silverlight MediaElement - c#

I am using the following XAML to display streaming video through a Silverlight media element:
<UserControl x:Class="slplayer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Padding="0">
<!-- source is set to a custom MediaStreamSource in code behind -->
<MediaElement Name="mediaElement" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Margin="0" Stretch="Uniform" />
</UserControl>
And expect that video should playback and should, depending on the size of the playback window, consume either all available horizontal space or all available vertical space and scale the non-constrained dimension to maintain the aspect ratio of the video.
In the case where the vertical dimension is constrained this is exactly what happens, however (as you can see in the screen shot below) when the horizontal dimension is constrained a significant amount of space is left on either side and the vertical dimension is scaled to this narrower width.
My question is why is the video not consuming all horizontal space?
Things I've tried:
simplifying the layout (which is how I got the above XAML)
hosting the control in both a web browser and a SilverlightViewportElement
modifying the dimensions of the mp4 file and video track
playing the mp4 file in WMP (which scales correctly)
setting css styles on the html, body, div, and object used
The problem illustrated:

Try putting your MediaElement in a grid.

Put your MediaElement in a Grid and remove all Alignment Stretch settings and also Padding and Margin too. Only set Stretch as uniform for MediaElement.
PS: Check your aspx div for Silverlight object. Its Width and Height might have been set with wrong values.

Related

scale and draw image inside a lager canvas

Hi I'm hoping someone can help point me in the right direction to how i might go about solving this issue.
I have a blank image (white) which is 3000x1500 which acts a a canvas, i then have an image which again could be any size and any orientation. What i need to do is scale this image to fit inside the blank white canvas as best as possible and also center it. I would expect to see white gaps at the top or bottom of the final image if the image could not be scaled to fix the exact canvas.
Can anyone suggest what i should research, how i would go about drawing an image inside another in C# WPF and anything that may already exist that i could use to achieve this.
I forgot to mention that this would need to output to a bitmap so it could be saved to disk
All you need to do is to put an Image control into an appropriately sized Grid with white background, and set its Stretch property to Uniform. No Viewbox required.
<Grid Width="3000" Height="1500" Background="White">
<Image Source="<path to image file>" Stretch="Uniform"/>
</Grid>
Look up ViewBox. A ViewBox automatically scales its single child element based off of the stretch parameter.

how to overlay an image over an image in WPF?

I'm trying to figure out how to overlay an image or textbox over an image in WPF. The base image will be hosting a video feed from a Kinect sensor and I want to overlay an image on it. For example the video feed will have a textbox on the feed that will update a counter or an image of a tree on top of the video feed.
Is there a straightforward way of achieving this in the layout? Is it just a matter of changing properties or adding a canvas?
The below picture better illustrates what it is I'm trying to do:
You can use two transparent canvas inside the Grid without any row and column then place your objects in Back and front Canvas accordingly they will overlap
that is:
<Grid>
<VideoControl><!-- I've never tried video --></VideoControl>
<TextBlock Text="My Text" />
</Grid>
Usually you specify <Grid.ColumnDefinitions> and <Grid.RowDefinitions> but since you do not du that here the controls will overlap
HTH
The usual way for me to overlay items in WPF is just to put all of the elements in a Grid. If you do not define any Columns or Rows the elements will overlap.
Example
<Grid>
<Image Source="image on lowest layer" />
<Image Source="overlaying image" />
</Grid>

Automatic height for WebBrowser Windows Phone 8

I'm trying to render some html content with a WebBrowser xaml control. The html content varies in length. The control is placed within a grid with single row at the moment. I would like to place other controls (StackPanels) before and after the web browser and to get vertical scrolling across the entire layout.
Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<phone:WebBrowser
Grid.Row="0"
x:Name="webBrowser1"
Background="Black"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
IsScriptEnabled="False"
Visibility="Visible">
</phone:WebBrowser>
<ProgressBar Visibility="Visible" x:Name="LoadingProgress" Indeterminate="True"></ProgressBar>
</Grid>
However I can only scroll the current viewport, being forced to apply a fixed height to the webbrowser.
The theoretical structure I'm aiming to is:
scrollviewer
StackPanel
WebBrowser (probably within another stackPanel)
StackPanel
...
Stackanel
and would like to scroll the scrollViewer, not the webbrowser, so the browser would automatically resize based on the content loaded. Any suggestions?
I found a good blog post on MSDN that addresses this issue: http://blogs.msdn.com/b/mikeormond/archive/2010/12/16/displaying-html-content-in-windows-phone-7.aspx
The blogger was initially trying to accomplish the same thing as you, placing a browser into a scrollviewer, etc. But found that it didn't work well for larger html pages, they went with a different solution to disable scrolling and zooming in the browser control. Hope this can help:
This worked fine until dealing with longer bodies of content; when the
height of the WebBrowser control was set to more than 1800px the the
application would crash. As it turned out, there’s a much easier way
to achieve the same effect.
Instead of disabling hit testing on the WebBrowser control (and then
embedding it in a ScrollViewer to re-enabled the scrolling
experience), it’s possible to set meta tags in the HTML to declare
that the user should not be able to zoom the content.
or
These meta tags set the width of the viewport to 320px (to avoid
horizontal scrolling) and specify that the user should not be able to
scale the viewport. Add these meta tags to the HTML injected into the
WebBrowser control and the desired behaviour is achieved.
To get the height from the webview, you can use
window.external.notify("rendered_height=document.getElementById('yourId').offsetHeight")
in body.onload and body.onresize
Here is a complete example of an AdaptativeWebview that handle scroll and resize

ScrollViewer's ScrollToHorizontalOffset not working in Windows 8 app

I have a Windows Store app with a ScrollViewer and an Image in it. When i double tap on the ScrollViewer I want it to zoom the Image to its width. This part is not a problem but I also want the Image to be centered after it has beed zoomed in.
I tried calling the ScrollToHorizontalOffset method on the ScrollViewer but It does not seem to work with any number I give it. What is the problem?
Perhaps the offset only works for the non-zoomed view where your image fills the ScrollViewer completely and thus can't be scrolled. You could try setting the image dimensions so that it is larger than the ScrollViewer, but set original ZoomFactor, so that it fills the ScrollViewer at first. Then zooming and scrolling might work.
Assign a SizeChangedEvent in the scrollviewer.
<ScrollViewer SizeChanged="OnSizeChange"></ScrollViewer>
like this. Then it is better to place your image inside a canvas. So your code will be probably like this.
<ScrollViewer SizeChanged="OnSizeChange" x:Name="scrl">
<Canvas RenderTransformOrigin="0.5,0.5" x:Name="main">
<Image source="" Canvas.Top="insert desire double value here", Canvas.Left="Same goes here"/>
</Canvas>
</ScrollViewer>
then in the code behind you can change the height and width of your canvass depending on the scroll viewer
Main.Width = scrl.ViewPortWidth;
Main.Height = scrl.ViewPortHeight;
You can experiment on the size of the canvass while adding a double tap event to it. Changing the size of the canvas can zoom in or out the image because the image is inside the canvass
Try with 'ChangeView' instead of 'ZoomToFactor'

Grid On Top Of A WINRT Media Element Causes Video To Be Blank

I have a MediaElement full screen on a page. On top of this I have a grid that contains controls (Play,Pause etc..) and general info about the video. These controls are hidden after a few seconds of no user interaction.
If any part of the controls overlap the video being played then the entire Video is blanked out and only reappears when the controls grid is collapsed. Is it not possible to have a control of any kind over the top of a media element?
I notice that the "Video" App that comes as part of Windows8 RTM has this kind of effect over the top of a video that is playing.
UPDATE:
I've found the problem but not a solution. I'm trying to have a "Global" mediaelement so I can view it on different pages (Fullscreen, preview etc). I found this answer
http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/241ba3b4-3e2a-4f9b-a704-87c7b1be7988
And followed Jim Man's suggestion to create a MediaElement on the root frame and then pull that item when needed. The issue happens as soon as I say CurrentPageMediaElement = RootFrameMediaElement
I presume it's changing the z order of the media element to that of the "Global" media element in the root frame? I'm not sure why it shows video when ControlsGrid is collapsed as the root grid is still there. If I change the root grid to have an opacity less than 1 I see the media element all the time as in the code below.
If someone has a better way to share a Global Media Element around then that would also work.
//Here I Can't See The MediaElement
<Grid Style="{StaticResource LayoutRootStyle}" Background="Black">
<MediaElement x:Name="MainMediaElement" />
<Grid x:Name="ControlsGrid" Opacity="0.7" />
</Grid>
//Here I Can See The Media Element
<Grid Style="{StaticResource LayoutRootStyle}" Background="Black" Opacity="0.7">
<MediaElement x:Name="MainMediaElement" />
<Grid x:Name="ControlsGrid" Opacity="0.7">
</Grid>

Categories