Display GIF in a WP7 application with Silverlight - silverlight

I would like to display gif in my WP7 application.
Is there some way to achieve this ?
I've tryed this one http://imagetools.codeplex.com/ but can't make it working with WP7.
Thanks in advance for any help

In fact,
it's working, but it lacks some documentation.
After some troubles, here's how to use it :
reference ImageTools
reference ImageTools.Controls
reference ImageTools.IO.Gif
Add namespace in xaml :
xmlns:imagetools="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
And resources :
<phone:PhoneApplicationPage.Resources>
<imagetools:ImageConverter x:Key="ImageConverter" />
</phone:PhoneApplicationPage.Resources>
Then use the control with the converter :
<imagetools:AnimatedImage Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" />
Your ImageSource should be an Uri, for example :
ImageSource = new Uri("http://mysite/my.gif", UriKind.Absolute);
Don't forget to add decoded :
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();

Check out Jamie Rodriguez's post here on using GIFs with WP7. He uses the ImageTools project from CodePlex.
http://blogs.msdn.com/b/jaimer/archive/2010/11/23/working-with-gif-images-in-windows-phone.aspx

I struggled to get the accepted answer working. The following solution worked for me to display a static gif.
public ImageResponse(string imageUrl)
{
InitializeComponent();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
var imageResponse = new ExtendedImage();
imageResponse.UriSource = new Uri(imageUrl);
imageResponse.LoadingCompleted += this.ImageResponseLoadingCompleted;
}
private void ImageResponseLoadingCompleted(object sender, EventArgs e)
{
var imageResponse = (ExtendedImage)sender;
Classes.Util.UiThread.Invoke(() =>
{
this.ImageResponse.Source = imageResponse.ToBitmap();
});
}
Classes.Util.UiThread is a helper class I use to call the UI Thread
this.ImageResponse is a standard image control

Is it an animated GIF? If not, I would try converting the GIF to another supported file format before using it in your app.

WP7 Silverlight supports JPG/PNG.

As per http://msdn.microsoft.com/en-us/library/ff462087(VS.92).aspx the Silverlight image control does not support GIF files.
By using ImageTools you are converting the GIF file to something else on the fly on the device. If you are using gif files that you have control of (i.e. You are bundling them in the XAP or they are coming from your webserver.) you should use converted versions of these files.
This will mean that the app has to do less.
The knock on effect is that:
1. You will have to write less code.
2. The app will have to do less work and so will perform slightly better.
Of course, this doesn't cover animated GIFs. For these you'll need to use a different approach.

Related

Reload a image using ReactiveUI and WPF

I am trying to change a image in the UI running with WPF on runtime by changing the source of the image. Unfortunately the image is not changing. I found examples of using a Bitmap image to change the Image source, but with ReactiveUI I am not able to change the UriSource for the BitmapImage.
XAML code:
<Image x:Name="button_image0" DockPanel.Dock="Top" Source="someimage.jpg" Stretch="Uniform" />
view code:
this.OneWayBind(ViewModel, vm => vm.button_image0, v => v.button_image0).DisposeWith(d);
Viewmodel code:
[Reactive] public string button_image0 { get; set; }
//button pressed
button_image0 = "newimage.jpg";
I checked that the source of the image is changing, but the image displayed is not. Does someone know how to solve this with reactiveUI or knows a way around?
Solution
If anyone is having the same problem, it's a syntax error of your code. When changing the source of an image using reactiveUI the image is reloaded. This question and answer confused me Reloading an image in wpf
When binding you should include the control property as part of the expression. For example
ContentControl.Content
SizableControl.Width
Image.Source
TextBlock.Text
So if you bind to the "Source" property on the Image on the view. You also appear to have your notation of v and vm the wrong way around which might be adding to the confusion. The viewmodel is the 2nd expression, the view is the 3rd.
this.OneWayBind(ViewModel, vm => vm.button_image0, vw => vw.button_image0.Source).DisposeWith(d);

Creating a mailto: hyperlink in Silverlight Out Of Browser app

Is there a way to create a mailto: hyperlink in a Silverlight 4 OOB app? Thanks!
Edit: based on some of the discussion, an acceptable answer would be a different way than using the HyperlinkButton, or a way to use the HyperlinkButton without having the extra popup in IE.
Ideally it would have been nice of you to post some code, as I have no idea whether the email address is known/determined at design-time or run-time, but nonetheless:
In XAML:
<HyperlinkButton x:Name="mailButton" NavigateUri="mailto:somedude#example.com" TargetName="_blank"></HyperlinkButton>
In C#:
HyperlinkButton hbtn = new HyperlinkButton();
hbtn.Name = "mailButton";
hbtn.TargetName = "_blank";
hbtn.NavigateUri = new Uri("mailto:somedude#example.com");
parent.Controls.Add(hbtn);
In a situation in which you don't know the email address at design time, it's relatively straightforward to assign the value of the NavigateUri property within a method.
I posted a solution to this issue on CodeProject
http://www.codeproject.com/Answers/383879/Silverlight-mailto-HyperlinkButton-always-opens-an#answer2
Essentially, instead of using the default behaviour add a click event and then call javascript location.href. This stops the extra browser window opening.
private void TestLink_Click(object sender, RoutedEventArgs e)
{
//Only run the click event if in browser because this will not work OOB
if (Application.Current.IsRunningOutOfBrowser)
return;
var cmd = String.Format("location.href='mailto:test#test.com?subject=blah&body=something';",
HtmlPage.Window.Eval(cmd);
}

How can I specify MediaElement.Source with non-relative uri?

We have some WPF modules which are hosted in a cpp unmanaged/managed application. This enviroment is causing troubles when specifying relative Uri's for media content. For example, I have no problem of doing something like this in a testapp:
<MediaElement Grid.Row="0" x:Name="player" Source="media\Getting started with.wmv" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill"
MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>
But, as mentioned, this does not work in production code.
If I try to use the pack schema like this:
Source="pack://application:,,,/media/Getting started with.wmv"
I get the exception:
Cannot navigate to application resource 'pack://application:,,,/media/Getting started with.wmw' by using a WebBrowser control. For URI navigation, the resource must be at the application�s site of origin. Use the pack://siteoforigin:,,,/ prefix to avoid hard-coding the URI.
If I try to use the 'siteoforigin' schema like this:
Source="pack://siteoforigin:,,,/media/Getting started with Olga 7.wmv"
I get another error:
Application identity is not set.
The media file is set up as "Content" and with "copy always".
How can I specify the MediaElement source using an absolute uri in a Wpf desktop application?
I found a solution (kinda). I am guessing that the relative url's can not be resolved because the main .exe is an cpp mfc application. So in order to create absolute uri's I did something like this:
player.Source = new Uri(CreateAbsolutePathTo("media/Getting started with.wmv"), UriKind.Absolute);
private static string CreateAbsolutePathTo(string mediaFile)
{
return Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, mediaFile);
}
I am binding to a viewmodel so this logic is wrapped into a property on the viewmodel and the source is databound in xaml.
Its working, but its not as pretty as I would like it to be.
Only you need to do this:
MediaElement bb = new MediaElement();
stage.Children.Add(bb);
bb.Source = new Uri("Recursos/MagicWandNoise.wav", UriKind.Relative);
Debug.WriteLine("URL:" + bb.Source);
bb.LoadedBehavior = MediaState.Manual;
bb.Play();
And then add the Binary Resources in your folder debug, check this link
Remember the media element to work fine, you need to add in a Visual Three
Canvas.Children.Add(bb);

Binding Image Control to URI in WP7

I ham developing an application for WP7 and i am having a strange problem with Image control, it does not show the image it is binded to.
I have tryed it in so many ways but event this way (the most naive one) does not work:
In the xaml I have an Image control named img and this is what I have done in the code behind
public MainPage()
{
InitializeComponent();
img.Source = new BitmapImage(new Uri (#"http://img11.imageshack.us/img11/5365/photovnj.jpg", UriKind.Absolute));
}
</code>
It seems to simple not to work...
Please help!
This code works fine for me presuming you have img declared as an Image in your xaml.
If not, I'd suggest you have a connection problem from your app running in the emulator or device to the internet.
This is the code I wrote.
image1.Source = new BitmapImage(new Uri(#"http://img11.imageshack.us/img11/5365/photovnj.jpg", UriKind.Absolute));

Using Image Control in Silverlight (4)

I'm trying to use the Image control is a very basic way, like here:
http://www.silverlightshow.net/items/Using-the-Image-control-in-Silverlight-2-Beta-1.aspx
So I'm ending up with XAML like this:
<Image x:Name="imgSmall" Stretch="Fill" Source="../Img/Small/105.jpg" Margin="10,0,0,0"></Image>
Which isn't working. The Image is blank, and in the designer the URI is underlined with a message of "...is not part of the project or its build action is not set to 'Resource"
If I change the source to a property on my ViewModel, set like this:
new Uri(App.Current.Host.Source, "../Img/Small/105.jpg");
Then it works fine. I'd much prefer to use the simpler syntax and get the image directly. Is this possible?
(The images are one level up from ClientBin)
Setting all of my web sites images to build=Resource is not possible.
Thanks!
You have to create a converter that takes the relative image path and adds the "absolute" part. You can pass the relative Uri as binding value or as converterParameter.
class ImageConverter : IValueConverter
{
// method convert()
return new BitmapImage(new Uri(App.Current.Host.Source, ((string)parameter));
//...
}
It id doesn't work because image is not added to your project.
Add image to project and in then you can set source from xaml.

Resources