When a new MediaElement is made and inserted into a Grid, it is auto-sized to fit the entire Grid size. How can you adjust the proportions of the MediaElement to mimic the video's original size ?
Grid gDim = new Grid();
MediaElement plr = new MediaElement();
plr.Source = new Uri(my_string); // web link to video file
plr.Play();
plr.Width = plr.NaturalVideoWidth ;
plr.Height = plr.NaturalVideoHeight ;
plr.AreTransportControlsEnabled = true;
gDim.Children.Add(plr);
Setting the NaturalVideoWidth/Height makes the MediaElement go invisible.
Did you try my advice for this case(using Stretch property)?
<MediaElement Stretch="None"/>
Related
i am trying to create a animation using grid. it's a login screen. whenever user taps forget password i want the second grid to animate from top and slide till it stops at center and on tap it's visibility changes. i know how to do it using blend but the thing is i hav a compulsion for doing it from code behind. For that i am using doublekeyframe class. Having real trouble in knowing where is the issue in code behind for animating the second grid. Don't know what the issue and how to animate so serious help needed.
here is my code behind:
Grid gd= this.FindName("SecondaryGrid") as Grid;
DoubleAnimationUsingKeyFrames dm=new DoubleAnimationUsingKeyFrames();
LinearDoubleKeyFrame l1=new LinearDoubleKeyFrame();
LinearDoubleKeyFrame l2=new LinearDoubleKeyFrame();
l1.Value=-703.203;
l1.KeyTime=TimeSpan.FromSeconds(0);
l2.Value=0;
l2.KeyTime=TimeSpan.FromSeconds(1);
dm.KeyFrames.Add(l1);
dm.KeyFrames.Add(l2);
dm.Duration=new Duration(TimeSpan.FromMilliseconds(3000));
Storyboard sb = new Storyboard();
sb.Children.Add(dm);
Storyboard.SetTarget(dm, gd);
Storyboard.SetTargetName(dm, gd.Name);
Storyboard.SetTargetProperty(dm, "Position");
sb.Begin();
SecondaryGrid.Visibility = Visibility.Visible;
Grid gd = this.FindName("SecondaryGrid") as Grid;
DoubleAnimationUsingKeyFrames dm = new DoubleAnimationUsingKeyFrames();
LinearDoubleKeyFrame l1 = new LinearDoubleKeyFrame();
LinearDoubleKeyFrame l2 = new LinearDoubleKeyFrame();
l1.Value = -703.203;
l1.KeyTime = TimeSpan.FromSeconds(0);
l2.Value = 0;
l2.KeyTime = TimeSpan.FromSeconds(1);
dm.KeyFrames.Add(l1);
dm.KeyFrames.Add(l2);
dm.Duration = new Duration(TimeSpan.FromMilliseconds(30000));
Storyboard sb = new Storyboard();
sb.Children.Add(dm);
Storyboard.SetTarget(dm, gd);
Storyboard.SetTargetName(dm, gd.Name);
Storyboard.SetTargetProperty(dm, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");
sb.Begin();
Enjoy :)
EDIT
Well what I did was went to XAML and created a SecondaryGrid now in its properties went to render transform and clicked on edit and set Y value as 1 then I saw in XAML that we get
<Grid.RenderTransform>
<CompositeTransform TranslateX="0" TranslateY="1"/>
</Grid.RenderTransform>
So from this I knew that since there is no position property so I need to set targetProperty as render transform and in that I should Change the Y property since it needs to come from top to bottom which is present inside Composite Transform.TranslateY
How can I create a grid in which i can put a box like structure that contains an image and a label using xamarin.forms.
I am posting the image which shows what I need
"I" refers to the Image
"N" refers to the number
"W" refers to the text
There are two separate components here.
First is the creation of a custom layout you need for the image, number and text.
The second is how you then display that custom layout inside your page.
The Grid is certainly a possibility for the second component, but you really don't need it for the first one. Especially because nesting grids inside Xamarin.Forms thrashes layout performance.
So use either two stack layouts or a relative layout.
For the former
StackLayout box = new StackLayout {
Orientation = Vertical
};
StackLayout firstRow = new StackLayout {
Orientation = Horizontal
};
firstRow.Children.Add(new Image {Source="FileName.png"});
firstRow.Children.Add(new Label {Text="1", HorizontalOptions = LayoutOptions.End});
box.Children.Add(firstRow);
box.Children.Add(new Label { Text = "This is the text" });
Or for the latter, something like
RelativeLayout rl = new RelativeLayout();
var image = new Image {Source="FileName.png"};
var numberLabel = new Label {Text="1"};
var textLabel = new Label{Text="This is the text"};
rl.Children.Add(image,Constraint.Constant(0),Constraint.Constant(0)); //Add image at 0,0 = top left
rl.Children.Add(numberLabel,Constraint.RelativeToParrent(parent=>parent.Width - numberLabel.Width),Constraint.Constant(0)); // add numberLabel at the top right corner of the relative layout
rl.Children.Add(textLabel,Constraint.RelativeToView(image,(sibling,parent) => sibling.Y + sibling.Height + 20),Constraint.Constant(0)); // Add text label below the image, on the left side of the relative layout
The Source of the Image is bound to a URL which points to an image.
If the image at the URL is smaller then the MaxHeight and MaxWidth, the following code works great. The image size is exactly the same as the url and the window is sized properly.
If the image at the URL is larger then the MaxHeight and MaxWidth, only a portion of the image is displayed. The image does not get shrunk to fit into the window.
If I remove Stretch="None", the large picture then shrinks to fit into the MaxHeight and MaxWidth, looks great, but the small image gets expanded to consume all available space and looks like crap.
Here are two images I have been testing with:
http://imgur.com/iaBp2Fv,fiRrTJS#0
<Window x:Class="MyNamespace.Windows.PictureWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Profile Picture" ResizeMode="NoResize" UseLayoutRounding="True" SizeToContent="WidthAndHeight" MaxHeight="750" MaxWidth="750">
<Image Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding}" />
</Window>
What you could do is to to remove the Stretch="None", as you say, to make the large image shrink down. But, to avoid the small image to be scaled up, you just add this property:
StretchDirection="DownOnly"
This prevents small images from being scaled upwards, and allows large images to be scaled down. The Window resizes appropriately as well.
This is the code that I have tested in LinqPad. Just change showLarge to true and false to switch between the images.
bool showLarge = false;
var w = new Window();
w.ResizeMode = ResizeMode.NoResize;
w.UseLayoutRounding = true;
w.SizeToContent = SizeToContent.WidthAndHeight;
w.MaxHeight = 750;
w.MaxWidth = 750;
Image img = new Image();
img.HorizontalAlignment = HorizontalAlignment.Center;
img.VerticalAlignment = VerticalAlignment.Center;
img.StretchDirection = StretchDirection.DownOnly;
if(showLarge)
img.Source = new BitmapImage(new System.Uri(#"http://i.imgur.com/iaBp2Fv.jpg"));
else
img.Source = new BitmapImage(new System.Uri(#"http://i.imgur.com/fiRrTJS.jpg"));
w.Content = img;
w.ShowDialog();
In your code behind, create a property as
public Point ImageSize {get;set}
Get the image from the URL in the constructor/Initialize() and set ImageSize accordingly
Bind the height and width of your window to ImageSize.X and ImageSize.Y
Height="{Binding ImageSize.Y}" Width="{Binding ImageSize.Y}"
I want to resize the image that I am going to display by keeping its aspect ratio.
How can I achieve that. ?
public MainPage()
{
InitializeComponent();
myImage = new Image();
Uri uri = new Uri("Penguins.jpg", UriKind.Relative);
bit = new BitmapImage(uri);
myImage.Source = bit;
imagePanel.Children.Add(myImage);
}
Here the image is stored in the variable bit. How can I resize it by keeping its aspect ratio ?
Thanks.
There is a Stretch property in Image class. You can set None, Uniform or UniformToFill to keep aspect ratio when image is resized in order to fill image area
I've not bothered with panels, docking, or anchors. I've simply thrown together a ToolBar control (not ToolStrip) and seem unable to size it.
System.Windows.Forms.ToolBar tb = new System.Windows.Forms.ToolBar();
// Reports 292x28 (approx) if I check width and height
// Basically the width of the form and I assume a default height
tb.Size = new System.Drawing.Size(195, 48);
// Reports 48x48, but does not actually create buttons of that size
// (It reports 48x48 because I'm retrieving 48x48 icons from a ResourceManager (resx))
tb.ButtonSize = new System.Drawing.Size(48, 48); //
The closest thing I found to making my ToolBar taller was:
http://bytes.com/topic/c-sharp/answers/241614-changing-height-toolbar-button
Although it's rather dated. And I didn't understand it. ToolBarButtons don't have Height, Width, or Size properties.
I'm using SharpDevelop, coding completely by hand on Vista, with all the .NET frameworks.
EDIT:
Here is the EXACT code that I am currently using.
#region ImageList/Toolbar
ImageList toolbarImages = new ImageList();
Image wizardToolbarImage = (Bitmap) rm.GetObject("wizard");
Image optionsToolbarImage = (Bitmap) rm.GetObject("configure");
toolbarImages.Images.Add(wizardToolbarImage);
toolbarImages.Images.Add(optionsToolbarImage);
ToolBar toolbarMain = new ToolBar();
toolbarMain.Size = new Size(195, 25); // no effect
ToolBarButton wizardToolbarButton = new ToolBarButton();
ToolBarButton optionsToolbarButton = new ToolBarButton();
wizardToolbarButton.ImageIndex = 0;
wizardToolbarButton.ToolTipText = "Wizard!";
optionsToolbarButton.ImageIndex = 1;
optionsToolbarButton.ToolTipText = "Options!";
toolbarMain.Buttons.Add(wizardToolbarButton);
toolbarMain.Buttons.Add(optionsToolbarButton);
toolbarMain.Appearance = ToolBarAppearance.Normal;
toolbarMain.ButtonSize = new System.Drawing.Size(48, 48); // no effect
toolbarMain.ImageList = toolbarImages;
toolbarMain.ButtonClick += new ToolBarButtonClickEventHandler(toolbarMain_Click);
Controls.Add(toolbarMain);
#endregion
In just about every winforms application I've written, regardless of language or framework, the toolbar could only be made taller by using larger icons.
You can also put the toolstrip inside a Panel and set the Dock property of the tool strip to Fill. And then you can size the Panel to whatever size you need.