2013年3月3日日曜日

フォルダ内の画像を表示スライドショーするプログラム

Pictures Slide Viewer
選択したフォルダー内(サブフォルダー内を含む)の画像ファイルをリストにし
スライドショー表示(全画面表示対応)するプログラムです。
スライドショーの再生間隔を、0.5秒から1時間まで、0.5秒単位で変更できます。



PicturesSlideViewer.exe  C#  WinDesktop
Form1 に button1 ~ 6 、 label1 ~ 3 、pictureBox1、listBox1、numericUpDown1 を配置
コンポーネント timer1、 ダイアログ folderBrowserDialog1 を追加

Form1.cs
=====================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PicturesSlideViewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // ListBox1に追加する
        private void button1_Click(object sender, EventArgs e)
        {
            // 初期選択するパスを設定する
            // RootFolder以下にあるフォルダである必要がある
            folderBrowserDialog1.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            // ダイアログを表示し、戻り値が [OK] の場合は、選択したディレクトリを表示する
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // 選択したフォルダ以下のファイルをすべて取得、ListBox1に結果を追加する
                    string[] filesjpg = System.IO.Directory.GetFiles(
                        folderBrowserDialog1.SelectedPath, "*.jp*g" , System.IO.SearchOption.AllDirectories);
                    listBox1.Items.AddRange(filesjpg);

                    string[] filesgif = System.IO.Directory.GetFiles(
                        folderBrowserDialog1.SelectedPath, "*.gif", System.IO.SearchOption.AllDirectories);                    
                    listBox1.Items.AddRange(filesgif);

                    string[] filesbmp = System.IO.Directory.GetFiles(
                        folderBrowserDialog1.SelectedPath, "*.bmp", System.IO.SearchOption.AllDirectories);
                    listBox1.Items.AddRange(filesbmp);

                    string[] filespng = System.IO.Directory.GetFiles(
                        folderBrowserDialog1.SelectedPath, "*.png", System.IO.SearchOption.AllDirectories);
                    listBox1.Items.AddRange(filespng);

                    // リストの1番を選択
                    listBox1.SetSelected(0, true);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        // 終了
        private void button2_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.ImageLocation = listBox1.Text;
            label1.Text = "ファイル名:" + listBox1.Text;
        }

        // 次へ
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count > 0)
            {
                if (listBox1.SelectedIndex == listBox1.Items.Count - 1)
                {
                    listBox1.SetSelected(0, true);
                }
                else
                {
                    int i = listBox1.SelectedIndex + 1;
                    listBox1.SetSelected(i, true);
                }
            }

        }

        // 前へ
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count > 0)
            {
                if (listBox1.SelectedIndex == 0)
                {
                    listBox1.SetSelected(listBox1.Items.Count - 1, true);
                }
                else
                {
                    int i = listBox1.SelectedIndex - 1;
                    listBox1.SetSelected(i, true);
                }
            }
        }

        // ListBox1をクリアにする
        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            label1.Text = "ファイル名:";
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = null;
            }
        }
        
        // スライドショー再生・停止
        private void button5_Click(object sender, EventArgs e)
        {
            if (button5.Text == "再生 >")
            {
                button5.Text = "停止 ■";
                // button3.PerformClick();
                timer1.Interval = Convert.ToInt32(numericUpDown1.Value);
                timer1.Start();
            }
            else
            {
                button5.Text = "再生 >";
                timer1.Stop();
            }
        }

        // スライドショーの切り替え間隔変更
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            timer1.Interval = Convert.ToInt32(numericUpDown1.Value);
        }

        // タイマーイベント
        private void timer1_Tick(object sender, EventArgs e)
        {
            button3.PerformClick();
        }

        // フルスクリーンモード

        // フルスクリーン・モードかどうかのフラグ
        private bool _bScreenMode;
        // フルスクリーン表示前のウィンドウの状態を保存する
        private FormWindowState prevFormState;
        // 通常表示時のフォームの境界線スタイルを保存する
        private FormBorderStyle prevFormStyle;
        // 通常表示時のウィンドウのサイズを保存する
        private Size prevFormSize;
        // 通常表示時のピクチャーボックスのサイズを保存する
        private Size prevPictureBoxSize;

        // フルスクリーンに切り替え
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (_bScreenMode == false)
            {
                // <フルスクリーン表示への切り替え処理>

                // ウィンドウの状態を保存する
                prevFormState = this.WindowState;
                // 境界線スタイルを保存する
                prevFormStyle = this.FormBorderStyle;
                // ピクチャーボックスのサイズを保存
                prevPictureBoxSize = pictureBox1.Size;

                // 「最大化表示」→「フルスクリーン表示」では
                // タスク・バーが消えないので、いったん「通常表示」を行う
                if (this.WindowState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Normal;
                }

                // フォームのサイズを保存する
                prevFormSize = this.Size;
                // フォームの境界線スタイルを「None」にする
                this.FormBorderStyle = FormBorderStyle.None;
                // フォームのウィンドウ状態を「最大化」する
                this.WindowState = FormWindowState.Maximized;
                // ピクチャーボックスを最大化
                pictureBox1.Size = this.ClientSize;

                // フルスクリーン表示フラグをONにする
                _bScreenMode = true;
            }
            else
            {
                // 既にフルスクリーンの場合は、次へ
                button3.PerformClick();
            }
        }

        // フルスクリーン解除
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (_bScreenMode == true)
            {
                // 次に進んでしまうので前に戻す
                button4.PerformClick();

                // <通常表示/最大化表示への切り替え処理>

                // フォームのウィンドウのサイズを元に戻す
                this.ClientSize = prevFormSize;

                // 最大化表示に戻す場合にはいったん通常表示を行う
                // (フルスクリーン表示の処理とのバランスと取るため)
                if (prevFormState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Normal;
                }
                // フォームの境界線スタイルを元に戻す
                this.FormBorderStyle = prevFormStyle;

                // フォームのウィンドウ状態を元に戻す
                this.WindowState = prevFormState;

                // ピクチャーボックスを戻す
                pictureBox1.Size = prevPictureBoxSize;

                // フルスクリーン表示フラグをOFFにする
                _bScreenMode = false;
            }
        }
    }
}
=====================================================================


今回は追加したコントロールが多いのでこちらも掲載
Form1.Designer.cs

=====================================================================

namespace PicturesSlideViewer
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナーで生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.label2 = new System.Windows.Forms.Label();
            this.button6 = new System.Windows.Forms.Button();
            this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
            this.label3 = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
            this.SuspendLayout();
            //
            // pictureBox1
            //
            this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(823, 567);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
            //
            // label1
            //
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 570);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(57, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "ファイル名:";
            //
            // button1
            //
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.Location = new System.Drawing.Point(829, 596);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "追加";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.button2.Location = new System.Drawing.Point(12, 596);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 3;
            this.button2.Text = "終了";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.button3.Location = new System.Drawing.Point(373, 596);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 4;
            this.button3.Text = "次へ >>";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // button4
            //
            this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.button4.Location = new System.Drawing.Point(183, 596);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 5;
            this.button4.Text = "<< 前へ";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            //
            // button5
            //
            this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.button5.Location = new System.Drawing.Point(277, 596);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 6;
            this.button5.Text = "再生 >";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            //
            // listBox1
            //
            this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.listBox1.FormattingEnabled = true;
            this.listBox1.HorizontalScrollbar = true;
            this.listBox1.ItemHeight = 12;
            this.listBox1.Location = new System.Drawing.Point(829, 26);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(160, 556);
            this.listBox1.TabIndex = 7;
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            //
            // label2
            //
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(840, 9);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 12);
            this.label2.TabIndex = 8;
            this.label2.Text = "リスト";
            //
            // button6
            //
            this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button6.Location = new System.Drawing.Point(914, 596);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(75, 23);
            this.button6.TabIndex = 9;
            this.button6.Text = "全削除";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            //
            // numericUpDown1
            //
            this.numericUpDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.numericUpDown1.Increment = new decimal(new int[] {
            500,
            0,
            0,
            0});
            this.numericUpDown1.Location = new System.Drawing.Point(534, 596);
            this.numericUpDown1.Maximum = new decimal(new int[] {
            360000,
            0,
            0,
            0});
            this.numericUpDown1.Minimum = new decimal(new int[] {
            500,
            0,
            0,
            0});
            this.numericUpDown1.Name = "numericUpDown1";
            this.numericUpDown1.ReadOnly = true;
            this.numericUpDown1.Size = new System.Drawing.Size(81, 19);
            this.numericUpDown1.TabIndex = 10;
            this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.numericUpDown1.ThousandsSeparator = true;
            this.numericUpDown1.Value = new decimal(new int[] {
            2000,
            0,
            0,
            0});
            this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
            //
            // label3
            //
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(621, 598);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(31, 12);
            this.label3.TabIndex = 11;
            this.label3.Text = "ミリ秒";
            //
            // timer1
            //
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // folderBrowserDialog1
            //
            this.folderBrowserDialog1.Description = "フォルダを選択";
            this.folderBrowserDialog1.ShowNewFolderButton = false;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(994, 631);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.numericUpDown1);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Pictures Slide Viewer";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.NumericUpDown numericUpDown1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
    }
}
=====================================================================




0 件のコメント:

コメントを投稿