You specify UriKind.Relative while you should use UrlKind.Absolute
Since you are probably loading the full URL from the database, e.g.
http://www.americanlayout.com/wp/wp-content/uploads/2012/08/C-To-Go-300x300.png
While UriKind.Relative will be used for something like
/wp/wp-content/uploads/2012/08/C-To-Go-300x300.png
In any case, the following code works:
var image = new Image(); var fullFilePath = @"http://www.americanlayout.com/wp/wp-content/uploads/2012/08/C-To-Go-300x300.png"; BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(fullFilePath, UriKind.Absolute); bitmap.EndInit(); image.Source = bitmap; wrapPanel1.Children.Add(image);
No need to set image .Width and Image.Height to Double.Nan
Side note. Although you can certainly load images at runtime like this, it would be better to use WPF Databinding (preferably with something like MVVM)
Basically, you will have a ListBox with a WrapPanel as ItemsPanelTemplate Then set the ItemsSource to your list (lstQuestions).
<ListBox ItemsSource={Binding lstQuestions}> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <Image Source="{Binding Path, Converter={StaticResource MyPathConverter}}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
You would snap the image to any property that represents the Path and use ValueConverter to normalize the path.
public class PathConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string path = value.ToString(); if (path.StartsWith("\\") path = path.Substring(1); return Path.Combine("whateveryourbasepathis", path); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
Code is just a way to give you an idea of which direction to go. The fact is that you may need to look for WPF data binding, rather than doing this with code.