前回のアプリを使うと面白い写真を大量Getすることができます。Winndowsに付属しているフォトギャラリーでみてもいいのですが、特にお気に入りの写真を簡単にみることができるようにフォトビューアーをつくることにします。
これもあえて自作しなくてもフリーで使えるものがあると思うのですが、自作することにします。(誰です?「四角い車輪の再発明」といっているのは)
こんな感じになります。

右側にあるのはリストボックスでファイルをドラッグ&ドロップをすると画像を登録することができます。そしてアイテムをクリックすると画像をみることができます。
背景色を黒にするとなんとなく高級感がでてきます。左にはPictureBoxを貼り付けています。PictureBoxはフォームの大きさが変更されると大きさがかわります。

まずリストボックスにファイルをドラッグすると項目として追加されるようにしましょう。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public partial class Form1 : Form {     public Form1()     {         InitializeComponent();         pictureBox1.BorderStyle = BorderStyle.None;         pictureBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;         listBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;         listBox1.DragOver += listBox1_DragOver;         listBox1.DragDrop += listBox1_DragDrop;         listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;     }     private void listBox1_DragOver(object sender, DragEventArgs e)     {         if (e.Data.GetDataPresent(DataFormats.FileDrop))         {             e.Effect = DragDropEffects.Copy;         }     }     private void listBox1_DragDrop(object sender, DragEventArgs e)     {         if (e.Data.GetDataPresent(DataFormats.FileDrop))         {             string[] vs = (string[])e.Data.GetData(DataFormats.FileDrop);             Point point = listBox1.PointToClient(new Point(e.X, e.Y));             int index = listBox1.IndexFromPoint(point);             if (index == -1)                 listBox1.Items.Add(vs[0]);             else                 listBox1.Items.Insert(index, vs[0]);         }     } } | 
ドロップされた場所によって末尾ではなく間に追加されます。
リストボックスの項目が選択されたら画像を表示させます。リストの項目名をファイルのパスにしています。そのまま取得して利用しています。
| 1 2 3 4 5 6 7 8 | public partial class Form1 : Form {     private void listBox1_SelectedIndexChanged(object sender, EventArgs e)     {         string filePath = (string)listBox1.SelectedItem;         ShowImage(filePath);     } } | 
ファイルパスを利用してピクチャーボックスに画像をさせる ShowImageメソッドです。
表示させようとしているイメージとPictureBoxのサイズでは大きいのはどちらでしょうか。イメージのほうが幅、高さのどちらかが大きい場合、PictureBoxのサイズにあわせてイメージを表示させます。PictureBoxのサイズが大きい場合は中央に表示させます。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public partial class Form1 : Form {     void ShowImage(string filePath)     {         // ファイルが存在するかチェック         if (!File.Exists(filePath))         {             MessageBox.Show("ファイルが存在しません。");             return;         }         // このファイルは画像ファイルなのか?         Image img = null;         try         {             img = Image.FromFile(filePath);         }         catch         {             MessageBox.Show("画像ファイルではありません。");             return;         }         // イメージとPictureBoxのサイズを比較する         int imageWidth = img.Width;         int imageHeight = img.Height;         img.Dispose();         int picWidth = pictureBox1.Size.Width;         int picHeight = pictureBox1.Size.Height;         if (picWidth < imageWidth || picHeight < imageHeight)         {             pictureBox1.ImageLocation = filePath;             pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;         }         else         {             pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;             pictureBox1.ImageLocation = filePath;         }     } } | 
フォームの大きさが変更になったときには再度ShowImageを呼び出しています。
| 1 2 3 4 5 6 7 8 9 10 11 | public partial class Form1 : Form {     private void Form1_Resize(object sender, EventArgs e)     {         if (listBox1.SelectedIndex == -1)             return;         string filePath = (string)listBox1.SelectedItem;         ShowImage(filePath);     } } | 
とりあえず大枠ができたので今日はおしまい。
