internal void Process() //object sender, DoWorkEventArgs e) { byte[] photoBytes = File.ReadAllBytes(inputFileName); Size size = new Size(150, 0); //If OnImageConversionStart event is being subscribed to, raise it OnImageConversionStart?.Invoke(new ImageOpsEventArgs(inputFileName)); using (MemoryStream inStream = new MemoryStream(photoBytes)) { using (MemoryStream outStream = new MemoryStream()) { // Initialize the ImageFactory using the overload to preserve EXIF metadata. using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true)) { // Load, resize, set the format and quality and save an image. //imageFactory.Load(inStream).Resize(size).Format(format).Save(outStream); imageFactory.Load(inStream).Save(outStream); //imageFactory.Load(inStream).Format(format).Save(outStream); } // Do something with the stream. using (Image img = new Bitmap(outStream)) { using (Image originalFile = new Bitmap(inputFileName)) { foreach (PropertyItem item in originalFile.PropertyItems) { img.SetPropertyItem(item); } img.Save(outputFileName, formatToOutput); } } } } //If OnImageConversionComplete event is being subscribed to, raise it OnImageConversionComplete?.Invoke(new ImageOpsEventArgs(outputFileName)); }
/// <summary> /// Converts images to a different format /// </summary> /// <param name="targetDir">The directory containing the images to be converted</param> /// <param name="includeSubDirs">Specifies whether or not to convert images in subdirectories</param> /// <param name="convertFromFileMasks">An array of strings containing the file extensions of images /// to be converted</param> /// <param name="convertTo">The format to convert images to</param> /// <param name="Iteration"></param> private static void ConvertImages(string targetDir, bool includeSubDirs, string[] convertFromFileMasks, ImageFormat convertTo, bool deleteAfterConversion, FilenameAlreadyExistsOption filenameAlreadyExistsOpt) { string newFilename = null; frmFileExists frmExists = null; string outputExt = null; try { //If the input path ends with a backslash, remove it targetDir = targetDir.Trim(); if (targetDir.EndsWith("\\")) { targetDir.TrimEnd('\\'); } outputExt = GetOutputExtension(convertTo.ToString()).ToLower(); //Loop through each file of the specified type in the specified directory, and convert it foreach (string fileMask in convertFromFileMasks) { foreach (string file in System.IO.Directory.GetFiles(targetDir, fileMask)) { newFilename = Path.GetFileNameWithoutExtension(file) + "." + outputExt; //Check if the new filename already exists if (File.Exists(targetDir + "\\" + newFilename)) { switch (filenameAlreadyExistsOpt) { case FilenameAlreadyExistsOption.Ask: //Display dialog asking user what to do if (frmExists == null) { frmExists = new frmFileExists(targetDir + "\\" + newFilename); } else { frmExists.Filename = targetDir + "\\" + newFilename; } frmExists.ShowDialog(); if (frmExists.AlwaysUseSelectedOption) { if (frmExists.Overwrite) { filenameAlreadyExistsOpt = FilenameAlreadyExistsOption.Overwrite; } else { filenameAlreadyExistsOpt = FilenameAlreadyExistsOption.Skip; } } //If we aren't overwriting the file, move on to the next file if (!frmExists.Overwrite) { continue; } break; case FilenameAlreadyExistsOption.Overwrite: //Original image will be overwritten in call to Save() below -- keep going break; case FilenameAlreadyExistsOption.Skip: continue; //Skip this image -- move on to the next iteration of the loop } } //If OnImageConversionStart event is being subscribed to, raise it OnImageConversionStart?.Invoke(new ImageOpsEventArgs(file)); //get the metadata from the original file var gps = ImageMetadataReader.ReadMetadata(file); //var gps = ImageMetadataReader.ReadMetadata(path).OfType<GpsDirectory>().FirstOrDefault(); byte[] photoBytes = File.ReadAllBytes(file); // Format is automatically detected though can be changed. ISupportedImageFormat format = GetFormatFromSelectedOutput(convertTo); Size size = new Size(150, 0); using (MemoryStream inStream = new MemoryStream(photoBytes)) { using (MemoryStream outStream = new MemoryStream()) { // Initialize the ImageFactory using the overload to preserve EXIF metadata. using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true)) { // Load, resize, set the format and quality and save an image. //imageFactory.Load(inStream).Resize(size).Format(format).Save(outStream); imageFactory.Load(inStream).Save(outStream); //imageFactory.Load(inStream).Format(format).Save(outStream); } // Do something with the stream. using (Image img = new Bitmap(outStream)) { using (Image originalFile = new Bitmap(file)) { foreach (PropertyItem item in originalFile.PropertyItems) { img.SetPropertyItem(item); } img.Save(targetDir + "\\" + newFilename, convertTo); } } } } //Create and save new image file //using (Image img = new Bitmap(file)) //{ // img.Save(targetDir + "\\" + newFilename, convertTo); //} //Delete the original image if so specified if (deleteAfterConversion) { File.Delete(file); } //If OnImageConversionComplete event is being subscribed to, raise it OnImageConversionComplete?.Invoke(new ImageOpsEventArgs(newFilename)); } //foreach file in the directory } //foreach filemask //Including subdirectories? if (includeSubDirs) { foreach (string subdir in System.IO.Directory.GetDirectories(targetDir)) { ConvertImages(subdir, true, convertFromFileMasks, convertTo, deleteAfterConversion, filenameAlreadyExistsOpt); } } } finally { if (frmExists != null) { frmExists.Dispose(); } } }