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);
}
しかしこれではフォルダの最後の画像しか表示されません。
この場合次々と画像を表示するにはどうしたらいいか教えてください。
No.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
*/
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- その他(プログラミング・Web制作) セレクトボックスで選択された値をコントローラーで使用したい 2 2022/07/26 16:41
- Excel(エクセル) エクセル VBA For Next 繰り返しの書き方を教えてください 6 2022/09/01 14:11
- JavaScript アップロードファイルの種類によって処理を分岐させたいのですが書き方が分からずアドバイスお願いします 4 2023/06/17 19:12
- その他(プログラミング・Web制作) python OpenPyXLを使って出力結果をエクセルに書き込み 2 2022/06/04 19:46
- Excel(エクセル) 【マクロ】フォルダを3つ、POモニター上に、決まった並べ方をしたい 4 2022/08/31 11:05
- Excel(エクセル) excelvbaでスライドショーを作りたい 2 2023/04/20 14:32
- JavaScript clear機能を失わずにファイルアップロード機能を作成したい 3 2023/06/10 16:12
- C言語・C++・C# このプログラミング誰か教えてくれませんか 1 2022/06/02 15:27
- Excel(エクセル) EXCELのグラフを画像(JPG形式)で保存、通常実行がうまく行かない。ステップインはうまく行く 3 2022/08/30 12:06
- アプリ MacBookでCLIP STUDIO PAINTから「写真」アプリを開いて、画像を読み込みたい 1 2023/08/05 12:34
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
VC++ (byte)(col & 0xFF) の意味
-
共有フォルダの「共有のアクセ...
-
Functionの戻り値を配列にした...
-
Excel VBAでsub,dimは何の略?
-
パーソナルXLSBのfuctionを呼び...
-
C言語のサフィックスについて
-
三項でたとえば交換って
-
VBA public変数はどのようなこ...
-
ピッ、ってアルファベットでど...
-
文字列を配列に…。
-
テキストボックスを空白にする方法
-
visual basic初心者です。 visu...
-
テキストボックス内の簡単な計算
-
VB6でClickイベントを一時的に...
-
subプロシージャーですか?sub...
-
EXCEL VBA 文字 アドレス 検...
-
RichTextBoxで指定行の色を自動...
-
texで図と表を並べたい
-
エクセルVBAついて ①if•••••the...
-
ExcelのVBA。Staticな変数について
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
二点の座標から距離や角度を求...
-
複数条件のオートフィルタ(VBA)
-
エクセル・VBAでテキストボック...
-
VC++ (byte)(col & 0xFF) の意味
-
エクセルのVBAでの7×7の魔方陣...
-
VB.NET)コンボボックスの連動に...
-
検索結果の指定列をリストボッ...
-
for文の質問です。
-
テキストファイルの読み込みと...
-
VBAにおけるニュートン法
-
カラーの16進表記の足し算
-
一行飛ばしで合計
-
2次元配列を返す関数について
-
どいつもこいつもbot3かよ
-
こうもりの撃退法
-
小数点以下五桁一致の判定
-
4変数の非線形方程式のときかた
-
ドラゴン曲線を再帰で書く
-
三項でたとえば交換って
-
VBA public変数はどのようなこ...
おすすめ情報