最後にImageConverter Ver 1.1の全コードを載せます。
【ImageConverte.cs】
//////////////////////////////////////////
// ImageConverter.cs
// Copyright (c) by Ysama September, 2024
// Version 1.1 (for HEIC) November, 2026
//////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging; //ImageCodecInfoを使う為
using System.IO; //https://learn.microsoft.com/ja-jp/dotnet/api/system.io.path?view=net-8.0
using System.Linq; //配列のWhere抽出を行う為
using System.Reflection; //Assemblyを使う為
using Heic2GdiPlus; //Heic2GDIplus使用の為
namespace ImageConverter
{
public class ImgConverter : Form
{
//MimeTypeリスト
string[] img_MimeType = new string [] {"image/png", "image/jpeg", "image/bmp", "image/gif", "image/tiff"};
//拡張子
string[] img_ext = new string [] {".png", ".jpg", ".bmp", ".gif", ".tiff"};
//出力フォールダーパス
string output_Path = null;
//出力イメージファイルパス・名
string output_Name = null;
//コントロール
private ListBox listBox; //変換対象ファイルのファイルパス、名のリスト表示用
private Button open_button; //変換対象ファイルの追加用
private Button del_button; //変換対象ファイルの削除用
private Button clr_button; //リストボックスクリア用
private Button sel_button; //変換ファイル出力先の指定用
private Button cnvt_button; //ファイル形式の変換用
private Button exit_button; //終了用
private ToolTip tooltip; //変換ファイル出力先の表示用
private Label num_label; //画像説明用
private Label format_label; //変換ファイル形式指定用
private Label w_label; //変更サイズ幅用
private Label h_label; //変更サイズ高用
private PictureBox pictureBox; //イメージファイル表示用
private ComboBox comBox; //変換ファイル形式指定用
private CheckBox selected_only; //選択ファイルのみ(falseの場合はリストボックスの全ファイル)変換
private CheckBox resizing; //変換時にサイズを変更する
private CheckBox asp_ratio; //縦横比(aspect ratio)維持有無
private TextBox img_Width; //変更サイズ幅
private TextBox img_Height; //変更サイズ高
//エントリーポイント
[STAThread]
public static void Main()
{
Application.Run(new ImgConverter());
}
//コンストラクター
public ImgConverter()
{
this.Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location); //プログラムアイコンをフォームにつける
this.Size = new Size(496, 518);
this.MinimumSize = new Size(496, 518);
this.Text = "Image Converter";
this.Load += new EventHandler(InitComponent); //コントロール等の初期化
}
//コントロール等の初期化
private void InitComponent(object sender, EventArgs e)
{
//表示停止
SuspendLayout();
//リストボックス
listBox = new ListBox();
listBox.Location = new Point(10, 10);
listBox.Size = new Size(280, 148);
listBox.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
listBox.HorizontalScrollbar = true;
listBox.TabIndex = 0;
//listBox.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged);
listBox.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox.DragEnter += listBox_DragEnter;
listBox.DragDrop += listBox_DragDrop;
listBox.AllowDrop = true;
//ボタン1
open_button = new Button();
open_button.Location = new Point(300, 10);
open_button.Size = new Size(160, 24);
open_button.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
open_button.Text = "イメージの選択・追加";
open_button.Click += new EventHandler(open_button_Click);
clr_button = new Button();
clr_button.Location = new Point(300, open_button.Height + 20);
clr_button.Size = new Size(160, 24);
clr_button.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
clr_button.Text = "リスト初期化";
clr_button.Click += new EventHandler(clr_button_Click);
del_button = new Button();
del_button.Location = new Point(300, open_button.Height + clr_button.Height + 30);
del_button.Size = new Size(160, 24);
del_button.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
del_button.Text = "イメージの削除";
del_button.Click += new EventHandler(del_button_Click);
sel_button = new Button();
sel_button.Location = new Point(300, open_button.Height + del_button.Height + clr_button.Height + 60);
sel_button.Size = new Size(160, 24);
sel_button.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
sel_button.Text = "出力先指定";
sel_button.Click += new EventHandler(sel_button_Click);
exit_button = new Button();
exit_button.Location = new Point(300, this.ClientSize.Height - exit_button.Height - 10);
exit_button.Size = new Size(160, 24);
exit_button.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
exit_button.Text = "終了";
exit_button.Click += new EventHandler(exit_button_Click);
cnvt_button = new Button();
cnvt_button.Location = new Point(300, this.ClientSize.Height - exit_button.Height - cnvt_button.Height - 20);
cnvt_button.Size = new Size(160, 24);
cnvt_button.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
cnvt_button.Text = "イメージ形式を変換";
cnvt_button.Click += new EventHandler(cnvt_button_Click);
//ツールチップ
tooltip = new ToolTip();
tooltip.ShowAlways = true;
//ラベル1
num_label = new Label();
num_label.AutoSize = true;
num_label.Location = new Point(10, this.ClientSize.Height - num_label.Height);
num_label.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
num_label.Text = "イメージ番号:";
format_label = new Label();
format_label.Size = new Size(160, 20);
format_label.Location = new Point(300, open_button.Height + del_button.Height + clr_button.Height + sel_button.Height + 65);
format_label.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
format_label.TextAlign = ContentAlignment.MiddleCenter;
format_label.Text = "出力ファイル形式指定";
//ピクチャーボックス
pictureBox = new PictureBox();
pictureBox.BorderStyle = BorderStyle.Fixed3D;
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox.Size = new Size(280, 280);
pictureBox.Location = new Point(10, this.ClientSize.Height - num_label.Height - pictureBox.Height - 8);
pictureBox.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
//コンボボックス
comBox = new ComboBox();
comBox.Location = new Point(300, open_button.Height + del_button.Height + clr_button.Height + sel_button.Height + format_label.Height + 65);
comBox.Size = new Size(160, 24);
comBox.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
comBox.DropDownStyle = ComboBoxStyle.DropDownList;
comBox.Items.AddRange(img_MimeType);
comBox.SelectedIndex = 0; //初期値は"image/png"
//チェックボックス1
selected_only = new CheckBox();
selected_only.TextAlign = ContentAlignment.MiddleLeft;
selected_only.Location = new Point(10, 20);
selected_only.Size = new Size(160, 24);
selected_only.Text = "選択ファイルのみ変換";
resizing = new CheckBox();
resizing.TextAlign = ContentAlignment.MiddleLeft;
resizing.Location = new Point(10, selected_only.Height + 25);
resizing.Size = new Size(160, 24);
resizing.Text = "ファイルサイズを変更";
resizing.CheckedChanged += new EventHandler(resizing_CheckedChanged);
//テキストボックス
img_Width = new TextBox();
img_Width.Location = new Point(60, selected_only.Height + resizing.Height + 30);
img_Width.Size = new Size(100, 24);
img_Width.Enabled = false;
img_Height = new TextBox();
img_Height.Location = new Point(60, selected_only.Height + resizing.Height + img_Width.Height + 35);
img_Height.Size = new Size(100, 24);
img_Height.Enabled = false;
//チェックボックス2
asp_ratio = new CheckBox();
asp_ratio.TextAlign = ContentAlignment.MiddleLeft;
asp_ratio.Location = new Point(60, selected_only.Height + resizing.Height + img_Width.Height + img_Height.Height + 35);
asp_ratio.Size = new Size(100, 24);
asp_ratio.Text = "縦横比を維持";
asp_ratio.Enabled = false;
//ラベル2
w_label = new Label();
w_label.Size = new Size(60, 20);
w_label.Location = new Point(10, selected_only.Height + resizing.Height + 32);
w_label.Text = "画像幅";
h_label = new Label();
h_label.Size = new Size(60, 20);
h_label.Location = new Point(10, selected_only.Height + resizing.Height + img_Width.Height + 37);
h_label.Text = "画像高";
//オプショングループボックス
GroupBox groupBox = new GroupBox();
groupBox.Location = new Point(296, open_button.Height + del_button.Height + clr_button.Height + sel_button.Height + format_label.Height + comBox.Height + 70);
groupBox.Size = new Size(174, 150);
groupBox.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
groupBox.Text = "Option";
groupBox.Controls.Add(selected_only);
groupBox.Controls.Add(resizing);
groupBox.Controls.Add(img_Width);
groupBox.Controls.Add(img_Height);
groupBox.Controls.Add(w_label);
groupBox.Controls.Add(h_label);
groupBox.Controls.Add(asp_ratio);
//フォームにコントロールを追加
Controls.Add(listBox);
Controls.Add(open_button);
Controls.Add(clr_button);
Controls.Add(del_button);
Controls.Add(sel_button);
Controls.Add(format_label);
Controls.Add(comBox);
Controls.Add(groupBox);
Controls.Add(cnvt_button);
Controls.Add(exit_button);
Controls.Add(pictureBox);
Controls.Add(num_label);
//表示再開
ResumeLayout(false);
}
(2)へ続く。