/// <summary> /// 画像データ格納フォルダ選択の選択ボタンを押下 /// </summary> /// <param name="sender">センダーオブジェクト</param> /// <param name="e">イベントデータ</param> private void BtImageDirectorySelect_Click(object sender, EventArgs e) { // フォルダ選択ダイアログを開き、画像データが格納してあるフォルダを選択する FolderBrowserDialog dialog; using (dialog = new FolderBrowserDialog()) { dialog.Description = Resources.CombineImagesDirectorySelectDialogTitle; dialog.ShowNewFolderButton = false; if (Directory.Exists(TxtImageDirectory.Text)) { dialog.SelectedPath = TxtImageDirectory.Text; } // ダイアログを表示 if (dialog.ShowDialog() == DialogResult.OK) { // フォルダ選択でOKが押された場合、選択したファイルパスを取得 string folderPath = dialog.SelectedPath; // 選択されたフォルダに画像データが存在するかチェック try { if (Directory.GetFiles(folderPath).Any(filePath => ImageTransform.CanImageLoad(filePath))) { // チェックOKの場合、選択したフォルダのパスを設定する TxtImageDirectory.Text = folderPath; // 実行エリアを使用可能とする PlRun.Enabled = true; } else { // チェックNGの場合、エラーメッセージを表示する MessageBox.ShowAttention(this, Resources.CombineImagesDirectoryErrorNotImage); } } catch (Exception ex) when(ex is UnauthorizedAccessException || ex is IOException) { // 下記の例外が発生した場合はメッセージを表示する // UnauthorizedAccessException // ・呼び出し元に、必要なアクセス許可がない場合 // IOException // ・IOエラーが発生した場合 string message = Resources.CombineImagesDirectoryErrorNotAccess + "\r\n" + ex.Message; MessageBox.ShowAttention(this, message); } } } // 画面の表示設定を行う SetControlDisplaySetting(); }
/// <summary> /// 実行時に行う、画像データ格納フォルダに画像データが存在するかチェックする /// </summary> /// <param name="filePaths">画像データ格納フォルダのファイルリスト</param> /// <returns>チェック結果、チェックOK:True、NG:False</returns> private bool RunCheckImageFileExists(string[] filePaths) { // 画像データ格納フォルダに画像データが存在するかチェックする try { if (!filePaths.Any(imageFilePath => ImageTransform.CanImageLoad(imageFilePath))) { // チェックNGの場合、エラーメッセージを表示し処理をしない MessageBox.ShowAttention(this, Resources.CombineImagesDirectoryErrorNotImage); return(false); } return(true); } catch (Exception ex) { // 下記のエラーの場合はメッセージを表示して処理をしない if (ex is ArgumentException || ex is PathTooLongException) { // 下記のパスが不正の場合の例外の場合は、その旨のメッセージを表示し処理しない // ArgumentException // ・パスとして正しくない文字を含んで入る場合 // PathTooLongException // ・指定したパスがシステム定義の最大長を超えている場合 string message = Resources.CombineImagesDirectoryErrorIncorrectPath + "\r\n" + ex.Message; MessageBox.ShowAttention(this, message); return(false); } else if (ex is DirectoryNotFoundException || ex is UnauthorizedAccessException || ex is IOException) { // 下記のアクセスに関する例外の場合は、その旨のメッセージを表示し処理しない // DirectoryNotFoundException: // ・指定したディレクトリが見つからない場合 // UnauthorizedAccessException // ・呼び出し元に、必要なアクセス許可がない場合 // IOException // ・IOエラーが発生した場合 string message = Resources.CombineImagesDirectoryErrorNotAccess + "\r\n" + ex.Message; MessageBox.ShowAttention(this, message); return(false); } // 上記以外の例外の場合はそのままスローする throw; } }
/// <summary> /// 追記元Gifの選択の選択ボタンを押下 /// </summary> /// <param name="sender">センダーオブジェクト</param> /// <param name="e">イベントデータ</param> private void BtAppendGifFileSelect_Click(object sender, EventArgs e) { // ファイル選択ダイアログを開き、回転パラメータファイルを選択する OpenFileDialog dialog; using (dialog = new OpenFileDialog()) { // ファイル選択ダイアログの表示設定 dialog.Title = Resources.CombineImagesGifFileSelectDialogTitle; dialog.Filter = Resources.CombineImagesGifFileSelectDialogFilter; dialog.FilterIndex = 1; dialog.RestoreDirectory = true; // ダイアログを表示 if (dialog.ShowDialog() == DialogResult.OK) { // ファイル選択でOKが押された場合、選択したファイルパスを取得 string filePath = dialog.FileName; // 選択されたのがGifファイルかチェック string extension = Path.GetExtension(filePath).ToUpperInvariant(); if (!AllowExtensionGif.Split(',').Any(allowExtension => allowExtension.ToUpperInvariant().Equals(extension, StringComparison.Ordinal))) { // チェックOKの場合、選択したファイルのパスを設定する TxtAppendGif.Text = filePath; } else { // チェックNGの場合、エラーメッセージを表示する MessageBox.ShowAttention(this, Resources.CombineImagesGifFileErrorNotGif); } } } // 画面の表示設定を行う SetControlDisplaySetting(); }
/// <summary> /// 実行ボタン押下 /// </summary> /// <param name="sender">センダーオブジェクト</param> /// <param name="e">イベントデータ</param> private async void BtRun_Click(object sender, EventArgs e) { // プログレスバーをリセット ProgressBarCreateGif.Value = 0; // 画像データ格納フォルダが選択されているかチェック string imageDirectory = TxtImageDirectory.Text; if (string.IsNullOrWhiteSpace(imageDirectory) || imageDirectory.Equals(initializeImageDirectoryText, StringComparison.Ordinal)) { // フォルダが選択されていない場合は、メッセージを表示し処理をしない MessageBox.ShowAttention(this, Resources.CombineImagesDirectoryNotSelect); return; } // 画像データ格納フォルダに格納されている画像データのパスリストを取得する string[] filePaths = Directory.GetFiles(TxtImageDirectory.Text); // 選択されたフォルダに画像データが存在するかチェック if (!RunCheckImageFileExists(filePaths)) { // チェック結果がNGの場合は処理をしない // (メッセージの表示はチェックメソッドにて実施) return; } // 追記を行うか判定する string gifFilePath = TxtAppendGif.Text; bool isAppendGif = !string.IsNullOrWhiteSpace(gifFilePath) && !gifFilePath.Equals(initializeAppendGifText, StringComparison.Ordinal); // 追記を行う場合は、追記元のGifファイルが存在するかチェックを行う if (isAppendGif && !File.Exists(gifFilePath)) { // ファイルが存在しない場合は、メッセージを表示し処理をしない MessageBox.ShowAttention(this, Resources.CombineImagesGifFileErrorNoGif); return; } // 追記を行わない場合は、ファイル保存ダイアログを開き、保存先のパスを設定する if (!isAppendGif) { SaveFileDialog dialog; using (dialog = new SaveFileDialog()) { // ファイル保存ダイアログ dialog.Title = Resources.RunSaveFileDialogTitle; dialog.Filter = Resources.RunSaveFileDialogFilter; dialog.FilterIndex = 2; dialog.RestoreDirectory = true; // ファイル保存ダイアログを表示 if (dialog.ShowDialog() == DialogResult.OK) { // 保存先が選択された場合、そのパスを保持する gifFilePath = dialog.FileName; } else { // 保存先の選択がキャンセルされた場合、処理を終了する return; } } // 保存先パスから拡張子情報を取得し、拡張子情報がgifの拡張子か判定する string extension = Path.GetExtension(gifFilePath); if (!extension.ToUpperInvariant().Equals( ".gif".ToUpperInvariant(), StringComparison.Ordinal)) { // 拡張子がGifの形式でない場合、 // 生成したGifの保存先のパスに「.gif」の拡張子を追加する gifFilePath += ".gif"; } } // コントロールを無効にする SetEnableForInputControl(false); // 実行中フラグを ON にする IsRuning = true; // ファイル名を名前順にソートする string[] sortFilePaths = filePaths.OrderBy(file => file).ToArray(); // 画像データ連結処理を実行(別タスクで実行) bool result = false; await Task.Run(() => { result = Run(sortFilePaths, gifFilePath, isAppendGif); }).ConfigureAwait(true); // 正常終了の場合、メッセージを表示する if (result) { MessageBox.ShowInfo(Resources.RunProcessEndMessage); } // 実行中フラグを OFF にする IsRuning = false; // コントロールを有効にする SetEnableForInputControl(true); }