2014-01-31 44 views
0

您好,我写了一个程序,显示文件夹中的任何图片,.exe所在。 它包含一个按钮,它随机选择另一张照片,并显示。 迄今为止工作。生成随机数字,不会再显示,未知数字池

接下来我想做的是,该按钮选择一个图片,这是以前没有选择。只有在显示每张照片时,程序才会重置并重新开始显示所有照片。

但是,当我现在启动程序,它显示一张图片,就这样了。按钮没有做任何可见的事情,你不能改变第一张照片 - 我不知道为什么。 (我看到了类似的问题,就像二十一点主题一样,但在我的情况下,我不知道会有多少图片,所以我不能创建一个随机数字的修复池并将它们从该列表或类似的东西)。

因此,这里是我的代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Reflection; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace randompix 
{ 
    public partial class Form1 : Form 
    { 

     int i = 0; 
     Random rnd = new Random(); 
     string pfad = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 
     IEnumerable<string> allebilder; // Will contain every filename of the folder 
     List<int> wdhlist = new List<int>(); //will contain 'i' that was already used 



     public Form1() 
     { 
      InitializeComponent(); 
      this.WindowState = FormWindowState.Maximized; 
      pBox1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top); 
      pBox1.SizeMode = PictureBoxSizeMode.Zoom; 
      btn2.Anchor = (AnchorStyles.Top | AnchorStyles.Right); 

      allebilder = Directory.EnumerateFiles(pfad); 
      wdhlist.Add(i); 

      if (allebilder.ElementAt<string>(i).EndsWith(".jpg") || allebilder.ElementAt<string>(i).EndsWith(".jpeg") || allebilder.ElementAt<string>(i).EndsWith(".png") || allebilder.ElementAt<string>(i).EndsWith(".gif") || allebilder.ElementAt<string>(i).EndsWith(".bmp")) 
      { 
       pBox1.ImageLocation = allebilder.ElementAt<string>(i); 
       label1.Text = allebilder.ElementAt<string>(i); 
      } 

      else 
      { 
       if (i == 0) // because 0 is already added 
       { 
        i = rnd.Next(allebilder.Count<string>()); 
       } 
       else 
       { 
        wdhlist.Add(i); 
        i = rnd.Next(allebilder.Count<string>()); 
       } 
      } 
     } 

     private void btn1_Click(object sender, EventArgs e) 
     { 
      i = rnd.Next(allebilder.Count<string>()); 
      wdhlist.Add(i); 

      if (wdhlist.Count() >= allebilder.Count<string>()) 
      { 
       wdhlist.Clear(); 
      } 

      else 
      { 
       if (wdhlist.Contains(i)) 
       { 
        i = rnd.Next(allebilder.Count<string>()); 
        wdhlist.Add(i); 
       } 

       else 
       { 
        if (allebilder.ElementAt<string>(i).EndsWith(".jpg") || allebilder.ElementAt<string>(i).EndsWith(".jpeg") || allebilder.ElementAt<string>(i).EndsWith(".png") || allebilder.ElementAt<string>(i).EndsWith(".gif") || allebilder.ElementAt<string>(i).EndsWith(".bmp")) 
        { 
         if (allebilder.ElementAt<string>(i) == null) 
         { 
          MessageBox.Show("Nope"); 
         } 

         else 
         { 
          label1.Text = allebilder.ElementAt<string>(i); 
          pBox1.ImageLocation = allebilder.ElementAt<string>(i); 
         } 
        } 
        else 
        { 
         i = rnd.Next(allebilder.Count<string>()); 
         wdhlist.Add(i); 
        } 
       } 
      } 
     } 

     private void btn1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Enter) 
      { 
       btn1_Click(sender, e); 
      } 
     } 

     private void btn2_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show("Version 0.3\n\nUse: Copy .exe in a folder with pictures\nSupports .jpg, .gif, .png and .bmp\n\nProgram by Fabian Schmidt\nIcon by Aleksandra Wolska\n(https://www.iconfinder.com/iconsets/49handdrawing)"); 
      btn1.Focus(); 
     } 
    } 
} 

回答

1

你需要形成的图像列表的文件名。然后形成索引列表 - 从0到列表中图像的数量。

List<int> numberList = Enumerable.Range(0, imagesList.Count).ToList(); 

然后你从0获得随机数到numberList.Count

int randomValue = numberList[rnd.Next(0, numberList.Count)]; 

你使用后,从numberList

删除
DoSomeThingWithImage(imagesList[randomValue]); 
numberList.Remove(randomValue); 

所以你必须非重复随机图像,直到它们都不出现。然后再做一遍。 希望有所帮助。

0

您可以创建按随机顺序的图像序列,然后在列表中重新创建充分利用序列

private Random rnd = new Random(); 
private IEnumerable<string> GetImagesInRandomOrder(){ 
    return Directory.EnumerateFiles(pfad).OrderBy(x => rnd.Next()); 
} 

private IEnumerator<string> randomImages = 
      Enumerable.Empty<string>.GetEnumerator(); 

private string GetNextImage(){ 
     if(!randomImages.MoveNext()){ 
      randomImages = GetImagesInRandomOrder().GetEnumerator(); 
      if(!randomImages.MoveNext()){ 
       throw new InvalidOperationException("No images"); 
      } 
     } 
     return randomImages.Current; 
} 

然后你会使用它像这样:

DoSomeThingWithImage(GetNextImage());