プロが教えるわが家の防犯対策術!

C#を勉強していて、指定したフォルダから画像を読み込んでピクチャボックスに次々と表示するスライドショーを作っています。
それで次のように作りました。
string[] files = System.IO.Directory.GetFiles(@"c:\test\", "*.jpg");

 foreach (string file in files)
 {
  pictureBox1.Image = Bitmap.FromFile(file);
  this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
Thread.Sleep(1000);
 }
しかしこれではフォルダの最後の画像しか表示されません。
この場合次々と画像を表示するにはどうしたらいいか教えてください。

A 回答 (2件)

using System;





namespace Q5582265

{

class Q5582265:System.Windows.Forms.Form

{



private System.Windows.Forms.PictureBox picturebox1;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;
/* System.Windows.Forms.Timerの方が好きな人いるかも */
private System.Timers.Timer timer1;

private System.Windows.Forms.TextBox textbox1;

private System.Collections.Generic.List<System.IO.FileInfo> ImagefileInfos;

Q5582265(){



this.Width = 800;

this.Height = 600;

textbox1 = new System.Windows.Forms.TextBox();



textbox1.Top = 0;

textbox1.Left = 0;

textbox1.Width = 800;



button1 = new System.Windows.Forms.Button();

button1.Top = 50 - 20;

button1.Left = 0;

button1.Width = 200;

button1.Enabled = true;

button1.Text = "Start";

button1.Click += this.button1_Click;



button2 = new System.Windows.Forms.Button();

button2.Top = 100 - 20;

button2.Left = 0;

button2.Width = 200;

button2.Enabled = false;

button2.Text = "Stop";

button2.Click += this.button2_Click;





picturebox1 = new System.Windows.Forms.PictureBox();

picturebox1.Top = 100;

picturebox1.Left = 0;

picturebox1.Width = 800;

picturebox1.Height = 500;

picturebox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;



this.Controls.Add(textbox1);

this.Controls.Add(button1);

this.Controls.Add(button2);

this.Controls.Add(picturebox1);



timer1 = new System.Timers.Timer(1000);

timer1.Enabled = false;

timer1.Elapsed += this.timer1_tick;



}



private void button1_Click(object sender,System.EventArgs e){

System.Collections.Generic.List<System.IO.FileInfo> fileInfos = new System.Collections.Generic.List<System.IO.FileInfo>(new System.IO.DirectoryInfo(textbox1.Text).GetFiles());
/* 画像だけが含まれるかどうかわからないので */
fileInfos = fileInfos.FindAll(delegate(System.IO.FileInfo fi){

bool isImage = false;

try{

new System.Drawing.Bitmap(fi.FullName);

isImage = true;

}catch(Exception){

isImage = false;

}

return isImage;

}

);
/* 俺の書き方の都合上、0個の場合は避けておきたいのだ */
if (fileInfos.Count > 0){

this.ImagefileInfos = fileInfos;

button1.Enabled = false;

button2.Enabled = true;

textbox1.Enabled = false;

timer1.Enabled = true;



}else{

this.ImagefileInfos = null;

}



}

private void button2_Click(object sender,System.EventArgs e){

button1.Enabled = true;

button2.Enabled = false;

textbox1.Enabled = true;

timer1.Enabled = false;

}

private void timer1_tick(object source, System.Timers.ElapsedEventArgs e){

picturebox1.Image = new System.Drawing.Bitmap(ImagefileInfos[0].FullName);
/*この辺の書き方は俺の書き方の好みがモロに出てる */
ImagefileInfos.Add(ImagefileInfos[0]);

ImagefileInfos.RemoveAt(0);

}

public static void Main(String[] args){



Q5582265 form1 = new Q5582265();

form1.ShowDialog();



}

}

}

/*
フォルダ名が正しくなかった時の処理はしてない。
実際にはテキストボックスへの直接入力を認めず、
OpenFileDialogでも置いた方がいいかと。

Windows APIには頼りたくないな。
俺のやり方だと、yieldっぽいのなくて、ファイル名全てを取得して帰ってくるまで待たされる欠点はあるけど。>#1
*/
    • good
    • 0
この回答へのお礼

細かくわかりやすい説明ありがとうございます。

お礼日時:2010/01/10 23:40

FileFindFirstとFileFindNext使うのでは駄目?

    • good
    • 0
この回答へのお礼

アドバイスありがとうございます。
参考にしてみます。

お礼日時:2010/01/10 23:41

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!