ディレクトリ内の画像ファイルを連続表示するグラフィックビューワ3

.NET Framework コンピュータ
.NET Framework

画像を左右に並べて見開き表示されるように変更。

コンパイル準備

ソースコード

using System;
using System.Windows.Forms;
using System.Drawing;

using System.Collections.Generic;
using System.Linq;

using System.IO;
using System.Text.RegularExpressions;

using System.IO.Compression;

// 
// ディレクトリ内の画像ファイルを連続表示するグラフィックビューワー
// 
// 履歴
// Zipファイル内の画像ファイルに対応
// 見開き表示

// コンパイル
// csc /t:winexe SlideView.cs /r:System.IO.Compression.dll /r:System.IO.Compression.FileSystem.dll


namespace SlideView
{
    class ImageItem
    {
        public string Name = "";
        public string Location = "";
    }
    
    class Form1 : Form
    {
        enum EViewMode { Single, Dual, };

        EViewMode ViewMode = EViewMode.Dual;
        List<ImageItem> ImageItems = new List<ImageItem>();
        int ImagePosition = -1;

        void MoveNext()
        {
            if (ImagePosition < 0) return;
            if (ImagePosition == (ImageItems.Count() - 1)) return;

            ImagePosition = ImagePosition + 1;
            ShowPicture();
        }
        void MovePrevious()
        {
            if (ImagePosition <= 0) return;

            ImagePosition = ImagePosition - 1;

            if (ViewMode == EViewMode.Single)
            {
                ShowPicture();
                return;
            }
            if (ViewMode == EViewMode.Dual)
            {
                if (ImagePosition == 0)
                {
                    ShowPicture();
                    return;
                }

                ImagePosition = ImagePosition - 1;
                if (ImagePosition == 0)
                {
                    ShowPicture();
                    return;
                }

                ImagePosition = ImagePosition - 1;
                if (ImagePosition == 0)
                {
                    ShowPicture();
                    return;
                }
                
                ShowPicture();
                return;
            }

        }
        void PictureBox1_MouseDown(Object s, MouseEventArgs e)
        {
            if (ImagePosition < 0) return;

            if (e.Button == MouseButtons.Right)
            {
                MovePrevious();
            }
            if (e.Button == MouseButtons.Left)
            {
                MoveNext();
            }
        }        
        PictureBox PictureBox1 = new PictureBox
        {
            Dock = DockStyle.Fill,
            AllowDrop = true,
            SizeMode = PictureBoxSizeMode.Zoom,
        };

        Image JoinImage(Image l, Image r)
        {
            int h = l.Height > r.Height ? l.Height : r.Height;
            int w = l.Width + r.Width;

            Bitmap bmp = new Bitmap(w, h);
            using(var g = Graphics.FromImage(bmp))
            {
                var ls = new Rectangle(0, 0, l.Width, l.Height);
                g.DrawImage(l, ls, ls, GraphicsUnit.Pixel);
                var rs = new Rectangle(0, 0, r.Width, r.Height);
                var rd = new Rectangle(l.Width, 0, r.Width, r.Height);
                g.DrawImage(r, rd, rs, GraphicsUnit.Pixel);
            }

            return bmp;
        }
        Image GetImage(int possition)
        {
            Image img = null;
            string location = ImageItems[possition].Location;
            string name = ImageItems[possition].Name;
            if (Directory.Exists(location))
            {
                string path = Path.Combine(location, name);
                using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    img = Image.FromStream(fs);
                }
            }
            else if (File.Exists(location))
            {
                // Zipアーカイブ
                using (var zip = ZipFile.OpenRead(location))
                {
                    var entry = zip.GetEntry(name);
                    img = Image.FromStream(entry.Open());
                }
            }
            return img;
        }
        void ShowPicture()
        {
            if (ImagePosition < 0) return;

            if (PictureBox1.Image != null)
                PictureBox1.Image.Dispose();
            
            Image img = GetImage(ImagePosition);

            if (ImagePosition == 0)
            {
                // 表紙(先頭ページ)は単ページ
                PictureBox1.Image = img;
                return;
            }

            if (ImagePosition == (ImageItems.Count() - 1))
            {
                // 最終ページ
                PictureBox1.Image = img;
                return;
            }
            if (ViewMode == EViewMode.Single)
            {
                // 単ページモード
                PictureBox1.Image = img;
                return;
            }
            if (ViewMode == EViewMode.Dual)
            {
                // 見開きモード

                // 左ページ読込
                Image img2 = GetImage(ImagePosition+1);
                PictureBox1.Image = JoinImage(img2, img);
                img2.Dispose();
                img.Dispose();
                
                ImagePosition = ImagePosition + 1;
            }
        }
        enum PathType { None, Img, Dir, Zip };
        void SetNewLocation(string path)
        {
            string location = "";

            PathType pathType = PathType.None;

            if (File.Exists(path))
            {
                if (Path.GetExtension(path).ToUpper() == ".ZIP")
                {
                    location = path;
                    pathType = PathType.Zip;
                }
                else
                {
                    location = Path.GetDirectoryName(path);
                    pathType = PathType.Img;
                }
            }
            else if(Directory.Exists(path))
            {
                location = path;
                pathType = PathType.Dir;
            }
            else return;

            ImageItems.Clear();
            ImagePosition = 0;

            if (pathType == PathType.Img || pathType == PathType.Dir)
            {
                var files = Directory.EnumerateFiles(
                    location, "*", System.IO.SearchOption.TopDirectoryOnly);
                
                ImagePosition = 0;
                foreach(string f in files)
                {
                    string ext = Path.GetExtension(f).ToUpper();

                    if (!Regex.IsMatch(ext, @"\.(PNG|JPEG|JPG|BMP)"))
                        continue;


                    ImageItems.Add(new ImageItem
                    {
                        Name = Path.GetFileName(f),
                        Location = location,
                    });
                    if (f == path)
                    {
                        ImagePosition = ImageItems.Count() - 1;
                    }
                }
            }
            else if (pathType == PathType.Zip)
            {
                using (var zip = ZipFile.OpenRead(location))
                {
                    foreach(var x in zip.Entries)
                    {
                        string ext = Path.GetExtension(x.FullName).ToUpper();

                        if (!Regex.IsMatch(ext, @"\.(PNG|JPEG|JPG|BMP)"))
                            continue;
                        
                        ImageItems.Add(new ImageItem
                        {
                            Name = x.FullName,
                            Location = location,
                        });
                    }
                }
            }
            if (ImageItems.Count() == 0)
            {
                ImagePosition = -1;
                return;
            }
            ShowPicture();
        }
        void PictureBox1_DragDrop(Object s, DragEventArgs e)
        {
            string[] files =
                (string[])e.Data.GetData(DataFormats.FileDrop, false);
            SetNewLocation(files[0]);
        }
        void PictureBox1_DragEnter(Object s, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }
        void ChangeScreenMode()
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                // フルスクリーン
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;                
            }
            else
            {
                // ウィンドウ
                this.FormBorderStyle = FormBorderStyle.Sizable;
                this.WindowState = FormWindowState.Normal;             
            }
        }
        void Form1_KeyDown(Object s, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case (Keys.F11):
                    ChangeScreenMode();
                    break;
            }
        }
        Form1()
        {
            this.KeyPreview = true;

            this.Controls.Add(PictureBox1);

            this.KeyDown += Form1_KeyDown;
            PictureBox1.DragEnter += PictureBox1_DragEnter;
            PictureBox1.DragDrop += PictureBox1_DragDrop;
            PictureBox1.MouseDown += PictureBox1_MouseDown;
        }
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }// class
}// ns

コンパイル

csc /t:winexe SlideView.cs /r:System.IO.Compression.dll /r:System.IO.Compression.FileSystem.dll

実行

最初のページは単ページ表示

2ページ以降は見開き表示

クリックを続けて最終ページへ

表示モードの切り替え機能は無し。

コメント