问题
I just want to change the position of the gif in the MediaElement
, so when i don't hover it with the mouse it should display a certain image of the GIF (the selected position) and when i move the cursor on the MediaElement
the GIF should start playing from position zero. But i am not able to change the position of the GIF at all.
It starts playing and i can pause it, but setting position and the stop() method have no influence at all.
XAML Code:
<MediaElement x:Name="mediaElement" Source="C:\temp\smartGif.gif"
ScrubbingEnabled="True" Loaded="mediaElement_Loaded"
MouseLeave="mediaElement_MouseLeave"
MouseEnter="mediaElement_MouseEnter"
LoadedBehavior="Manual"
HorizontalAlignment="Left"
Height="600"
Width="800"
VerticalAlignment="Top"/>
Basic Code:
public UserWindow()
{
InitializeComponent();
}
private void mediaElement_Loaded(object sender, RoutedEventArgs e)
{
mediaElement.Play();
mediaElement.Position = TimeSpan.FromMilliseconds(100);
mediaElement.Pause();
}
private void mediaElement_MouseEnter(object sender, MouseEventArgs e)
{
mediaElement.Play();
mediaElement.Position = TimeSpan.Zero;
}
private void mediaElement_MouseLeave(object sender, MouseEventArgs e)
{
mediaElement.Position = TimeSpan.FromMilliseconds(100);
mediaElement.Pause();
}
Is it right that the MediaElement
needs to play that the position can be changed?
Changes: As suggested i added this:
MediaFailed="mediaElement_MediaFailed"
and that:
private void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show("failed");
}
But it does not show up, i dont know what to do. Is the gif then working fine or what could cause this? Do i need to download gifs in a special way to ensure it supports normal features? I tried it with different gifs and its still not working.
回答1:
Surprisingly no one on the internet has reported this yet and lots of pepople say that it´s working, but it is actually not possible to change the position of a gif running in a MediaElement. The normal way is above in my question, which works for *.mp4 for example, but not for gifs. To convince you the easy way try out to play a gif in the Windows Media Player. As the MediaElement is very thin wrapped around Windows Media Player you will see the same result, changing the position is disabled.
There is a very ugly way how to reset a gif to play it from the beginning, but i dont suggest to use it if you have other options. This can be applied to any event/trigger.
private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
if (mediaElement.Source.ToString() == "file:///C:/temp/newGif1.gif")
{
mediaElement.Source = new Uri("C:\\temp\\newGif.gif");
mediaElement.Play();
}
else
{
mediaElement.Source = new Uri("C:\\temp\\newGif1.gif");
mediaElement.Play();
}
}
The only way is to reset the source to start the gif from the beginning, but you need a copy of your gif, because if it is the same source the MediaElement won´t update the source. In generell when you set the source for a MediaElement you have to set the fullpath. In XAML you can choose the gif from anywhere on your pc, it does not need to be set as a resource in your project.
But setting the source in the normal code, requires the gif to bet set as a resource in the project. So normally it is not temp like in the example but rather something like
C:\Users\UnknownUser\Documents\Visual Studio 2015\Projects\RandomTestProject\Ressources
.
If you dont rely on using gifs i suggest you to use *.mp4, because it is easier to handle and works the expected way and you can convert your *.gif easy to *.mp4.
来源:https://stackoverflow.com/questions/45033553/c-sharp-gif-medialement-how-to-change-position