diff --git a/MaddoShared/ImageCreationStuff.cs b/MaddoShared/ImageCreationStuff.cs index 9df3308..9ab89cc 100644 --- a/MaddoShared/ImageCreationStuff.cs +++ b/MaddoShared/ImageCreationStuff.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; +using System.Drawing; using System.IO; using System.Linq; using System.Text; @@ -57,12 +58,23 @@ namespace MaddoShared { List dataToProcess = GetFilesToProcess(options); - int threads = options.MaxThreads == 0 ? Environment.ProcessorCount * 2 : options.MaxThreads; - + // int threads = options.MaxThreads == 0 ? Environment.ProcessorCount * 2 : options.MaxThreads; + int threads = options.MaxThreads; + + Bitmap logoBmp = null; + // Load Logo + if (PicSettings.LogoAggiungi & File.Exists(PicSettings.LogoNomeFile)) + { + logoBmp = new Bitmap(PicSettings.LogoNomeFile); + } + Func processFile = async fileData => { + using var logoCopy = logoBmp.Clone(new Rectangle(0, 0, logoBmp.Width, logoBmp.Height), + logoBmp.PixelFormat); + using var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory); - await imgCreator.CreaImmagineThread(fileData.File.Name); + await imgCreator.CreaImmagineThread(fileData.File.Name, logoCopy); results.Add(fileData.File.Name); @@ -104,6 +116,16 @@ namespace MaddoShared GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: false, compacting: false); } } + + try + { + logoBmp?.Dispose(); + } + catch (Exception e) + { + logger.LogError(e, "Error in disposing the logo"); + } + } private List GetFilesToProcess(Options options) @@ -150,156 +172,156 @@ namespace MaddoShared }).ToList(); } - public async Task CreaImmaginiParallel(Options options, ConcurrentBag results, - EventHandler> updateEvent, - CancellationToken cancellationToken = default(CancellationToken)) - { - var dataToProcess = new List(); - if (options.AggiornaSottodirectory && options.CreaSottocartelle) - { - var helper = new FileHelperSharp(); - dataToProcess = helper.GetFilesRecursive(new DirectoryInfo(options.SourcePath), - new DirectoryInfo(options.DestinationPath), - "*.jpg", new FileHelperOptions() - { - FilesPerFolder = options.FilePerCartella, - Suffix = options.SuffissoCartelle, - CounterSize = options.CifreContatore, - NumerationType = options.NumerazioneType - }); - } - else if (!options.CreaSottocartelle) - { - var files = Directory.EnumerateFiles(options.SourcePath, "*.jpg", - options.AggiornaSottodirectory - ? SearchOption.AllDirectories - : SearchOption.TopDirectoryOnly); - - dataToProcess = files.Select(x => - { - var fInfo = new FileInfo(x); - var filePath = fInfo.DirectoryName; - var trimmedSourcePath = options.SourcePath.TrimEnd('\\'); - var newFilePath = fInfo.FullName.Replace(trimmedSourcePath, "").TrimStart('\\'); - newFilePath = Path.Combine(options.DestinationPath, newFilePath); - - var destFolderPath = new FileInfo(newFilePath).DirectoryName; - var destFolderInfo = new DirectoryInfo(destFolderPath); - destFolderInfo.EnsureDirectoryExists(); - - return new FileData(fInfo, new DirectoryInfo(new FileInfo(newFilePath).DirectoryName)); - }).ToList(); - - //// TODO - //dataToProcess = - // (from f in Directory.EnumerateFiles(options.SourcePath, "*.jpg", - // options.AggiornaSottodirectory - // ? SearchOption.AllDirectories - // : SearchOption.TopDirectoryOnly) - // select new FileData(new FileInfo(f), - // new DirectoryInfo(options.DestinationPath.PathCombine( - // new FileInfo(f).DirectoryName.Replace(options.SourcePath.TrimEnd(new char[] {'\\'}), "") - // ) - // ) - // ) - // ) - // .ToList(); - } - - var threads = options.MaxThreads == 0 ? Environment.ProcessorCount * 2 : options.MaxThreads; - var scheduler = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, threads) - .ConcurrentScheduler; - - //var allTasks = new List(); - var test = from d in dataToProcess - select Task.Factory.StartNew(async () => - { - var imgCreator = new ImageCreatorSharp(d.File, d.Directory); - await imgCreator.CreaImmagineThread(d.File.Name); - - //await new ImageCreatorSharp(d.File, d.Directory).CreaImmagineThread(d.File.Name); - - imgCreator = null; - - //var imgC = new ImageCreatorSharp(d.File, d.Directory); - //imgC.CreaImmagineThread(d.File.Name); - //imgC = null; - }, CancellationToken.None, TaskCreationOptions.None, scheduler); - - //int count = 0; - - if (options.LinearExecution) - { - foreach (var task in test) - { - await task; - } - } - else - { - if (options.ChunksSize == 0) - { - //var opts = new ParallelOptions() { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken, TaskScheduler = scheduler}; - await dataToProcess.ParallelForEachAsync(async fileData => - { - var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory); - await imgCreator.CreaImmagineThread(fileData.File.Name); - // await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread(fileData.File.Name); - results.Add(fileData.File.Name); - //count = Interlocked.Increment(ref count); - - try - { - updateEvent?.Invoke(this, new Tuple(fileData.File.Name, dataToProcess.Count)); - } - catch (Exception e) - { - logger.LogError(e, "Error in reporting update"); - //Console.WriteLine(e); - throw; - } - finally - { - // Disposing of the creator - imgCreator = null; - } - }, maxDegreeOfParallelism: threads, false, cancellationToken); - } - else - { - var asdf = SplitList(dataToProcess.ToList(), options.ChunksSize).ToList(); - - - foreach (var sdaf in asdf) - { - await sdaf.ParallelForEachAsync(async fileData => - { - var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory); - await imgCreator.CreaImmagineThread(fileData.File.Name); - // await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread( - // fileData.File.Name); - results.Add(fileData.File.Name); - //count = Interlocked.Increment(ref count); - try - { - updateEvent?.Invoke(this, - new Tuple(fileData.File.Name, dataToProcess.Count)); - } - catch (Exception e) - { - logger.LogError("Error in reporting update"); - //Console.WriteLine(e); - throw; - } - finally - { - imgCreator = null; - } - }, maxDegreeOfParallelism: threads, false, cancellationToken); - } - } - } - } + // public async Task CreaImmaginiParallel(Options options, ConcurrentBag results, + // EventHandler> updateEvent, + // CancellationToken cancellationToken = default(CancellationToken)) + // { + // var dataToProcess = new List(); + // if (options.AggiornaSottodirectory && options.CreaSottocartelle) + // { + // var helper = new FileHelperSharp(); + // dataToProcess = helper.GetFilesRecursive(new DirectoryInfo(options.SourcePath), + // new DirectoryInfo(options.DestinationPath), + // "*.jpg", new FileHelperOptions() + // { + // FilesPerFolder = options.FilePerCartella, + // Suffix = options.SuffissoCartelle, + // CounterSize = options.CifreContatore, + // NumerationType = options.NumerazioneType + // }); + // } + // else if (!options.CreaSottocartelle) + // { + // var files = Directory.EnumerateFiles(options.SourcePath, "*.jpg", + // options.AggiornaSottodirectory + // ? SearchOption.AllDirectories + // : SearchOption.TopDirectoryOnly); + // + // dataToProcess = files.Select(x => + // { + // var fInfo = new FileInfo(x); + // var filePath = fInfo.DirectoryName; + // var trimmedSourcePath = options.SourcePath.TrimEnd('\\'); + // var newFilePath = fInfo.FullName.Replace(trimmedSourcePath, "").TrimStart('\\'); + // newFilePath = Path.Combine(options.DestinationPath, newFilePath); + // + // var destFolderPath = new FileInfo(newFilePath).DirectoryName; + // var destFolderInfo = new DirectoryInfo(destFolderPath); + // destFolderInfo.EnsureDirectoryExists(); + // + // return new FileData(fInfo, new DirectoryInfo(new FileInfo(newFilePath).DirectoryName)); + // }).ToList(); + // + // //// TODO + // //dataToProcess = + // // (from f in Directory.EnumerateFiles(options.SourcePath, "*.jpg", + // // options.AggiornaSottodirectory + // // ? SearchOption.AllDirectories + // // : SearchOption.TopDirectoryOnly) + // // select new FileData(new FileInfo(f), + // // new DirectoryInfo(options.DestinationPath.PathCombine( + // // new FileInfo(f).DirectoryName.Replace(options.SourcePath.TrimEnd(new char[] {'\\'}), "") + // // ) + // // ) + // // ) + // // ) + // // .ToList(); + // } + // + // var threads = options.MaxThreads == 0 ? Environment.ProcessorCount * 2 : options.MaxThreads; + // var scheduler = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, threads) + // .ConcurrentScheduler; + // + // //var allTasks = new List(); + // var test = from d in dataToProcess + // select Task.Factory.StartNew(async () => + // { + // var imgCreator = new ImageCreatorSharp(d.File, d.Directory); + // await imgCreator.CreaImmagineThread(d.File.Name); + // + // //await new ImageCreatorSharp(d.File, d.Directory).CreaImmagineThread(d.File.Name); + // + // imgCreator = null; + // + // //var imgC = new ImageCreatorSharp(d.File, d.Directory); + // //imgC.CreaImmagineThread(d.File.Name); + // //imgC = null; + // }, CancellationToken.None, TaskCreationOptions.None, scheduler); + // + // //int count = 0; + // + // if (options.LinearExecution) + // { + // foreach (var task in test) + // { + // await task; + // } + // } + // else + // { + // if (options.ChunksSize == 0) + // { + // //var opts = new ParallelOptions() { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken, TaskScheduler = scheduler}; + // await dataToProcess.ParallelForEachAsync(async fileData => + // { + // var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory); + // await imgCreator.CreaImmagineThread(fileData.File.Name); + // // await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread(fileData.File.Name); + // results.Add(fileData.File.Name); + // //count = Interlocked.Increment(ref count); + // + // try + // { + // updateEvent?.Invoke(this, new Tuple(fileData.File.Name, dataToProcess.Count)); + // } + // catch (Exception e) + // { + // logger.LogError(e, "Error in reporting update"); + // //Console.WriteLine(e); + // throw; + // } + // finally + // { + // // Disposing of the creator + // imgCreator = null; + // } + // }, maxDegreeOfParallelism: threads, false, cancellationToken); + // } + // else + // { + // var asdf = SplitList(dataToProcess.ToList(), options.ChunksSize).ToList(); + // + // + // foreach (var sdaf in asdf) + // { + // await sdaf.ParallelForEachAsync(async fileData => + // { + // var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory); + // await imgCreator.CreaImmagineThread(fileData.File.Name); + // // await new ImageCreatorSharp(fileData.File, fileData.Directory).CreaImmagineThread( + // // fileData.File.Name); + // results.Add(fileData.File.Name); + // //count = Interlocked.Increment(ref count); + // try + // { + // updateEvent?.Invoke(this, + // new Tuple(fileData.File.Name, dataToProcess.Count)); + // } + // catch (Exception e) + // { + // logger.LogError("Error in reporting update"); + // //Console.WriteLine(e); + // throw; + // } + // finally + // { + // imgCreator = null; + // } + // }, maxDegreeOfParallelism: threads, false, cancellationToken); + // } + // } + // } + // } public static IEnumerable> SplitList(List bigList, int nSize = 3) { diff --git a/MaddoShared/ImageCreatorSharp.cs b/MaddoShared/ImageCreatorSharp.cs index d177ceb..acf7f1a 100644 --- a/MaddoShared/ImageCreatorSharp.cs +++ b/MaddoShared/ImageCreatorSharp.cs @@ -85,7 +85,7 @@ public class ImageCreatorSharp : IDisposable this.DestDir = destination; } - public async Task CreaImmagineThread(string Info) + public async Task CreaImmagineThread(string Info, Image logo) { try { @@ -120,7 +120,7 @@ public class ImageCreatorSharp : IDisposable AggiungiTesto(g, imgOutputBig); - aggiungiLogo(imgOutputBig); + aggiungiLogo(imgOutputBig, logo); SalvaFoto(imgOutputBig, thumbSizeBig, nomeFileBig, nomeFileSmall, thumbSizeSmall, thisFormat); }); @@ -676,19 +676,14 @@ public class ImageCreatorSharp : IDisposable - private void aggiungiLogo(Bitmap imgOutputBig) + private void aggiungiLogo(Bitmap imgOutputBig, Image logo) { // imgOutputBig if (PicSettings.LogoAggiungi == true & File.Exists(PicSettings.LogoNomeFile)) { - using var ImmagineLogo = Image.FromFile(PicSettings.LogoNomeFile); + // using var ImmagineLogo = Image.FromFile(PicSettings.LogoNomeFile); Color LogoColoreTrasparente = Color.White; - // Dim bmWatermark As Bitmap - - // * Create a Bitmap based on the previously modified photograph Bitmap - // bmWatermark = New Bitmap(imgOutputBig) - // bmWatermark.SetResolution(imgOutputBig.HorizontalResolution, imgOutputBig.VerticalResolution) // * Load this Bitmap into a new Graphic Object using Graphics grWatermark = Graphics.FromImage(imgOutputBig); @@ -711,13 +706,13 @@ public class ImageCreatorSharp : IDisposable int FotoLogoH = PicSettings.LogoAltezza; int FotoLogoW = PicSettings.LogoLarghezza; - double FattoreAlt = ImmagineLogo.Height / (double)FotoLogoH; - double FattoreLarg = ImmagineLogo.Width / (double)FotoLogoW; + double FattoreAlt = logo.Height / (double)FotoLogoH; + double FattoreLarg = logo.Width / (double)FotoLogoW; Size NuovaSize; if (FattoreLarg > FattoreAlt) - NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.Height, FotoLogoW, "Larghezza"); + NuovaSize = NewthumbSize(logo.Width, logo.Height, FotoLogoW, "Larghezza"); else - NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.Height, FotoLogoH, "Altezza"); + NuovaSize = NewthumbSize(logo.Width, logo.Height, FotoLogoH, "Altezza"); int MargineUsato; int MargineL; @@ -777,7 +772,7 @@ public class ImageCreatorSharp : IDisposable } } - grWatermark.DrawImage(ImmagineLogo, new Rectangle(xPosOfWm, yPosOfWm, NuovaSize.Width, NuovaSize.Height), 0, 0, ImmagineLogo.Width, ImmagineLogo.Height, GraphicsUnit.Pixel, imageAttributes); + grWatermark.DrawImage(logo, new Rectangle(xPosOfWm, yPosOfWm, NuovaSize.Width, NuovaSize.Height), 0, 0, logo.Width, logo.Height, GraphicsUnit.Pixel, imageAttributes); //grWatermark.Dispose(); } }