2016-09-03 16 views
2

同時に4つの画像を表示したいと思います。現在、画像は異なる数字で表示されます。たとえば、1画像が表示されたり、2画像などが表示されます。また、重複が表示されないようにしたい場合もあります。 Form1_Loadから複数の画像を複数の画像にループする

コード:現在、何が起こるかのように上に与えられた

PictureBox[] boxes = new PictureBox[4]; 

boxes[0] = pictureBox0; 
boxes[1] = pictureBox1; 
boxes[2] = pictureBox2; 
boxes[3] = pictureBox3; 

for (int i = 0; i < boxes.Length; i++) 
{ 
    int switcher = r.Next(0, 5); 

    switch (switcher) 
    { 
     case 0: 
      { boxes[i].Image = Properties.Resources.dog0; } break; 
     case 1: 
      { boxes[i].Image = Properties.Resources.dog1; } break; 
     case 2: 
      { boxes[i].Image = Properties.Resources.dog2; } break; 
     case 3: 
      { boxes[i].Image = Properties.Resources.dog3; } break; 
    } 
} 

enter image description here 二つの例。

アップデート - ワーキング

プログラムは、現在の負荷時の周りの画像を移動し、重複しない:)

List<Bitmap> resources = new List<Bitmap>(); 
resources.Add(Properties.Resources.dog0); 
resources.Add(Properties.Resources.dog1); 
resources.Add(Properties.Resources.dog2); 
resources.Add(Properties.Resources.dog3); 

resources = resources.OrderBy(a => Guid.NewGuid()).ToList(); 

for (int i = 0; i < resources.Count; i++) 
{ 
    pictureBox0.Image = resources[0]; 
    pictureBox1.Image = resources[1]; 
    pictureBox2.Image = resources[2]; 
    pictureBox3.Image = resources[3]; 
} 

enter image description here enter image description here

二つ今何が起こるかを示す上記の例それは動作する。 M.kazem Ahkharyとして

+1

画像の配列を作成し、それをシャッフルします。その配列を反復処理します。 http://stackoverflow.com/a/26931594/4767498 –

+1

もう一度[答え](http://stackoverflow.com/a/39310462/6290553):) –

+0

あなたのアップデートのforループは絶対に必要ではありません。これは、同じリソースをピクチャボックスに4回指すだけです。 –

答えて

1

実装は非常に簡単です。まず、配列をシャッフルしてからそれを反復処理する必要があります。 Fisher–Yates shuffle

として以下の方法ShuffleImagesを作成します。

public void ShuffleImages(PictureBox[] img) 
{ 
    Random r = new Random(); 
    for (int i = 0; i < img.Length - 1; i++) 
    { 
     int j = r.Next(i, img.Length); 
     PictureBox temp = img[j]; 
     img[j] = img[i]; 
     img[i] = temp;     
    } 
} 

をし、あなたのForm1_Loadイベントでメソッドを呼び出します。

private void Form1_Load(object sender, EventArgs e) 
{ 
    PictureBox[] boxes = new PictureBox[4]; 
    boxes[0] = pictureBox0; 
    boxes[1] = pictureBox1; 
    boxes[2] = pictureBox2; 
    boxes[3] = pictureBox3; 

    ShuffleImages(boxes); //call the method 

    for (int i = 0; i <= 3; i++) 
    { 
     switch (i) 
     { 
      case 0: 
       { boxes[i].Image = Properties.Resources.dog0; } 
       break; 
      case 1: 
       { boxes[i].Image = Properties.Resources.dog1; } 
       break; 
      case 2: 
       { boxes[i].Image = Properties.Resources.dog2; } 
       break; 
      case 3: 
       { boxes[i].Image = Properties.Resources.dog3; } 
       break; 
     } 
    } 
} 

shuffled_imgs

1

あなたは、画像をシャッフルする必要が指摘:

List<Bitmap> resources = new List<Bitmap>(); 
resources.Add(Properties.Resources.dog0); 
resources.Add(Properties.Resources.dog1); 
resources.Add(Properties.Resources.dog2); 
resources.Add(Properties.Resources.dog3); 

resources = resources.OrderBy(a => Guid.NewGuid()).ToList(); // Dirty but effective shuffle method 

pictureBox0.Image = resources[0]; 
pictureBox1.Image = resources[1]; 
pictureBox2.Image = resources[2]; 
pictureBox3.Image = resources[3]; 
+0

ありがとう、どうすれば実装できますか? 何か置き換えてもよろしいですか? – Boats

+0

いくつか編集しましたが、完全には正しくありませんでした。あなたは通常、あなたの提供されたコードの上にこのコードを貼り付けることができます –

+0

ビットマップを文字列に変換できないという "resources.Add"のそれぞれから私にエラーが1つあります。 IOrderedEnumerable 'を' System.Collections.Generic.List 'に変更します。明示的な変換が存在します(キャストがありませんか?) "リソース"は現在のコンテキストに存在しません。 – Boats