public static Guid ComputeOverlay(string sourceFilePath) { FileContainer fileContainer = FileContainer.NewOverlayContainer(sourceFilePath); var processStartInfo = new ProcessStartInfo(); processStartInfo.WorkingDirectory = TempFileManager.GetTempDirectory(); processStartInfo.UseShellExecute = false; processStartInfo.ErrorDialog = false; processStartInfo.CreateNoWindow = true; processStartInfo.WindowStyle = ProcessWindowStyle.Hidden; string oldFilePath = fileContainer.SourceFileItem.FilePath; string outputFilePath = null; try { LogManager.AddOverlayMessage("SourceFileName " + Path.GetFileName(oldFilePath), "Start Crop"); // resize + crop source image outputFilePath = Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".png"); processStartInfo.FileName = Path.Combine(FrontSettings.ImageMagickPath, "convert"); processStartInfo.Arguments = $"{Path.GetFileName(fileContainer.SourceFileItem.FilePath)} -resize \"{_finalWidth}x{_finalHeight}^\" -gravity Center -crop {_finalWidth}x{_finalHeight}+0+0 {Path.GetFileName(outputFilePath)}"; StartProcess(processStartInfo, 5000); fileContainer.SourceFileItem.FilePath = outputFilePath; LogManager.AddOverlayMessage("OutputFileName " + Path.GetFileName(outputFilePath), "End Crop"); IpfsDaemon.Queue(fileContainer.SourceFileItem); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(outputFilePath); LogManager.AddOverlayMessage(ex.ToString(), "Exception"); return(fileContainer.ProgressToken); } finally { TempFileManager.SafeDeleteTempFile(oldFilePath); } try { LogManager.AddOverlayMessage("SourceFileName " + Path.GetFileName(fileContainer.SourceFileItem.FilePath), "Start Overlay"); // watermark source image outputFilePath = Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".png"); processStartInfo.FileName = Path.Combine(FrontSettings.ImageMagickPath, "composite"); processStartInfo.Arguments = $"-gravity NorthEast {_overlayImagePath} {Path.GetFileName(fileContainer.SourceFileItem.FilePath)} {Path.GetFileName(outputFilePath)}"; StartProcess(processStartInfo, 5000); fileContainer.OverlayFileItem.FilePath = outputFilePath; LogManager.AddOverlayMessage("OutputFileName " + Path.GetFileName(outputFilePath), "End Overlay"); IpfsDaemon.Queue(fileContainer.OverlayFileItem); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(outputFilePath); LogManager.AddOverlayMessage(ex.ToString(), "Exception"); return(fileContainer.ProgressToken); } return(fileContainer.ProgressToken); }
public static Guid ComputeOverlay(string sourceFilePath, int?x = null, int?y = null) { FileContainer fileContainer = FileContainer.NewOverlayContainer(sourceFilePath); ResizeImage(fileContainer); try { using (Image overlayImage = Image.FromFile(_overlayImagePath)) { using (Image sourceImage = Image.FromFile(fileContainer.SourceFileItem.FilePath)) { using (Graphics graphics = Graphics.FromImage(sourceImage)) { // Si position n'est pas fournie, centrer l'image if (x == null || y == null) { x = (sourceImage.Width / 2) - (overlayImage.Width / 2); y = (sourceImage.Height / 2) - (overlayImage.Height / 2); } graphics.DrawImage(overlayImage, x.Value, y.Value); } string outputFilePath = System.IO.Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".jpeg"); sourceImage.Save(outputFilePath, ImageFormat.Jpeg); fileContainer.OverlayFileItem.FilePath = outputFilePath; } } IpfsDaemon.Queue(fileContainer.OverlayFileItem); } catch (Exception ex) { LogManager.AddOverlayMessage(ex.ToString(), "Exception"); } return(fileContainer.ProgressToken); }
public static Guid ComputeOverlay(string sourceFilePath) { FileContainer fileContainer = FileContainer.NewOverlayContainer(sourceFilePath); var sourceFile = fileContainer.SourceFileItem; try { LogManager.AddOverlayMessage(LogLevel.Information, "SourceFileName " + Path.GetFileName(sourceFile.SourceFilePath), "Start Crop"); // resize + crop source image string arguments = $"{Path.GetFileName(sourceFile.SourceFilePath)} -resize \"{_finalWidth}x{_finalHeight}^\" -gravity Center -crop {_finalWidth}x{_finalHeight}+0+0 {Path.GetFileName(sourceFile.TempFilePath)}"; var process = new ProcessManager(Path.Combine(GeneralSettings.Instance.ImageMagickPath, "convert"), arguments, LogManager.OverlayLogger); bool success = process.Launch(5); if (!success) { TempFileManager.SafeDeleteTempFile(sourceFile.SourceFilePath); TempFileManager.SafeDeleteTempFile(sourceFile.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Error, "Erreur convert", "Erreur"); return(fileContainer.ProgressToken); } sourceFile.SetOutputFilePath(sourceFile.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Information, "OutputFileName " + Path.GetFileName(sourceFile.OutputFilePath), "End Crop"); IpfsDaemon.Instance.Queue(sourceFile); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(sourceFile.SourceFilePath); TempFileManager.SafeDeleteTempFile(sourceFile.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Critical, "Exception non gérée", "Exception", ex); return(fileContainer.ProgressToken); } // remplacement de l'image source string oldSourceFilePath = sourceFile.SourceFilePath; TempFileManager.SafeDeleteTempFile(oldSourceFilePath); sourceFile.SetSourceFilePath(sourceFile.TempFilePath); // changement de la source de OverlayFileItem fileContainer.OverlayFileItem.SetSourceFilePath(sourceFile.SourceFilePath); try { LogManager.AddOverlayMessage(LogLevel.Information, "SourceFileName " + Path.GetFileName(fileContainer.OverlayFileItem.SourceFilePath), "Start Overlay"); // watermark source image string arguments = $"-gravity NorthEast {_overlayImagePath} {Path.GetFileName(fileContainer.OverlayFileItem.SourceFilePath)} {Path.GetFileName(fileContainer.OverlayFileItem.TempFilePath)}"; var process = new ProcessManager(Path.Combine(GeneralSettings.Instance.ImageMagickPath, "composite"), arguments, LogManager.OverlayLogger); bool success = process.Launch(5); if (!success) { TempFileManager.SafeDeleteTempFile(fileContainer.OverlayFileItem.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Error, "Erreur composite", "Erreur"); return(fileContainer.ProgressToken); } fileContainer.OverlayFileItem.SetOutputFilePath(fileContainer.OverlayFileItem.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Information, "OutputFileName " + Path.GetFileName(fileContainer.OverlayFileItem.OutputFilePath), "End Overlay"); IpfsDaemon.Instance.Queue(fileContainer.OverlayFileItem); } catch (Exception ex) { TempFileManager.SafeDeleteTempFile(fileContainer.OverlayFileItem.TempFilePath); LogManager.AddOverlayMessage(LogLevel.Critical, "Exception non gérée", "Exception", ex); return(fileContainer.ProgressToken); } return(fileContainer.ProgressToken); }