Jump to content

Retrieving images with for loop [C#]

nemaides
Go to solution Solved by fizzlesticks,

It should work if you just move everything into the loop

            string[] filePaths = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg", SearchOption.AllDirectories);                        foreach (string value in filePaths)            {                Image img = new Image();                BitmapImage bmpimg = new BitmapImage();                img.Width = 200;                bmpimg.DecodePixelWidth = 200;                img.Margin = new Thickness(5);                bmpimg.BeginInit();                bmpimg.UriSource = new Uri(value);                img.Source = bmpimg;                wp_imgs.Children.Add(img); // <--- error here                bmpimg.EndInit();            }

Pretty common mistake i think, but i can't get my crap to work..

 

i know that my "wp_imgs.Children.Add(img); should be outside the for loop, but then i will only return the last image found in that loop, i need all of them, which is why i tried inside the loop, i can't really find the solution i'm looking for..

 

 

I know you guys at LTT are clever as fk, so please help me with a simple solution, people on the interwebs tend to show how much crap they can do, instead of giving a simple straight forward solution.

 

Also, if you didn't notice by the title, this is written in C#, it's a WPF Application with a WrapPanel (wp_imgs).

string[] filePaths = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg", SearchOption.AllDirectories);         Image img = new Image();         BitmapImage bmpimg = new BitmapImage();         img.Width = 200;         bmpimg.DecodePixelWidth = 200;         img.Margin = new Thickness(5);         bmpimg.BeginInit();         foreach (string value in filePaths)            {             bmpimg.UriSource = new Uri(value);             img.Source = bmpimg;             wp_imgs.Children.Add(img); // <--- error here            }         bmpimg.EndInit();
Link to comment
Share on other sites

Link to post
Share on other sites

Where are you initializing and setting up wp_imgs? Is that a canvas or something?

Link to comment
Share on other sites

Link to post
Share on other sites

It should work if you just move everything into the loop

            string[] filePaths = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg", SearchOption.AllDirectories);                        foreach (string value in filePaths)            {                Image img = new Image();                BitmapImage bmpimg = new BitmapImage();                img.Width = 200;                bmpimg.DecodePixelWidth = 200;                img.Margin = new Thickness(5);                bmpimg.BeginInit();                bmpimg.UriSource = new Uri(value);                img.Source = bmpimg;                wp_imgs.Children.Add(img); // <--- error here                bmpimg.EndInit();            }

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 months later...

 

It should work if you just move everything into the loop

            string[] filePaths = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg", SearchOption.AllDirectories);                        foreach (string value in filePaths)            {                Image img = new Image();                BitmapImage bmpimg = new BitmapImage();                img.Width = 200;                bmpimg.DecodePixelWidth = 200;                img.Margin = new Thickness(5);                bmpimg.BeginInit();                bmpimg.UriSource = new Uri(value);                img.Source = bmpimg;                wp_imgs.Children.Add(img); // <--- error here                bmpimg.EndInit();            }

oh lol shit, i forgot i had posted in here since i abandoned my project for some time, but yeah with some tweaking it worked.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×