Binding Image Control to URI in WP7 - silverlight

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));

Related

What iis wrong with this imagesource in WPF?

I try to set an imagesource in code behind in WPF. For some reason it doesn't work - am I missing something?
ImageBrush imgBrushBackground = new ImageBrush();
imgBrushBackground.ImageSource = new BitmapImage(new Uri(#"\Assets\img\bg_spaceFlare.jpg"));
The link to the file is correct (I checked it by dragging in an image from Solution Explorer).
The error message is this: System.UriFormatException: 'Invalid URI: The format of the URI could not be determined.'
Thanks!
Petter
You can fix this by passing UriKind.Relative to Uri's constructor:
new BitmapImage(new Uri(#"\Assets\img\bg_spaceFlare.jpg", UriKind.Relative));

How to bind a image in Label in WPF using pure code?

I am new to WPF, here I am making a jigsaw game but encountering a problem: I just don't know how to let a image show in a Label in pure code (in XAML, I can do it.), I have searched google a lot but seems that no releated articles regarding this topic. Anyone can help me on this? thx.
My Code Sample:
Label lbl = new Label();
lbl.Width = 120;
lbl.Height = 120;
lbl.Background = new SolidColorBrush(Colors.YellowGreen);
BitmapImage myImageSource = new BitmapImage();
myImageSource.BeginInit();
myImageSource.UriSource = new Uri("Images/test.png",UriKind.Relative);
myImageSource.EndInit();
lbl.Background.SetValue(ImageBrush.ImageSourceProperty, myImageSource);
myGridContainer.Children.Add(lbl);
well, this code sample can't work. why ? any comment or answer from you will be appreciated , thx.
You can use BindingOperations.SetBinding to data-bind a DependencyProperty to its source.
See also: How can I create a tiled image in code that is also data-bound?

Cannot seek media element in silverlight

This is the situation.
I have a MainPage.Xaml, within this MainPage.Xaml I have embedded a UserControl (VideoWidget.Xaml) (LayouRoot.Children).
VideoWidget.Xaml has a mediaElement embedded in its LayoutRoot.
I'm trying to Pause/Play the MediaElement (mediaElement1) from MainPage.Xaml but does not respond.
e.g.
public MainPage()
{
// Required to initialize variables
InitializeComponent();
Video testVideo = new Video();
VideoWidget video = new VideoWidget(testVideo);
videoWidget.Height = 246;
videoWidget.Width = 290;
LayoutRoot.Children.Add(video);
video.mediaElement1.play();
}
The All methods for mediaElement are not responsive.
Help please.
Mohit
Try putting test control buttons inside the VideoWidget itself. Does that work? If so, there may be a problem with the way you are exposing the mediaElement in the VideoWidget as a public member.

Display GIF in a WP7 application with 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.

How to set uri for local image in silverlight app?

In SL class library MyLib, I have a image say my.png. Then I want to use it in code behind I tried following way:
StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("/MyLib;component/my.png", UriKind.Relative));
BitmapImage image = new BitmapImage();
image.SetSource(resourceStream.Stream);
this.MyIcon.Source = image;
But it's not woking. I think it's the Uri not set correctly. Help please.
This works:-
BitmapImage image = new BitmapImage(new Uri("/MyLib;component/my.png", UriKind.Relative));
MyIcon.Source = image;
I can't see why you would want to use a Stream here. Having said that your Stream code should work. The build action on the png should be "Resource" and "MyLib" in your Uri should be the Assembly name of the library as found on the "Silverlight" tab of the project properties.
Do you have your image marked as "Resource" in the properties window, or "Content"?
You could always set a style as a resource in your application and then call it like:
Application.Current.Resources["myCoolStyle"] and apply that to the image.

Resources