1

当我说我是初学者时,我真的很新鲜,你可以提前道歉!

我想做的是在一个表单中有两个图片框和一个按钮。当我单击按钮时,它会从我的图像文件夹中选择 2 张新图像,总共会执行 5 次

我如何解决这个问题的任何想法 - 这是我到目前为止点击按钮的内容

 private void button1_Click(object sender, EventArgs e)
    {
      pictureBox1.Load(@"C:\Resources\PICT01.JPG");
      pictureBox2.Load(@"C:\Resources\PICT02.JPG");
}

任何答案都必须是基本的,因为我只是在学习!提前致谢

4

3 回答 3

1

您需要创建一个全局 int 来跟踪您切换图片的频率,并在您的 button1_click 中处理该数字。

我自己不是专家,但我会这样做。开关在这里是理想的,因为您需要检查 5 种不同的可能性。

//global int
int count = 0;

private void button1_Click(object sender, EventArgs e)
{
  count++;
  switch(count)
  {
     case 1:
       //load image 1 and 2
       break;
     case 2:
       //load image 3 and 4
       break;
     case 3:
       //load image 5 and 6
       break;
     case 4:
       //load image 7 and 8
       break;
     case 5:
       //load image 9 and 10
       break;
     default: 
       break;
  }
}
于 2012-11-21T21:36:30.477 回答
0

这个示例程序将在 MyPictures 文件夹中创建一个所有 jpg 文件的列表,并从列表中随机选择图片,当列表为空时会引发异常。

public Form1()
{
    // Reading pictures from My Pictures:
    string path = Environment.GetFolderPath(Environment.SpecialFolders.MyPictures);
    DictionaryInfo myPictures = new DictionaryInfo(path);
    FPictureFiles = myPictures.GetFiles("*.jpg", SearchOption.AllDirectories).ToList();
}
private List<FileInfo> FPictureFiles;
private Random FRandom = new Random();

private void button1_Click(object sender)
{
    pictureBox1.Load(PickFile());
    pictureBox2.Load(PickFile());
}

private string PickFile()
{
    if (FPictureFiles.Count == 0) throw new Exception("No more picture files");

    int index = FRandom.Next(FPictureFiles.Count);
    string filename = FPictureFiles[index].FullName;
    FPictureFiles.RemoveAt(index);
    return filename;
}

我希望这对你的追求有所帮助。

于 2012-11-21T21:57:03.363 回答
0

您可以尝试创建一个新List<string>文件,其中列出了所有具有您选择的特定扩展名的文件。Random然后,考虑到范围不会超过我们的值,初始化一个新类,该类在特定范围内获取随机数List<string>.Count

例子

假设CurrentClicks标识一个新整数并且MaximumClicks是我们的最大值

public Form1()
{
    InitializeComponent();
    button1.Click += new EventHandler(button1_Click); //Link the Click event of button1 to button1_Click
}

const int MaximumClicks = 5; //Initialize a new constant int of value 5 named MaximumClicks
int CurrentClicks = 0; //Initialize a new variable of name CurrentClicks as an int of value 0

以下可能适用

private void button1_Click(object sender, EventArgs e)
{
    if (CurrentClicks < MaximumClicks) //Continue if CurrentClicks is less than 5
    {
        string FilesPath = @"D:\Resources\International"; //Replace this with the targeted folder
        string FileExtensions = "*.png"; //Applies only to .png file extensions. Replace this with your desired file extension (jpg/bmp/gif/etc)
        List<string> ItemsInFolder = new List<string>(); //Initialize a new Generic Collection of name ItemsInFolder as a new List<string>
        foreach (string ImageLocation in Directory.GetFiles(FilesPath, FileExtensions)) //Get all files in FilesPath creating a new string of name ImageLocation considering that FilesPath returns a directory matching FileExtensions considering that FileExtensions returns a valid file extension within the targeted folder
        {
            ItemsInFolder.Add(ImageLocation); //Add ImageLocation to ItemsInFolder
        }
        Random Rnd = new Random(); //Initialize a new Random class of name Rnd
        int ImageIndex = Rnd.Next(0, ItemsInFolder.Count); //Initialize a new variable of name ImageIndex as a random number from 0 to the items retrieved count
        pictureBox1.Load(ItemsInFolder[ImageIndex]); //Load the random image based on ImageIndex as ImageIndex belongs to a random index in ItemsInFolder
        CurrentClicks++; //Increment CurrentClicks by 1
    }
    else
    {
        //Do Something (MaximumClicks reached)
    }
}

这将在提供的文件夹中获取与指定扩展名匹配的文件位置。PictureBox然后,从收集到name的文件位置加载一个随机文件pictureBox1

但是,这不会避免之前添加到PictureBox. 如果您想避免重复,您可以创建一个新int数组或List<int>添加所有ImageIndex应用到的数组,PictureBox但建议使用它,List<int>因为它似乎更易于管理

例子

List<int> AlreadyAdded = new List<int>();
redo:
    Random Rnd = new Random();
    int ImageIndex = Rnd.Next(0, ItemsInFolder.Count);
    if (AlreadyAdded.Contains(ImageIndex)) //Continue within this block if the item already exists in AlreadyAdded
    {
        goto redo;
    }                
    AlreadyAdded.Add(ImageIndex); //Add ImageIndex to AlreadyAdded

谢谢,
我希望你觉得这有帮助:)

于 2012-11-21T21:58:02.447 回答