两个图片框,每个按钮点击循环图像

时间:2012-11-21 21:07:33

标签: c# .net picturebox

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

我要做的是在表格中有两个图片框和一个按钮。当我点击按钮时,它从我的图像文件夹中选择了2个新图像,它将共执行5次

任何想法我如何处理这个 - 这是我到目前为止按钮点击

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

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

3 个答案:

答案 0 :(得分: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;
  }
}

答案 1 :(得分: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;
}

我希望这能帮助你完成任务。

答案 2 :(得分: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名称pictureBox1的文件位置中加载随机文件。

但这不会避免先前添加到PictureBox的文件。如果您想避免重复,可以创建一个新的int数组或List<int>并添加应用于ImageIndex的每个PictureBox,但建议您使用List<int> {1}}因为它似乎更容易管理

示例

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

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