0

I have some code of c# and i want to add some images in c# code via url but it is not happen to add so please tell me what is wrong in this code. My project name is ProjectDemo and i have folder images that have maintain all images.

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        collection = new ObservableCollection<Image> { 
            new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/highway.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/highway-1.png", UriKind.Relative))}, 
            new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo/images/part-5.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/part-6.png", UriKind.Relative))} ,
             new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/part-7.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/part-8.png", UriKind.Relative))} ,
             new Image{ Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/speed-1.png", UriKind.Relative))} ,
            new Image{Height=128, Width=128,Source=new BitmapImage(new Uri("/ProjectDemo;component/images/speedroad.png", UriKind.Relative))} 
         };
         FirstListBox.Items.Add(collection[0]);
         FirstListBox.Items.Add(collection[1]);
         FirstListBox.Items.Add(collection[2]);
         FirstListBox.Items.Add(collection[3]);
         FirstListBox.Items.Add(collection[4]);
         FirstListBox.Items.Add(collection[5]);
         FirstListBox.Items.Add(collection[6]);
         FirstListBox.Items.Add(collection[7]);

    }

2 Answers 2

1

Just a friendly advice, learn MVVM before writing single line of code in wpf.

First make sure that you have Images included in the project and their BuildAction is "Resource"

Then in your VM or code behind you can do

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            InitializeImages();
            DataContext = this;

        }

        private void InitializeImages()
        {
            ImageModels.Add(new ImageModel
            {
                Source = new BitmapImage(new Uri("/ProjectDemo;component/images/highway.png", UriKind.Relative))
            });
            ImageModels.Add(new ImageModel
            {
                Source = new BitmapImage(new Uri("/ProjectDemo;component/images/highway-1.png", UriKind.Relative))
            });
            ImageModels.Add(new ImageModel
            {
                Source = new BitmapImage(new Uri("/ProjectDemo;component/images/part-5.png", UriKind.Relative))
            });
        }


        ObservableCollection<ImageModel> _imageModels = new ObservableCollection<ImageModel>();

        ObservableCollection<ImageModel> ImageModels
        {
            get { return _imageModels; }
        }
    }

    public class ImageModel : INotifyPropertyChanged
    {
        private ImageSource imageSource;

        public ImageSource Source
        {
            get { return imageSource; }
            set
            {
                imageSource = value;
                RaisePropertyChanged("Source");
            }
        }

        private void RaisePropertyChanged(string propName)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(propName));
            }
        }


    public event PropertyChangedEventHandler PropertyChanged;
    }

and in your View

  <ListBox ItemsSource="{Binding ImageModels}">
       <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Height="128" Width="128" Source="{Binding Source}"></Image>
            </DataTemplate>
       </ListBox.ItemTemplate>
   </ListBox>
Sign up to request clarification or add additional context in comments.

5 Comments

I'd recommend to write absolute Pack URIs, e.g. new Uri("pack://application:,,,/ProjectDemo;component/images/highway.png") instead of new Uri("/ProjectDemo;component/images/highway.png", UriKind.Relative).
@Clemens I second that
Moreover there is no strict need for having BitmapImages in the view model. You could as well have URI strings, as WPF provides built-in automatic conversion from string (and Uri) to ImageSource.
@Clemens I think that only holds true when we set string directly in xaml. If we bind it to string property it does not convert it to Imagesource
That's not true. WPF automatically converts strings or Uris to ImageSource, regardless if you write it directly in XAML or if you have a binding to a property. Just give it a try.
0

Could you try this :

Source = new BitmapImage(new Uri(@"pack://application:,,,/ProjectDemo;component/images/highway.png"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.