protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave( SourceFile source, ImageFileKind destinationKind, ArgumentOptionCollection optionCollection, ILogger logger) { foreach (var savedEmf in this.powerpoint2Emf.ConvertThenSave(source, ImageFileKind.Emf, optionCollection, logger)) { var sourceEmf = savedEmf.AsSourceFile(); foreach (var savedEps in this.emf2Eps.ConvertThenSave(sourceEmf, destinationKind, optionCollection, logger)) { yield return(savedEps); } // Delete tempolary EMF image try { File.Delete(sourceEmf.Path); } catch (Exception e) { logger.WriteLog($"Failed to delete tempolary emf file: {e.Message}", LogLevel.Error); continue; } } }
protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave( SourceFile source, ImageFileKind destinationKind, ArgumentOptionCollection optionCollection, ILogger logger) { var savedImageFiles = new List <DestinationFile>(); try { using (var powerPointApplication = new PowerPointApplication(new Application())) { var application = powerPointApplication.Application; var presentation = application.Presentations; using (var presenationFile = new PowerPointPresentation(presentation.Open( FileName: source.Path, ReadOnly: MsoTriState.msoTrue, Untitled: MsoTriState.msoTrue, WithWindow: MsoTriState.msoTrue))) { var file = presenationFile.Presentation; foreach (var slide in EnumeratePowerPointSlides(file)) { if (!optionCollection.PowerpointPageRange.Contains((uint)slide.SlideNumber)) { continue; } slide.Select(); slide.Shapes.SelectAll(); var selection = application.ActiveWindow.Selection; var shapes = selection.ShapeRange; var destinationPathForSlide = $"{Path.GetDirectoryName(source.Path)}\\{Path.GetFileNameWithoutExtension(source.Path)}{slide.SlideNumber}{destinationKind.GetExtensionWithDot()}"; var destinationForSlide = new DestinationFile(destinationPathForSlide); if (!DeleteExistingFileIfNecessary(destinationForSlide, optionCollection, logger)) { continue; } //create emf shapes.Export(destinationForSlide.Path, PpShapeFormat.ppShapeFormatEMF); savedImageFiles.Add(destinationForSlide); } } } } catch (Exception e) { logger.WriteLog($"Failed to convert powerpoint slide to emf images: {e.Message}\n{e.StackTrace}", LogLevel.Error); } return(savedImageFiles); }
protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave( SourceFile source, ImageFileKind destinationKind, ArgumentOptionCollection optionCollection, ILogger logger) { foreach (var tempEps in this.emf2Eps.ConvertThenSave(source, ImageFileKind.Eps, optionCollection, logger)) { var tempEpsSource = tempEps.AsSourceFile(); foreach (var savedImage in this.eps2Pdf.ConvertThenSave(tempEpsSource, destinationKind, optionCollection, logger)) { yield return(savedImage); } } }
public void TestFromExtension() { Assert.AreEqual(ImageFileKind.Bmp, ImageFileKind.FromExtension(".bmp")); Assert.AreEqual(ImageFileKind.Png, ImageFileKind.FromExtension("png")); Assert.AreEqual(ImageFileKind.Jpg, ImageFileKind.FromExtension(".jpg")); Assert.AreEqual(ImageFileKind.Jpg, ImageFileKind.FromExtension(".jpeg")); Assert.AreEqual(ImageFileKind.Gif, ImageFileKind.FromExtension("Gif")); Assert.AreEqual(ImageFileKind.Tif, ImageFileKind.FromExtension("TIF")); Assert.AreEqual(ImageFileKind.Tif, ImageFileKind.FromExtension(".TIFF")); Assert.AreEqual(ImageFileKind.Wmf, ImageFileKind.FromExtension(".Wmf")); Assert.AreEqual(ImageFileKind.Emf, ImageFileKind.FromExtension("emf")); Assert.AreEqual(ImageFileKind.Eps, ImageFileKind.FromExtension("eps")); Assert.AreEqual(ImageFileKind.Pdf, ImageFileKind.FromExtension(".pdf")); Assert.AreEqual(ImageFileKind.Powerpoint, ImageFileKind.FromExtension(".ppt")); Assert.AreEqual(ImageFileKind.Powerpoint, ImageFileKind.FromExtension(".pptx")); }
/// <summary> /// Delete all file by selected kind. /// </summary> /// <param name="filename">File name.</param> /// <param name="imageFileKind">Image file kind.</param> /// <param name="deleteOriginal">Delete original file</param> public static void DeleteImageFileByKind(string filename, ImageFileKind imageFileKind, bool deleteOriginal = true) { // delete from original if (deleteOriginal) { DeleteFile(Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == ImageFileKind.Original).SingleOrDefault().DirectoryToSave, filename)); } //delete from linked directory foreach (ImageFileDimension ifd in listImageFileDimensions) { if (imageFileKind == ifd.ImageKind) { DeleteFile(Path.Combine(Files.UploadDirectoryPath, ifd.DirectoryToSave, filename)); } } }
public IEnumerable <DestinationFile> ConvertThenSave(SourceFile source, ImageFileKind destinationKind, ArgumentOptionCollection optionCollection, ILogger logger) { if (!this.GetApplicableImageFileKinds().Contains(source.ImageFileKind)) { logger.WriteLog($"Unsuitable source image kind: {source.ImageFileKind}", LogLevel.Error); yield break; } if (!this.GetAvailableDestinationImageFileKinds().Contains(destinationKind)) { logger.WriteLog($"Unsuitable source image kind: {destinationKind}", LogLevel.Error); yield break; } foreach (var savedDestination in this.ConvertApplicableImageThenSave(source, destinationKind, optionCollection, logger)) { yield return(savedDestination); } }
protected override IEnumerable <DestinationFile> ConvertApplicableImageThenSave( SourceFile source, ImageFileKind destinationKind, ArgumentOptionCollection optionCollection, ILogger logger) { var image = Image.GetInstance(source.Path); var destination = source.GetConvertionDestination(destinationKind); if (!DeleteExistingFileIfNecessary(destination, optionCollection, logger)) { yield break; } try { using (var fileStream = new FileStream(destination.Path, FileMode.Create, FileAccess.Write)) { var document = new Document( pageSize: new Rectangle(image.Width, image.Height), marginLeft: 0f, marginRight: 0f, marginTop: 0f, marginBottom: 0f); image.SetAbsolutePosition(0f, 0f); PdfWriter.GetInstance(document, fileStream); document.Open(); document.Add(image); document.Close(); } } catch (Exception e) { logger.WriteLog($"Failed to convert an image: {e.Message}", LogLevel.Error); yield break; } yield return(destination); }
public DestinationFile(string path) { this.ImageFileKind = ImageFileKind.FromExtension(System.IO.Path.GetExtension(path)); this.Path = path; }
public static void UploadImageFile(HttpPostedFile uploadedFile, string newFileName, ImageFileKind fileKind) { Files.MakeDirectoryIfNotExists(Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == fileKind).SingleOrDefault().DirectoryToSave)); uploadedFile.SaveAs(Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == fileKind).SingleOrDefault().DirectoryToSave, newFileName)); }
/// <summary> /// Resize uploaded file to all defined dimensions. /// </summary> /// <param name="origFileName">File upload content.</param> /// <param name="newFileName">New unique file name.</param> /// <param name="imageFileKind">ImageFileKind .</param> public static void ResizeImageFilesByKindOnUpload(HttpPostedFile uploadedFile, string newFileName, ImageFileKind imageFileKind, bool isThumbListItem = false) { UploadImageFile(uploadedFile, newFileName, ImageFileKind.Original); using (Bitmap bitmap = (Bitmap)Bitmap.FromStream(uploadedFile.InputStream)) { foreach (ImageFileDimension ifd in listImageFileDimensions) { if (imageFileKind == ifd.ImageKind) { Files.MakeDirectoryIfNotExists(Path.Combine(Files.UploadDirectoryPath, ifd.DirectoryToSave)); if (ifd.IsCropped) { ResizeImageFile(bitmap, newFileName, ifd); } else { ResizeImageFileWithDimens(bitmap, newFileName, ifd); } } } } }
/// <summary> /// Resize file to all defined dimensions. /// </summary> /// <param name="origFileName">Original file name.</param> /// <param name="newFileName">New unique file name.</param> /// <param name="imageFileKind">ImageFileKind .</param> public static void ResizeImageFilesByKind(string origFileName, string newFileName, ImageFileKind imageFileKind) { string origFullFileName = Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == ImageFileKind.Original).SingleOrDefault().DirectoryToSave, origFileName); using (Bitmap bitmap = (Bitmap)Bitmap.FromFile(origFullFileName)) { foreach (ImageFileDimension ifd in listImageFileDimensions) { if (imageFileKind == ifd.ImageKind) { Files.MakeDirectoryIfNotExists(Path.Combine(Files.UploadDirectoryPath, ifd.DirectoryToSave)); if (ifd.IsCropped) { ResizeImageFile(bitmap, newFileName, ifd); } else { ResizeImageFileWithDimens(bitmap, newFileName, ifd); } } } } //rename original file name to new file name. if (origFileName != newFileName) { string newFullFileName = Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == ImageFileKind.Original).SingleOrDefault().DirectoryToSave, newFileName); FileInfo TheFile = new FileInfo(newFullFileName); if (TheFile.Exists) { File.Delete(origFullFileName); } else { File.Move(origFullFileName, newFullFileName); } } }
/// <exception cref="System.ArgumentNullException">Thrown when originalImgPath or format is null.</exception> /// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown when the image was saved with the wrong image format. Or when the image was saved to the same file it was created from.</exception> public static void SaveCroppedImageByKind(string fileName, Rectangle cropData, ImageFileKind imageKind) { string fileExt = System.IO.Path.GetExtension(fileName); string tempImgPath = Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == ImageFileKind.Temp).SingleOrDefault().DirectoryToSave, fileName); string originalImgPath = Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == ImageFileKind.Original).SingleOrDefault().DirectoryToSave, fileName); using (Bitmap src = new Bitmap(tempImgPath)) { if (src.Width < (cropData.Width + cropData.Left) || src.Height < (cropData.Height + cropData.Top)) { cropData = GetDefaultCrop(cropData, src); } // Resize the bitmap data using (Bitmap result = CropBitmap(src, cropData)) { result.Save(originalImgPath, allowedImageFiles[fileExt]); ResizeImageFilesByKind(fileName, fileName, imageKind); } } DeleteFile(tempImgPath); }
public static void SendImageThumbnailAsStream(Stream stream, string filename, ImageFileKind imageFileKind) { string origFullFileName = Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == ImageFileKind.Original).SingleOrDefault().DirectoryToSave, filename); ImageFileDimension imageFileDimension = listImageFileDimensions.Where(ifd => ifd.ImageKind == imageFileKind).FirstOrDefault(); using (Image bitmap = Bitmap.FromFile(origFullFileName)) { int origHeight = bitmap.Height; int origWidth = bitmap.Width; int newHeight = imageFileDimension.Height.Value; int newWidth = imageFileDimension.Width.Value; float widthRatio = ((float)newWidth) / origWidth; float heightRatio = ((float)newHeight) / origHeight; float ratio = (widthRatio < heightRatio ? widthRatio : heightRatio);// (widthRatio)MIN(maxWidth / Width, maxHeight / Height); if (ratio >= 1.0f) { bitmap.Save(stream, allowedImageFiles[Path.GetExtension(origFullFileName)]); } else { using (Image resultBitmap = bitmap.GetThumbnailImage((int)(origWidth * ratio), (int)(origHeight * ratio), null, IntPtr.Zero)) { resultBitmap.Save(stream, allowedImageFiles[Path.GetExtension(origFullFileName)]); } } } }
protected abstract IEnumerable <DestinationFile> ConvertApplicableImageThenSave( SourceFile source, ImageFileKind destinationKind, ArgumentOptionCollection optionCollection, ILogger logger);
public void TestFromExtension_UnexpectedExtension() { Assert.ThrowsException <InvalidOperationException>(delegate { ImageFileKind.FromExtension(".txt"); }); }