LoginSignup
3
2

More than 5 years have passed since last update.

ただひたすらに画像を開くだけのImageViewerをC#で作った

Last updated at Posted at 2018-08-03

画像を開くやつを作った

ただひたすらに画像を開き見るだけ
以下二つの起動パターンがある
・起動してからドラッグ&ドロップ
・画像の起動アプリケーションに指定(ダブルクリックで開けるようになる)

理由

・win10フォトの動作が異常に重いときがある(画像サイズに関係ない)
・ていうか画像見たいだけなのでほかの機能いらない
・画像の取り扱い方法をいろいろ調べてた

学び

・コマンドライン引数というものを知る
・Image機能強い
・身近なソフトウェアの簡単な改善は自分ですぐできる

設定とか

・FormのAllowDropをTrueに
・controlSizeChanger()のthis.width,heightパラメータをいじれば
最小のウィンドウサイズを変えられる

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 ImageViewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Console.WriteLine(System.Environment.CommandLine);

            string filepath;

            //コマンドライン引数を配列で取得する
            string[] cmds = System.Environment.GetCommandLineArgs();
            //コマンドライン引数を列挙する
            if(cmds.Length >= 2)
            {
                filepath = cmds[1];

                System.Drawing.Image img = System.Drawing.Image.FromFile(filepath);

                controlSizeChanger();

                pictureBox1.Image = img;
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            this.pictureBox1.Width = this.Width;
            this.pictureBox1.Height = this.Height;

            this.panel1.Width = this.Width - 15;
            this.panel1.Height = this.Height - 37;
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;

            foreach(var filepath in (string[])e.Data.GetData(DataFormats.FileDrop))
            {
                System.Drawing.Image img = Image.FromFile(filepath);
                this.pictureBox1.Image = img;

                controlSizeChanger();
            }
        }

        private void controlSizeChanger()
        {
            this.Width = 700;
            this.Height = 800;

            this.pictureBox1.Width = this.Width;
            this.pictureBox1.Height = this.Height;

            this.panel1.Width = this.Width - 15;
            this.panel1.Height = this.Height - 37;
        }
    }
}

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2