2014-01-31 45 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());