Csharp_crack
Csharp_crack

Reputation: 246

how to provide imagesource for an image in windows phone code behind(c#)

This is not working while binding the image dynamically

Image Imgsource = new Image();

Imgsource.Source = new BitmapImage(new Uri("/Finder;component/Images/Chrysanthemum.png", UriKind.RelativeOrAbsolute));

thanks in advance

Upvotes: 0

Views: 863

Answers (4)

Jignesh.Raj
Jignesh.Raj

Reputation: 5987

you need to bind through image converter :

 public class ImageConverter : IValueConverter
 {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
           MemoryStream memStream = new MemoryStream((byte[])value,false);
           BitmapImage empImage = new BitmapImage();
           empImage.SetSource(memStream);
           return empImage; 
      }
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
           throw new NotImplementedException();
      }
 }

Upvotes: 1

Manic
Manic

Reputation: 223

Please try this...

  public void setimagebackgroud(string uri)
    {
        ImageBrush imageBrush = new ImageBrush();
        Image image = new Image();
        image.Source = new BitmapImage(new Uri(uri,UriKind.RelativeOrAbsolute));
        imageBrush.ImageSource = image.Source;
    }

Upvotes: 1

Prasad Pathak
Prasad Pathak

Reputation: 43

 The binding as it should be done

 string s = "Hello";

 //Create the binding description   
 Binding b = new Binding("");   
 b.Mode = BindingMode.OneTime;   
 b.Source = s;

 //Attach the binding to the target  
 MyText.SetBinding(TextBlock.TextProperty, b);

 See if this helps

Upvotes: -2

anderZubi
anderZubi

Reputation: 6424

Your code creates an Image element. But then you need to add that element to a container in the page. For example to the LayoutRoot Grid:

Image Imgsource = new Image();
Imgsource.Source = new BitmapImage(new Uri("/Finder;component/Images/Chrysanthemum.png", UriKind.RelativeOrAbsolute));
this.LayoutRoot.Children.Add(Imgsource);

Upvotes: 1

Related Questions