/// <summary> /// Create empty image button click hander /// Create an empty image and prompt the user to save the file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void BtnCreateEmptyImg_Click(object sender, EventArgs e) { //Parse width and height if (!ValidateDimensions(TBEmptyImgWidth, TBEmptyImgHeight, out int width, out int height)) { return; } //Set file type SaveFileDialogMain.FilterIndex = ComboBoxOutFormat.SelectedIndex + 1; //Show dialogue if (SaveFileDialogMain.ShowDialog() == DialogResult.OK) { //Lock UI GroupBoxCreateEmptyImg.Enabled = false; //OK clicked, save file bool success = await Task.Run(() => { return(ImLib.ImEmpty(width, height, SaveFileDialogMain.FileName, IOLib.ParseFormat(SaveFileDialogMain.FilterIndex - 1), true)); }); //Launch directory if successful and checkbox checked if (success && CBLaunchWhenDone.Checked) { await Task.Run(() => { Process.Start(Path.GetDirectoryName(SaveFileDialogMain.FileName)); }); } //Log PutLog((success ? "Created" : "ERROR: Failed to create") + " empty image of size " + width.ToString() + "x" + height.ToString() + " to " + SaveFileDialogMain.FileName); //Unlock UI GroupBoxCreateEmptyImg.Enabled = true; } }
/// <summary> /// Validate dimension inputs /// </summary> /// <param name="width">The width output variable</param> /// <param name="height">The height output varialbe</param> /// <returns>True if the operation was successful, false otherwise</returns> private bool ValidateDimensions(TextBox tbWidth, TextBox tbHeight, out int width, out int height) { if (!ImLib.ValidateDimension(tbWidth.Text, out width)) { MessageBox.Show("The width entered is not valid. "); tbWidth.Focus(); height = -1; return(false); } if (!ImLib.ValidateDimension(tbHeight.Text, out height)) { MessageBox.Show("The height entered is not valid. "); tbHeight.Focus(); return(false); } return(true); }
/// <summary> /// The core code of this software /// </summary> private async Task ProcessImages() { //Check input directory string dirIn = TBDirIn.Text; if (!Directory.Exists(dirIn)) { MessageBox.Show("Input directory does not exist. "); TBDirIn.Focus(); return; } //Check output directory string dirOut = TBDirOut.Text; if (!Directory.Exists(dirOut)) { if (MessageBox.Show("Output directory does not exist, would you like to create it? ", "", MessageBoxButtons.YesNo) == DialogResult.Yes) { bool dirCreateResult = await Task.Run(() => { try { Directory.CreateDirectory(dirOut); } catch (Exception err) when(err is IOException || err is UnauthorizedAccessException || err is ArgumentException || err is ArgumentNullException || err is PathTooLongException || err is DirectoryNotFoundException || err is NotSupportedException) { MessageBox.Show("Could not create output directory. "); return(false); } return(true); }); //Check if directory was successfully created if (!dirCreateResult) { TBDirOut.Focus(); return; } } else { TBDirOut.Focus(); return; } } //Check resize dimensions bool resize = CBResize.Checked; bool noUpscale = CBNoUpscale.Checked; int width = -1; int height = -1; if (resize) { if (!ValidateDimensions(TBResizeWidth, TBResizeHeight, out width, out height)) { return; } } //Check file names bool keepFileName = CBKeepFileName.Checked; string fileNamePrefix = TBRenamePrefix.Text; string fileNameSuffix = TBRenameSuffix.Text; if (!keepFileName) { if (!IOLib.CheckFileName(fileNamePrefix)) { MessageBox.Show("File name prefix is not valid. "); GroupBoxConvert.Enabled = true; TBRenamePrefix.Focus(); return; } if (!IOLib.CheckFileName(fileNameSuffix)) { MessageBox.Show("File name suffix is not valid. "); TBRenameSuffix.Focus(); GroupBoxConvert.Enabled = true; return; } } //Get output format ImageFormat format = IOLib.ParseFormat(ComboBoxOutFormat.SelectedIndex); //Start processing int total = 0; int error = 0; await Task.Run(() => { //Get potential files to process string[] files = Directory.GetFiles(dirIn); //Find files that are images List <string> validFiles = new List <string>(); for (int i = 0; i < files.Length; i++) { if (IOLib.FormatCanRead(Path.GetExtension(files[i]))) { validFiles.Add(files[i]); } } //Write file count Interlocked.Add(ref total, validFiles.Count); //Start processing each image Parallel.For(0, validFiles.Count, (i) => { //Load the image Image img; if (!ImLib.ImLoad(validFiles[i], out img, false)) { PutLog("ERROR: Could not read " + validFiles[i]); //Update counter Interlocked.Add(ref error, 1); return; } //Scale the image Image scaledImg = new Bitmap(1, 1); if (resize) { if (!ImLib.ImScale(img, width, height, "", out scaledImg, noUpscale, false)) { PutLog("ERROR: Could not allocate memory to process " + validFiles[i]); img.Dispose(); return; } else { img.Dispose(); } } //Find output file name string outFile; if (keepFileName) { outFile = Path.Combine(dirOut, Path.GetFileNameWithoutExtension(validFiles[i]) + IOLib.FormatToString(format)); } else { outFile = Path.Combine(dirOut, fileNamePrefix + i.ToString() + fileNameSuffix + IOLib.FormatToString(format)); } //Convert and write out if (!ImLib.ImSave(resize ? scaledImg : img, outFile, format, false)) { PutLog("ERROR: Could not write to " + outFile); } else { PutLog("Processed " + validFiles[i] + " and is saved to " + outFile); } }); }); //Tell the user that we finished MessageBox.Show("Processing finished, successfully proccessed " + (total - error).ToString() + " images out of " + total.ToString() + ". Please check the log for more details. "); if (CBLaunchWhenDone.Checked) { await Task.Run(() => { Process.Start(dirOut); }); } }