Multiple images from folder

前端 未结 3 859
深忆病人
深忆病人 2020-12-22 00:20

as I\'m fairly new to C# and WPF I just can\'t figure out how to do this. I have a form that should show 151 images (all pokemon generation 1 sprites) in a form. The way I\'

3条回答
  •  爱一瞬间的悲伤
    2020-12-22 01:11

    You create carBitmap exactly once, outside the loop, and use it every time. Create a new one for each image instead.

            Image img_ding = new Image();
            BitmapImage carBitmap = new BitmapImage(new Uri("pack://application:,,,/Images/All_Sprites/001.png", UriKind.Absolute));
            img_ding.Source = carBitmap;
    

    I assume that path ending in 001.jpg should be changing each time; no doubt you can figure that out. Is it the value of i in the for loop, stringified and left-padded with zeroes? That'd look like this:

            Image img_ding = new Image();
            var uri = String.Format("pack://application:,,,/Images/All_Sprites/{0:000}.png", i);
            //  N.B. UriKind.Absolute is redundant, sigh
            BitmapImage carBitmap = new BitmapImage(new Uri(uri, UriKind.Absolute ));
            img_ding.Source = carBitmap;
    

    Also, @Clemens is going to provide an answer that shows you how to rewrite the whole thing using an ItemsControl, which will be much nicer than this. I already wrote somebody a bunch of XAML this morning so it's his turn.

提交回复
热议问题