文件夹中的多个图像

文件夹中的多个图像

本文介绍了文件夹中的多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我对C#和WPF相当新,所以我无法弄清楚如何做到这一点。我有一个表格应该在一个表格中显示151张图片(所有口袋妖怪第1代精灵)。我现在的方式是它显示相同的图像151次而不是所有图像一次。我为此写的代码如下:

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've done it now is that it shows the same image 151 times instead of all images just once. The code I wrote for this is as follow:

    public partial class PokeGame : Window
{
    BitmapImage carBitmap = new BitmapImage(new Uri("pack://application:,,,/Images/All_Sprites/001.png", UriKind.Absolute));

    {

        InitializeComponent();

        int imgCount = 151;
        int left = 0;
        int top = 0;
        List<Image> imageList = new List<Image>();
        for (int i = 0; i < imgCount; i++)
        {
            if(i % 10 == 0)
            {
                if (i != 0)
                {
                    top += 175;
                    left = 0;
                } else
                {
                    top = 0;
                    left = 0;
                }
            }

            Image img_ding = new Image();
            img_ding.Source = carBitmap;
            img_ding.Height = 150;
            img_ding.Width = 150;
            img_ding.Margin = new Thickness(left, top ,0 ,0);
            imageList.Add(img_ding);
            left += 175;
        }

        int j = 0;

        foreach (Image img in imageList)
        {
            imageCanvas.Children.Add(img);
            j++;
        }

    }

正如你所看到的那样,很可能很多在我的代码中有改进的余地。但是,我的问题是:我如何才能使它不会显示相同的图像151次,而是所有图像(sprite001.png,sprite002.png,sprite003.png等)?

As you can see there's probably lots of room for improvement in my code. However, my question is: How can I make it so that it doesn't display the same image 151 times but all images (sprite001.png, sprite002.png, sprite003.png, etc.)?

推荐答案

您需要在循环内定义位图,而不是在外部。然后每次迭代都会创建一个带有新路径的新位图。

You need to define your bitmap inside the loop, not outside. Then each iteration will create a new bitmap with the new path.

所以类似于:

for (int i = 0; i < imgCount; i++)
{
   // padding left will give you 001 and 010 and 151
   string img = i.ToString().PadLeft(3, '0');
   BitmapImage carBitmap = new BitmapImage(new Uri("pack://application:,,,/Images/All_Sprites/" + img+".png", UriKind.Absolute));
   // the rest of your code
}

这篇关于文件夹中的多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:37