From fa889a5cd317e651545d5554ff5d1f4d17fd2fcd Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 28 Jul 2025 11:25:46 +0200 Subject: [PATCH] Refactored image creator code --- MaddoShared/ImageCreationStuff.cs | 14 +- MaddoShared/ImageCreatorSharp.cs | 1069 ++++++++++++----------------- MaddoShared/ImageState.cs | 45 ++ MaddoShared/MaddoShared.csproj | 4 + imagecatalog/Program.cs | 1 + 5 files changed, 515 insertions(+), 618 deletions(-) create mode 100644 MaddoShared/ImageState.cs diff --git a/MaddoShared/ImageCreationStuff.cs b/MaddoShared/ImageCreationStuff.cs index 22276a8..66d03cc 100644 --- a/MaddoShared/ImageCreationStuff.cs +++ b/MaddoShared/ImageCreationStuff.cs @@ -13,7 +13,7 @@ using Microsoft.Extensions.Logging; namespace MaddoShared { - public class ImageCreationStuff(ILogger logger, PicSettings picSettings) + public class ImageCreationStuff(ILogger logger, PicSettings picSettings, ImageCreatorSharp imageCreatorService) { public class Options { @@ -72,8 +72,16 @@ namespace MaddoShared using var logoCopy = logoBmp.Clone(new Rectangle(0, 0, logoBmp.Width, logoBmp.Height), logoBmp.PixelFormat); - using var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory, picSettings); - await imgCreator.CreaImmagineThread(fileData.File.Name, logoCopy); + var imgState = new ImageState + { + WorkFile = fileData.File, + DestDir = fileData.Directory, + }; + + await imageCreatorService.CreaImmagineThread(imgState, logoCopy); + + // using var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory, picSettings); + // await imgCreator.CreaImmagineThread(fileData.File.Name, logoCopy); results.Add(fileData.File.Name); diff --git a/MaddoShared/ImageCreatorSharp.cs b/MaddoShared/ImageCreatorSharp.cs index ef78f98..3837318 100644 --- a/MaddoShared/ImageCreatorSharp.cs +++ b/MaddoShared/ImageCreatorSharp.cs @@ -1,129 +1,59 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Security; -using System.Text; -using System.Threading.Tasks; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; -using MaddoShared; +using System.IO; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using SixLabors.ImageSharp.Metadata.Profiles.Exif; // Imports System.Threading +namespace MaddoShared; + [SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")] -public class ImageCreatorSharp : IDisposable +public class ImageCreatorSharp(PicSettings picSettings, ILogger logger) { - private bool FotoRuotaADestra = false; - private bool FotoRuotaASinistra = false; - - private string TempMinText = ""; - // Private crFont1 As Font - private string _NomeFileChild; - - private DirectoryInfo _SourceDir; - private DirectoryInfo _DestDirStart; - private DirectoryInfo _DestDir; - - private FileInfo _workFile; - - private string testoFirma; - private string testoFirmaV; - private int alphaScelta; - private int DimensioneStandard; - private int DimensioneStandardMiniatura; - private DateTime dataFoto; - private DateTime dataPartenzaI; - private string testoOrario; - private string testoFirmaPiccola; - private Size thumbSizeSmall; - private Size thumbSizeBig; - private string nomeFileSmall; - private string nomeFileBig; - private string nomeFileBig2; - - private float yPosFromBottom; - private float yPosFromBottom1; - private float yPosFromBottom2; - private float yPosFromBottom3; - private float yPosFromBottom4; - - private Orientations _orientation; - private DateTime? _creationDate; - - private readonly PicSettings _picSettings; - - public ImageCreatorSharp() - { - } - - public ImageCreatorSharp(string nomeFileChild, DirectoryInfo sourceDir, DirectoryInfo destDir, DirectoryInfo destDirStart) - { - this.NomeFileChild = nomeFileChild; - this.SourceDir = sourceDir; - this.DestDir = destDir; - this.DestDirStart = destDirStart; - this.WorkFile = new FileInfo(nomeFileChild); - } - - public ImageCreatorSharp(string nomeFileChild, DirectoryInfo sourceDir, DirectoryInfo destDir) - { - this.NomeFileChild = nomeFileChild; - this.DestDir = destDir; - } - - public ImageCreatorSharp(FileInfo file, DirectoryInfo destination, PicSettings picSettings) - { - _picSettings = picSettings; - this.WorkFile = file; - this.DestDir = destination; - } - - public async Task CreaImmagineThread(string Info, Image logo) + public async Task CreaImmagineThread(ImageState imgState, Image logo) { + try { await Task.Run(() => { - Console.WriteLine($"File: {WorkFile} Dest: {DestDir}"); - preparaVariabili(); - ExtractExif(); - // Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Path.Combine(SourceDir.FullName, NomeFileChild)) - using Image g = Image.FromFile(WorkFile.FullName); - // Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(WorkFile.FullName) + logger.LogInformation("File: {FileInfo} Dest: {DirectoryInfo}", imgState.WorkFile, imgState.DestDir); + PreparaVariabili(imgState); + ExtractExif(imgState); + + using var g = Image.FromFile(imgState.WorkFile.FullName); // Imposta testo extra - impostaTestoExtra(g); + ImpostaTestoExtra(g, imgState); // Ruota l'immagine in base ai dati EXIF - Rotation(g); + Rotation(g, imgState); // Forza jpeg se è selezionata l'opzione - System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat; - if (_picSettings.UsaForzaJpg == true) - thisFormat = System.Drawing.Imaging.ImageFormat.Jpeg; + var thisFormat = g.RawFormat; + if (picSettings.UsaForzaJpg == true) + thisFormat = ImageFormat.Jpeg; - prepareThumbnailSize(g); + PrepareThumbnailSize(g, imgState); + + using var imgOutputBig = new Bitmap(g, imgState.ThumbSizeBig.Width, imgState.ThumbSizeBig.Height); - using Bitmap imgOutputBig = new Bitmap(g, thumbSizeBig.Width, thumbSizeBig.Height); - //Bitmap imgOutputBig = new Bitmap(g, thumbSizeBig.Width, thumbSizeBig.Height); imgOutputBig.SetResolution(g.HorizontalResolution, g.VerticalResolution); // Crea le miniature - creaMiniature(g, imgOutputBig, thisFormat); + CreaMiniature(g, imgState, imgOutputBig, thisFormat); - AggiungiTesto(g, imgOutputBig); + AggiungiTesto(g, imgState, imgOutputBig); - aggiungiLogo(imgOutputBig, logo); + AggiungiLogo(imgOutputBig, imgState, logo); - SalvaFoto(imgOutputBig, thumbSizeBig, nomeFileBig, nomeFileSmall, thumbSizeSmall, thisFormat); + SalvaFoto(imgOutputBig, imgState, thisFormat); }); // g.Dispose() @@ -142,10 +72,10 @@ public class ImageCreatorSharp : IDisposable } } - private void ExtractExif() + private void ExtractExif(ImageState imgState) { - using var img = SixLabors.ImageSharp.Image.Load(_workFile.FullName); - _orientation = Orientations.TopLeft; + using var img = SixLabors.ImageSharp.Image.Load(imgState.WorkFile.FullName); + imgState.Orientation = Orientations.TopLeft; IExifValue rotation = null; @@ -154,7 +84,7 @@ public class ImageCreatorSharp : IDisposable if (found ) { var intOrientation = rotation.Value.ToInt32(); - _orientation = (Orientations)intOrientation; + imgState.Orientation = (Orientations)intOrientation; } IExifValue date = null; @@ -164,37 +94,37 @@ public class ImageCreatorSharp : IDisposable var succ = DateTime.TryParse(date.ToString(), out var crDate); if (succ) { - _creationDate = crDate; + imgState.CreationDate = crDate; } else { - _creationDate = null; + imgState.CreationDate = null; } } else { - _creationDate = null; + imgState.CreationDate = null; } } - private void Rotation(System.Drawing.Image g) + private void Rotation(System.Drawing.Image g, ImageState imgState) { - FotoRuotaADestra = false; - FotoRuotaASinistra = false; + imgState.FotoRuotaADestra = false; + imgState.FotoRuotaASinistra = false; - if (_picSettings.UsaRotazioneAutomatica == true) + if (picSettings.UsaRotazioneAutomatica == true) { if (g.PropertyIdList.Length > 0) { //ExifReader DatiExif = new ExifReader((Bitmap)g); - switch (_orientation /*DatiExif.Orientation*/) + switch (imgState.Orientation /*DatiExif.Orientation*/) { case Orientations.BottomLeft: case Orientations.BottomRight: case Orientations.LeftTop: case Orientations.LftBottom: - FotoRuotaASinistra = true; + imgState.FotoRuotaASinistra = true; break; case Orientations.RightBottom: case Orientations.RightTop: @@ -206,52 +136,50 @@ public class ImageCreatorSharp : IDisposable } } - if (FotoRuotaASinistra == true) + if (imgState.FotoRuotaASinistra) g.RotateFlip(RotateFlipType.Rotate270FlipNone); - if (FotoRuotaADestra == true) + if (imgState.FotoRuotaADestra) g.RotateFlip(RotateFlipType.Rotate90FlipNone); } + /// /// ''' Aggiunge Orario, tempo gara e altri /// ''' /// ''' Image + /// /// ''' - private void impostaTestoExtra(Image g) + private void ImpostaTestoExtra(Image g, ImageState imgState) { - if (_picSettings.UsaOrarioTestoApplicare | _picSettings.UsaTempoGaraTestoApplicare | _picSettings.UsaOrarioMiniatura | _picSettings.TestoMin | _picSettings.AggTempoGaraMin | _picSettings.AggNumTempMin) + if (picSettings.UsaOrarioTestoApplicare | picSettings.UsaTempoGaraTestoApplicare | picSettings.UsaOrarioMiniatura | picSettings.TestoMin | picSettings.AggTempoGaraMin | picSettings.AggNumTempMin) { - if (g.PropertyIdList.Length > 0) + if (g.PropertyIdList.Length <= 0) return; + //ExifReader DatiExif = new ExifReader((Bitmap)g); + imgState.DataFoto = imgState.CreationDate ?? DateTime.Now; //DatiExif.DateTimeOriginal; + imgState.TestoFirma = picSettings.TestoFirmaStart; + imgState.TestoFirmaV = picSettings.TestoFirmaStartV; + + if (imgState.DataFoto.Year == 1) return; + imgState.TestoFirmaPiccola = imgState.DataFoto.ToShortTimeString(); + if (picSettings.UsaOrarioTestoApplicare == true) { - //ExifReader DatiExif = new ExifReader((Bitmap)g); - dataFoto = _creationDate ?? DateTime.Now; //DatiExif.DateTimeOriginal; - testoFirma = _picSettings.TestoFirmaStart; - testoFirmaV = _picSettings.TestoFirmaStartV; - - if (dataFoto.Year != 1) - { - testoFirmaPiccola = dataFoto.ToShortTimeString(); - if (_picSettings.UsaOrarioTestoApplicare == true) - { - testoFirma += " " + dataFoto.ToShortDateString() + " " + dataFoto.ToLongTimeString(); - testoFirmaV += " " + dataFoto.ToShortDateString() + " " + dataFoto.ToLongTimeString(); - } - if (_picSettings.UsaTempoGaraTestoApplicare == true) - { - - var diff = dataPartenzaI - dataFoto; - var diffA = diff.TotalSeconds * 10000000; - - TimeSpan Orario = new TimeSpan(0, 0, 0, (int)diffA); - testoFirma += " " + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00"); - testoFirmaV += " " + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00"); - } - } + imgState.TestoFirma += + $" {imgState.DataFoto.ToShortDateString()} {imgState.DataFoto.ToLongTimeString()}"; + imgState.TestoFirmaV += + $" {imgState.DataFoto.ToShortDateString()} {imgState.DataFoto.ToLongTimeString()}"; } + + if (picSettings.UsaTempoGaraTestoApplicare != true) return; + var diff = imgState.DataPartenzaI - imgState.DataFoto; + var diffA = diff.TotalSeconds * 10000000; + + var orario = new TimeSpan(0, 0, 0, (int)diffA); + imgState.TestoFirma += $" {imgState.TestoOrario}{orario.Hours:00}:{orario.Minutes:00}:{orario.Seconds:00}"; + imgState.TestoFirmaV += $" {imgState.TestoOrario}{orario.Hours:00}:{orario.Minutes:00}:{orario.Seconds:00}"; } else { - testoFirma = _picSettings.TestoFirmaStart; - testoFirmaV = _picSettings.TestoFirmaStartV; + imgState.TestoFirma = picSettings.TestoFirmaStart; + imgState.TestoFirmaV = picSettings.TestoFirmaStartV; } } @@ -259,231 +187,225 @@ public class ImageCreatorSharp : IDisposable /// ''' Prepara diverse variabili azzerandole, elaborandole e prendendole dalle impostazioni /// ''' /// ''' - private void preparaVariabili() + private void PreparaVariabili(ImageState imgState) { - alphaScelta = System.Convert.ToInt32((255 * (100 - _picSettings.Trasparenza) / (double)100)); - testoFirma = ""; - testoFirmaV = ""; - dataPartenzaI = _picSettings.DataPartenza; - testoOrario = _picSettings.TestoOrario; - if (testoOrario.Length > 0) - testoOrario += " "; - testoFirmaPiccola = ""; - thumbSizeSmall = new Size(); - thumbSizeBig = new Size(); - nomeFileSmall = ""; - nomeFileBig2 = ""; - nomeFileBig = ""; - DimensioneStandard = _picSettings.DimStandard; - DimensioneStandardMiniatura = _picSettings.DimStandardMiniatura; + imgState.AlphaScelta = System.Convert.ToInt32((255 * (100 - picSettings.Trasparenza) / (double)100)); + imgState.TestoFirma = ""; + imgState.TestoFirmaV = ""; + imgState.DataPartenzaI = picSettings.DataPartenza; + imgState.TestoOrario = picSettings.TestoOrario; + if (imgState.TestoOrario.Length > 0) + imgState.TestoOrario += " "; + imgState.TestoFirmaPiccola = ""; + imgState.ThumbSizeSmall = new Size(); + imgState.ThumbSizeBig = new Size(); + imgState.NomeFileSmall = ""; + imgState.NomeFileBig2 = ""; + imgState.NomeFileBig = ""; + imgState.DimensioneStandard = picSettings.DimStandard; + imgState.DimensioneStandardMiniatura = picSettings.DimStandardMiniatura; // nomeFileSmall = Suffisso & NomeFileChild // nomeFileBig = NomeFileChild - nomeFileSmall = _picSettings.Suffisso + WorkFile.Name; - nomeFileBig = WorkFile.Name; + imgState.NomeFileSmall = picSettings.Suffisso + imgState.WorkFile.Name; + imgState.NomeFileBig = imgState.WorkFile.Name; } - private void prepareThumbnailSize(Image g) + private void PrepareThumbnailSize(Image g, ImageState imgState) { if (g.Width > g.Height) { - thumbSizeSmall = NewthumbSize(g.Width, g.Height, _picSettings.LarghezzaSmall, "Larghezza"); - Size SizeOrig = new Size(g.Width, g.Height); - thumbSizeBig = SizeOrig; + imgState.ThumbSizeSmall = NewthumbSize(g.Width, g.Height, picSettings.LarghezzaSmall, "Larghezza"); + var sizeOrig = new Size(g.Width, g.Height); + imgState.ThumbSizeBig = sizeOrig; } else { - thumbSizeSmall = NewthumbSize(g.Width, g.Height, _picSettings.AltezzaSmall, "Altezza"); - Size SizeOrig = new Size(g.Width, g.Height); - thumbSizeBig = SizeOrig; + imgState.ThumbSizeSmall = NewthumbSize(g.Width, g.Height, picSettings.AltezzaSmall, "Altezza"); + var sizeOrig = new Size(g.Width, g.Height); + imgState.ThumbSizeBig = sizeOrig; } } - private void creaMiniature(Image g, Bitmap imgOutputBig, ImageFormat thisFormat) + private void CreaMiniature(Image g, ImageState imgState, Bitmap imgOutputBig, ImageFormat thisFormat) { - if (_picSettings.TestoMin) - testoFirmaPiccola = nomeFileBig; - else if (_picSettings.AggNumTempMin) - testoFirmaPiccola = nomeFileBig + " "; + if (picSettings.TestoMin) + imgState.TestoFirmaPiccola = imgState.NomeFileBig; + else if (picSettings.AggNumTempMin) + imgState.TestoFirmaPiccola = imgState.NomeFileBig + " "; // Dim yPosFromBottom4 As Single Font crFont1 = null/* TODO Change to default(_) if this is not a reference type */; Font crFont2 = null/* TODO Change to default(_) if this is not a reference type */; - SizeF crSize1 = new SizeF(); - SizeF crSize2 = new SizeF(); + var crSize1 = new SizeF(); + var crSize2 = new SizeF(); - if (_picSettings.CreaMiniature == true) + if (picSettings.CreaMiniature == true) { - if (_picSettings.AggiungiScritteMiniature == false) + if (picSettings.AggiungiScritteMiniature == false) { - if (string.Equals(_picSettings.DirectorySorgente, _picSettings.DirectoryDestinazione, StringComparison.OrdinalIgnoreCase)) - nomeFileSmall = nomeFileSmall.Substring(0, nomeFileSmall.Length - 4) + _picSettings.Codice + nomeFileSmall.Substring(nomeFileSmall.Length - 4); - if (_picSettings.UsaOrarioMiniatura | _picSettings.TestoMin | _picSettings.AggTempoGaraMin | _picSettings.AggNumTempMin) + if (string.Equals(picSettings.DirectorySorgente, picSettings.DirectoryDestinazione, StringComparison.OrdinalIgnoreCase)) + imgState.NomeFileSmall = imgState.NomeFileSmall.Substring(0, imgState.NomeFileSmall.Length - 4) + picSettings.Codice + imgState.NomeFileSmall.Substring(imgState.NomeFileSmall.Length - 4); + if (picSettings.UsaOrarioMiniatura | picSettings.TestoMin | picSettings.AggTempoGaraMin | picSettings.AggNumTempMin) { - if (testoFirmaPiccola.Length > 0) + if (imgState.TestoFirmaPiccola.Length > 0) { - using (var imgOutputSmall = (Bitmap)imgOutputBig.Clone()) + using var imgOutputSmall = (Bitmap)imgOutputBig.Clone(); + using var grPhoto1 = Graphics.FromImage(imgOutputSmall); + grPhoto1.SmoothingMode = SmoothingMode.AntiAlias; + + // quick fix + imgState.DimensioneStandardMiniatura = 50; + if (picSettings.Grassetto == true) { - Graphics grPhoto1; - grPhoto1 = Graphics.FromImage(imgOutputSmall); - grPhoto1.SmoothingMode = SmoothingMode.AntiAlias; - - int LarghezzaStandard1; - // quick fix - DimensioneStandardMiniatura = 50; - if (_picSettings.Grassetto == true) - { - crFont1 = new Font(_picSettings.IlFont, DimensioneStandardMiniatura, FontStyle.Bold); - crFont2 = new Font(_picSettings.IlFont, DimensioneStandard, FontStyle.Bold); - } - else - { - crFont1 = new Font(_picSettings.IlFont, DimensioneStandardMiniatura); - crFont2 = new Font(_picSettings.IlFont, DimensioneStandard); - } - - crSize1 = grPhoto1.MeasureString(testoFirmaPiccola, crFont1); - crSize2 = grPhoto1.MeasureString(testoFirma, crFont1); - LarghezzaStandard1 = System.Convert.ToInt32(crSize1.Width); - - if (crSize1.Width > System.Convert.ToSingle(g.Width)) - { - int Conta = DimensioneStandardMiniatura; - do - { - if (Conta > 20) - Conta -= 5; - else - Conta -= 1; - if (_picSettings.Grassetto == true) - crFont1 = new Font(_picSettings.IlFont, Conta, FontStyle.Bold); - else - crFont1 = new Font(_picSettings.IlFont, Conta); - crSize1 = grPhoto1.MeasureString(testoFirmaPiccola, crFont1); - if (crSize1.Width < System.Convert.ToSingle(g.Width)) - { - LarghezzaStandard1 = System.Convert.ToInt32(crSize1.Width); - break; - } - - if (Conta <= 5) - break; - } - while (true); - DimensioneStandardMiniatura = Conta; - } - - switch (_picSettings.Posizione.ToUpper()) - { - case "ALTO": - { - yPosFromBottom1 = (_picSettings.Margine); - yPosFromBottom4 = (_picSettings.MargVert); - break; - } - - case "BASSO": - { - yPosFromBottom1 = System.Convert.ToSingle((g.Height - crSize1.Height - (g.Height * _picSettings.Margine / (double)100))); - yPosFromBottom4 = System.Convert.ToSingle((g.Height - crSize1.Height - (g.Height * _picSettings.MargVert / (double)100))); - break; - } - } - - float xCenterOfImg1 = 0; - - StringFormat StrFormat1 = new StringFormat(); - switch (_picSettings.Allineamento.ToUpper()) - { - case "SINISTRA": - { - xCenterOfImg1 = System.Convert.ToSingle((_picSettings.Margine + (LarghezzaStandard1 / (double)2))); - - if ((LarghezzaStandard1 / (double)2) > (g.Width / (double)2) - _picSettings.Margine) - xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2)); - break; - } - - case "CENTRO": - { - xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2)); - break; - } - - case "DESTRA": - { - xCenterOfImg1 = System.Convert.ToSingle((g.Width - _picSettings.Margine - (LarghezzaStandard1 / (double)2))); - - if ((LarghezzaStandard1 / (double)2) > (g.Width / (double)2) - _picSettings.Margine) - xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2)); - break; - } - } - StrFormat1.Alignment = StringAlignment.Center; - - SolidBrush semiTransBrush21 = new SolidBrush(Color.FromArgb(alphaScelta, 0, 0, 0)); - SolidBrush semiTransBrush1 = new SolidBrush(Color.FromArgb(alphaScelta, _picSettings.FontColoreRGB)); - - // quick fix - DimensioneStandardMiniatura = _picSettings.DimMin; - - if (_picSettings.Grassetto == true) - crFont1 = new Font(_picSettings.IlFont, DimensioneStandardMiniatura, FontStyle.Bold); - else - crFont1 = new Font(_picSettings.IlFont, DimensioneStandardMiniatura); - // asdgadfhdfhjgfsjgfjygfdhsdafa - if (_picSettings.TestoMin) - { - grPhoto1.DrawString(nomeFileBig, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1); - grPhoto1.DrawString(nomeFileBig, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1); - } - else if (_picSettings.AggTempoGaraMin & _picSettings.UsaTempoGaraTestoApplicare) - { - var diff = dataPartenzaI - dataFoto; - var diffA = diff.TotalSeconds * 10000000; - - TimeSpan Orario = new TimeSpan(0, 0, (int)diffA);/* new TimeSpan(DateTime.DateDiff(DateInterval.Second, dataPartenzaI, dataFoto) * 10000000);*/ - string tempstr = ""; - - - tempstr += Environment.NewLine + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00"); - - - grPhoto1.DrawString(tempstr, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1); - grPhoto1.DrawString(tempstr, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1); - } - else if (_picSettings.AggNumTempMin) - { - var diff = dataPartenzaI - dataFoto; - var diffA = diff.TotalSeconds * 10000000; - TimeSpan Orario = new TimeSpan(0, 0, (int)diffA); - string tempstr = ""; - tempstr += nomeFileBig; - - tempstr += Environment.NewLine + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00"); - - - grPhoto1.DrawString(tempstr, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1); - grPhoto1.DrawString(tempstr, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1); - } - else - { - grPhoto1.DrawString(testoFirmaPiccola, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1); - grPhoto1.DrawString(testoFirmaPiccola, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1); - } - - // Salva la miniatura - //using (var g22 = Image.FromHbitmap(imgOutputSmall)) - - - using (var imgOutputSmall2 = new Bitmap(imgOutputSmall, thumbSizeSmall.Width, thumbSizeSmall.Height)) - { - imgOutputSmall2.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat); - - //imgOutputSmall2.Dispose(); - } - - //imgOutputSmall.Dispose(); + crFont1 = new Font(picSettings.IlFont, imgState.DimensioneStandardMiniatura, FontStyle.Bold); + crFont2 = new Font(picSettings.IlFont, imgState.DimensioneStandard, FontStyle.Bold); } + else + { + crFont1 = new Font(picSettings.IlFont, imgState.DimensioneStandardMiniatura); + crFont2 = new Font(picSettings.IlFont, imgState.DimensioneStandard); + } + + crSize1 = grPhoto1.MeasureString(imgState.TestoFirmaPiccola, crFont1); + crSize2 = grPhoto1.MeasureString(imgState.TestoFirma, crFont1); + var larghezzaStandard1 = System.Convert.ToInt32(crSize1.Width); + + if (crSize1.Width > System.Convert.ToSingle(g.Width)) + { + int Conta = imgState.DimensioneStandardMiniatura; + do + { + if (Conta > 20) + Conta -= 5; + else + Conta -= 1; + if (picSettings.Grassetto == true) + crFont1 = new Font(picSettings.IlFont, Conta, FontStyle.Bold); + else + crFont1 = new Font(picSettings.IlFont, Conta); + crSize1 = grPhoto1.MeasureString(imgState.TestoFirmaPiccola, crFont1); + if (crSize1.Width < System.Convert.ToSingle(g.Width)) + { + larghezzaStandard1 = System.Convert.ToInt32(crSize1.Width); + break; + } + + if (Conta <= 5) + break; + } + while (true); + imgState.DimensioneStandardMiniatura = Conta; + } + + switch (picSettings.Posizione.ToUpper()) + { + case "ALTO": + { + imgState.YPosFromBottom1 = (picSettings.Margine); + imgState.YPosFromBottom4 = (picSettings.MargVert); + break; + } + + case "BASSO": + { + imgState.YPosFromBottom1 = System.Convert.ToSingle((g.Height - crSize1.Height - (g.Height * picSettings.Margine / (double)100))); + imgState.YPosFromBottom4 = System.Convert.ToSingle((g.Height - crSize1.Height - (g.Height * picSettings.MargVert / (double)100))); + break; + } + } + + float xCenterOfImg1 = 0; + + using StringFormat strFormat1 = new StringFormat(); + switch (picSettings.Allineamento.ToUpper()) + { + case "SINISTRA": + { + xCenterOfImg1 = System.Convert.ToSingle((picSettings.Margine + (larghezzaStandard1 / (double)2))); + + if ((larghezzaStandard1 / (double)2) > (g.Width / (double)2) - picSettings.Margine) + xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2)); + break; + } + + case "CENTRO": + { + xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2)); + break; + } + + case "DESTRA": + { + xCenterOfImg1 = System.Convert.ToSingle((g.Width - picSettings.Margine - (larghezzaStandard1 / (double)2))); + + if ((larghezzaStandard1 / (double)2) > (g.Width / (double)2) - picSettings.Margine) + xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2)); + break; + } + } + strFormat1.Alignment = StringAlignment.Center; + + using var semiTransBrush21 = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, 0, 0, 0)); + using var semiTransBrush1 = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, picSettings.FontColoreRGB)); + + // quick fix + imgState.DimensioneStandardMiniatura = picSettings.DimMin; + + if (picSettings.Grassetto == true) + crFont1 = new Font(picSettings.IlFont, imgState.DimensioneStandardMiniatura, FontStyle.Bold); + else + crFont1 = new Font(picSettings.IlFont, imgState.DimensioneStandardMiniatura); + // asdgadfhdfhjgfsjgfjygfdhsdafa + if (picSettings.TestoMin) + { + grPhoto1.DrawString(imgState.NomeFileBig, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, imgState.YPosFromBottom1 + 1), strFormat1); + grPhoto1.DrawString(imgState.NomeFileBig, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, imgState.YPosFromBottom1), strFormat1); + } + else if (picSettings.AggTempoGaraMin & picSettings.UsaTempoGaraTestoApplicare) + { + var diff = imgState.DataPartenzaI - imgState.DataFoto; + var diffA = diff.TotalSeconds * 10000000; + + var orario = new TimeSpan(0, 0, (int)diffA);/* new TimeSpan(DateTime.DateDiff(DateInterval.Second, dataPartenzaI, dataFoto) * 10000000);*/ + string tempstr = ""; + + + tempstr += Environment.NewLine + imgState.TestoOrario + orario.Hours.ToString("00") + ":" + orario.Minutes.ToString("00") + ":" + orario.Seconds.ToString("00"); + + + grPhoto1.DrawString(tempstr, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, imgState.YPosFromBottom1 + 1), strFormat1); + grPhoto1.DrawString(tempstr, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, imgState.YPosFromBottom1), strFormat1); + } + else if (picSettings.AggNumTempMin) + { + var diff = imgState.DataPartenzaI - imgState.DataFoto; + var diffA = diff.TotalSeconds * 10000000; + TimeSpan Orario = new TimeSpan(0, 0, (int)diffA); + string tempstr = ""; + tempstr += imgState.NomeFileBig; + + tempstr += Environment.NewLine + imgState.TestoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00"); + + + grPhoto1.DrawString(tempstr, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, imgState.YPosFromBottom1 + 1), strFormat1); + grPhoto1.DrawString(tempstr, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, imgState.YPosFromBottom1), strFormat1); + } + else + { + grPhoto1.DrawString(imgState.TestoFirmaPiccola, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, imgState.YPosFromBottom1 + 1), strFormat1); + grPhoto1.DrawString(imgState.TestoFirmaPiccola, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, imgState.YPosFromBottom1), strFormat1); + } + + // Salva la miniatura + //using (var g22 = Image.FromHbitmap(imgOutputSmall)) + + + using var imgOutputSmall2 = new Bitmap(imgOutputSmall, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height); + imgOutputSmall2.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat); + + //imgOutputSmall2.Dispose(); + + //imgOutputSmall.Dispose(); //File.Delete(Path.Combine(DestDir.FullName, "Temp_" + nomeFileSmall)); @@ -491,15 +413,15 @@ public class ImageCreatorSharp : IDisposable } else { - using var imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height); - imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat); + using var imgOutputSmall = new Bitmap(g, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height); + imgOutputSmall.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat); //imgOutputSmall.Dispose(); } } else { - using Bitmap imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height); - imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat); + using var imgOutputSmall = new Bitmap(g, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height); + imgOutputSmall.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat); //imgOutputSmall.Dispose(); } } @@ -509,79 +431,76 @@ public class ImageCreatorSharp : IDisposable crFont2?.Dispose(); } - private void AggiungiTesto(Image g, Bitmap imgOutputBig) + private void AggiungiTesto(Image g, ImageState imgState, Bitmap imgOutputBig) { using var grPhoto = Graphics.FromImage(imgOutputBig); grPhoto.SmoothingMode = SmoothingMode.AntiAlias; Font crFont = null/* TODO Change to default(_) if this is not a reference type */; - SizeF crSize = new SizeF(); - int LarghezzaStandard; - if (_picSettings.Grassetto == true) - crFont = new Font(_picSettings.IlFont, DimensioneStandard, FontStyle.Bold); + if (picSettings.Grassetto == true) + crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard, FontStyle.Bold); else - crFont = new Font(_picSettings.IlFont, DimensioneStandard); - crSize = grPhoto.MeasureString(testoFirma, crFont); - LarghezzaStandard = System.Convert.ToInt32(crSize.Width); + crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard); + var crSize = grPhoto.MeasureString(imgState.TestoFirma, crFont); + var larghezzaStandard = System.Convert.ToInt32(crSize.Width); if (crSize.Width > System.Convert.ToSingle(g.Width)) { - int Conta = DimensioneStandard; + int conta = imgState.DimensioneStandard; do { - if (Conta > 20) - Conta -= 5; + if (conta > 20) + conta -= 5; else - Conta -= 1; - if (_picSettings.Grassetto == true) - crFont = new Font(_picSettings.IlFont, Conta, FontStyle.Bold); + conta -= 1; + if (picSettings.Grassetto == true) + crFont = new Font(picSettings.IlFont, conta, FontStyle.Bold); else - crFont = new Font(_picSettings.IlFont, Conta); - crSize = grPhoto.MeasureString(testoFirma, crFont); + crFont = new Font(picSettings.IlFont, conta); + crSize = grPhoto.MeasureString(imgState.TestoFirma, crFont); if (crSize.Width < System.Convert.ToSingle(g.Width)) { - LarghezzaStandard = System.Convert.ToInt32(crSize.Width); + larghezzaStandard = System.Convert.ToInt32(crSize.Width); break; } - if (Conta <= 5) + if (conta <= 5) break; } while (true); - DimensioneStandard = Conta; + imgState.DimensioneStandard = conta; } - - - switch (_picSettings.Posizione.ToUpper()) + + switch (picSettings.Posizione.ToUpper()) { case "ALTO": { - yPosFromBottom = (_picSettings.Margine); - yPosFromBottom3 = (_picSettings.MargVert); + imgState.YPosFromBottom = (picSettings.Margine); + imgState.YPosFromBottom3 = (picSettings.MargVert); break; } case "BASSO": { - yPosFromBottom = System.Convert.ToSingle((g.Height - crSize.Height - (g.Height * _picSettings.Margine / (double)100))); - yPosFromBottom3 = System.Convert.ToSingle((g.Height - crSize.Height - (g.Height * _picSettings.MargVert / (double)100))); + imgState.YPosFromBottom = System.Convert.ToSingle((g.Height - crSize.Height - (g.Height * picSettings.Margine / (double)100))); + imgState.YPosFromBottom3 = System.Convert.ToSingle((g.Height - crSize.Height - (g.Height * picSettings.MargVert / (double)100))); break; } } float xCenterOfImg = 0; float xCenterOfImg3 = 0; - StringFormat StrFormat = new StringFormat(); - switch (_picSettings.Allineamento.ToUpper()) + using var strFormat = new StringFormat(); + switch (picSettings.Allineamento.ToUpper()) { case "SINISTRA": { - xCenterOfImg = System.Convert.ToSingle((_picSettings.Margine + (LarghezzaStandard / (double)2))); - xCenterOfImg3 = System.Convert.ToSingle((_picSettings.MargVert + (LarghezzaStandard / (double)2))); - if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - _picSettings.Margine) + xCenterOfImg = System.Convert.ToSingle((picSettings.Margine + (larghezzaStandard / (double)2))); + xCenterOfImg3 = System.Convert.ToSingle((picSettings.MargVert + (larghezzaStandard / (double)2))); + if ((larghezzaStandard / (double)2) > (g.Width / (double)2) - picSettings.Margine) xCenterOfImg = System.Convert.ToSingle((g.Width / (double)2)); - if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - _picSettings.MargVert) + if ((larghezzaStandard / (double)2) > (g.Width / (double)2) - picSettings.MargVert) xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2)); break; } @@ -594,193 +513,176 @@ public class ImageCreatorSharp : IDisposable case "DESTRA": { - xCenterOfImg = System.Convert.ToSingle((g.Width - _picSettings.Margine - (LarghezzaStandard / (double)2))); - xCenterOfImg3 = System.Convert.ToSingle((g.Width - _picSettings.MargVert - (LarghezzaStandard / (double)2))); - if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - _picSettings.Margine) + xCenterOfImg = System.Convert.ToSingle((g.Width - picSettings.Margine - (larghezzaStandard / (double)2))); + xCenterOfImg3 = System.Convert.ToSingle((g.Width - picSettings.MargVert - (larghezzaStandard / (double)2))); + if ((larghezzaStandard / (double)2) > (g.Width / (double)2) - picSettings.Margine) xCenterOfImg = System.Convert.ToSingle((g.Width / (double)2)); - if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - _picSettings.MargVert) + if ((larghezzaStandard / (double)2) > (g.Width / (double)2) - picSettings.MargVert) xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2)); break; } } - StrFormat.Alignment = StringAlignment.Center; + strFormat.Alignment = StringAlignment.Center; - SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(alphaScelta, 0, 0, 0)); + using var semiTransBrush2 = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, 0, 0, 0)); // Dim semiTransBrush As SolidBrush = New SolidBrush(Color.FromArgb(AlphaScelta, _FontColoreR, _FontColoreG, _FontColoreB)) - SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(alphaScelta, _picSettings.FontColoreRGB)); + using var semiTransBrush = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, picSettings.FontColoreRGB)); - if (FotoRuotaADestra | FotoRuotaASinistra) + if (imgState.FotoRuotaADestra | imgState.FotoRuotaASinistra) { - if (_picSettings.Grassetto == true) - crFont = new Font(_picSettings.IlFont, _picSettings.DimVert, FontStyle.Bold); + if (picSettings.Grassetto == true) + crFont = new Font(picSettings.IlFont, picSettings.DimVert, FontStyle.Bold); else - crFont = new Font(_picSettings.IlFont, _picSettings.DimVert); + crFont = new Font(picSettings.IlFont, picSettings.DimVert); } - else if (_picSettings.Grassetto == true) - crFont = new Font(_picSettings.IlFont, DimensioneStandard, FontStyle.Bold); + else if (picSettings.Grassetto == true) + crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard, FontStyle.Bold); else - crFont = new Font(_picSettings.IlFont, DimensioneStandard); + crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard); // qui scrive il testo (nomefilebig) - if (_picSettings.TestoNome) + if (picSettings.TestoNome) { - if (_picSettings.NomeData & g.PropertyIdList.Length > 0) + if (picSettings.NomeData & g.PropertyIdList.Length > 0) { //ExifReader DatiExif = new ExifReader((Bitmap)g); - dataFoto = _creationDate ?? DateTime.Now; //DatiExif.DateTimeOriginal; + imgState.DataFoto = imgState.CreationDate ?? DateTime.Now; //DatiExif.DateTimeOriginal; - grPhoto.DrawString((nomeFileBig + " " + dataFoto.ToShortDateString()), crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat); - grPhoto.DrawString((nomeFileBig + " " + dataFoto.ToShortDateString()), crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); + grPhoto.DrawString((imgState.NomeFileBig + " " + imgState.DataFoto.ToShortDateString()), crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, imgState.YPosFromBottom + 1), strFormat); + grPhoto.DrawString((imgState.NomeFileBig + " " + imgState.DataFoto.ToShortDateString()), crFont, semiTransBrush, new PointF(xCenterOfImg, imgState.YPosFromBottom), strFormat); } else { - grPhoto.DrawString(nomeFileBig, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat); - grPhoto.DrawString(nomeFileBig, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); + grPhoto.DrawString(imgState.NomeFileBig, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, imgState.YPosFromBottom + 1), strFormat); + grPhoto.DrawString(imgState.NomeFileBig, crFont, semiTransBrush, new PointF(xCenterOfImg, imgState.YPosFromBottom), strFormat); } } - if (_picSettings.TestoNome == false) + if (picSettings.TestoNome == false) { - if (FotoRuotaADestra | FotoRuotaASinistra) + if (imgState.FotoRuotaADestra | imgState.FotoRuotaASinistra) { - if (_picSettings.TestoMin == false) + if (picSettings.TestoMin == false) { - grPhoto.DrawString(testoFirmaV, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom3 + 1), StrFormat); - grPhoto.DrawString(testoFirmaV, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom3), StrFormat); + grPhoto.DrawString(imgState.TestoFirmaV, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, imgState.YPosFromBottom3 + 1), strFormat); + grPhoto.DrawString(imgState.TestoFirmaV, crFont, semiTransBrush, new PointF(xCenterOfImg, imgState.YPosFromBottom3), strFormat); } - if (_picSettings.TestoMin == true) + if (picSettings.TestoMin == true) { - grPhoto.DrawString(testoFirmaV, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom4 + 1), StrFormat); - grPhoto.DrawString(testoFirmaV, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom4), StrFormat); + grPhoto.DrawString(imgState.TestoFirmaV, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, imgState.YPosFromBottom4 + 1), strFormat); + grPhoto.DrawString(imgState.TestoFirmaV, crFont, semiTransBrush, new PointF(xCenterOfImg, imgState.YPosFromBottom4), strFormat); } } else { - grPhoto.DrawString(testoFirma, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat); - grPhoto.DrawString(testoFirma, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); + grPhoto.DrawString(imgState.TestoFirma, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, imgState.YPosFromBottom + 1), strFormat); + grPhoto.DrawString(imgState.TestoFirma, crFont, semiTransBrush, new PointF(xCenterOfImg, imgState.YPosFromBottom), strFormat); } } - if (string.Equals(_picSettings.DirectorySorgente, _picSettings.DirectoryDestinazione, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(picSettings.DirectorySorgente, picSettings.DirectoryDestinazione, StringComparison.OrdinalIgnoreCase)) { - nomeFileBig2 = nomeFileBig; - nomeFileBig = nomeFileBig.Substring(0, nomeFileBig.Length - 4) + _picSettings.Codice + nomeFileBig.Substring(nomeFileBig.Length - 4); + imgState.NomeFileBig2 = imgState.NomeFileBig; + imgState.NomeFileBig = $"{imgState.NomeFileBig[..^4]}{picSettings.Codice}{imgState.NomeFileBig[^4..]}"; } //grPhoto.Dispose(); crFont?.Dispose(); } - - - - private void aggiungiLogo(Bitmap imgOutputBig, Image logo) + private void AggiungiLogo(Bitmap imgOutputBig, ImageState imgState, Image logo) { // imgOutputBig - if (_picSettings.LogoAggiungi == true & File.Exists(_picSettings.LogoNomeFile)) + if (!(picSettings.LogoAggiungi == true & File.Exists(picSettings.LogoNomeFile))) return; + // using var ImmagineLogo = Image.FromFile(_picSettings.LogoNomeFile); + + var logoColoreTrasparente = Color.White; + + // * Load this Bitmap into a new Graphic Object + using var grWatermark = Graphics.FromImage(imgOutputBig); + using ImageAttributes imageAttributes = new ImageAttributes(); + + // * The first step replace the background color with one that is transparent (Alpha=0, R=0, G=0, B=0) + var colorMap = new ColorMap { - // using var ImmagineLogo = Image.FromFile(_picSettings.LogoNomeFile); - - Color LogoColoreTrasparente = Color.White; - - // * Load this Bitmap into a new Graphic Object - using Graphics grWatermark = Graphics.FromImage(imgOutputBig); - ImageAttributes imageAttributes = new ImageAttributes(); - - // * The first step replace the background color with one that is transparent (Alpha=0, R=0, G=0, B=0) - ColorMap colorMap = new ColorMap(); - // * background this will be the color we search for and replace with transparency - colorMap.OldColor = LogoColoreTrasparente; - colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); + OldColor = logoColoreTrasparente, + NewColor = Color.FromArgb(0, 0, 0, 0) + }; - ColorMap[] remapTable = new[] { colorMap }; - imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); + var remapTable = new[] { colorMap }; + imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); - // * The second color manipulation is used to change the opacity by setting the 3rd row and 3rd column to 0.3f - float[][] colorMatrixElements = new[] { new float[] { 1.0F, 0.0F, 0.0F, 0.0F, 0.0F }, new float[] { 0.0F, 1.0F, 0.0F, 0.0F, 0.0F }, new float[] { 0.0F, 0.0F, 1.0F, 0.0F, 0.0F }, new float[] { 0.0F, 0.0F, 0.0F, System.Convert.ToSingle(_picSettings.LogoTrasparenza) / 100F, 0.0F }, new float[] { 0.0F, 0.0F, 0.0F, 0.0F, 1.0F } }; - ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); - imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); + // * The second color manipulation is used to change the opacity by setting the 3rd row and 3rd column to 0.3f + var colorMatrixElements = new[] { new float[] { 1.0F, 0.0F, 0.0F, 0.0F, 0.0F }, new float[] { 0.0F, 1.0F, 0.0F, 0.0F, 0.0F }, new float[] { 0.0F, 0.0F, 1.0F, 0.0F, 0.0F }, new float[] { 0.0F, 0.0F, 0.0F, System.Convert.ToSingle(picSettings.LogoTrasparenza) / 100F, 0.0F }, new float[] { 0.0F, 0.0F, 0.0F, 0.0F, 1.0F } }; + var wmColorMatrix = new ColorMatrix(colorMatrixElements); + imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); - int FotoLogoH = _picSettings.LogoAltezza; - int FotoLogoW = _picSettings.LogoLarghezza; - double FattoreAlt = logo.Height / (double)FotoLogoH; - double FattoreLarg = logo.Width / (double)FotoLogoW; - Size NuovaSize; - if (FattoreLarg > FattoreAlt) - NuovaSize = NewthumbSize(logo.Width, logo.Height, FotoLogoW, "Larghezza"); - else - NuovaSize = NewthumbSize(logo.Width, logo.Height, FotoLogoH, "Altezza"); + var fotoLogoH = picSettings.LogoAltezza; + var fotoLogoW = picSettings.LogoLarghezza; + var fattoreAlt = logo.Height / (double)fotoLogoH; + var fattoreLarg = logo.Width / (double)fotoLogoW; + var nuovaSize = fattoreLarg > fattoreAlt ? NewthumbSize(logo.Width, logo.Height, fotoLogoW, "Larghezza") : NewthumbSize(logo.Width, logo.Height, fotoLogoH, "Altezza"); - int MargineUsato; - int MargineL; - bool InPercentualeL; - if (_picSettings.LogoMargine.EndsWith("%") == true) - InPercentualeL = true; - else - InPercentualeL = false; - MargineL = System.Convert.ToInt32(_picSettings.LogoMargine); - if (InPercentualeL == true) - MargineUsato = System.Convert.ToInt32(imgOutputBig.Height * MargineL / (double)100); - else - MargineUsato = MargineL; + var inPercentualeL = picSettings.LogoMargine.EndsWith('%'); + var margineL = System.Convert.ToInt32(picSettings.LogoMargine); + var margineUsato = inPercentualeL ? System.Convert.ToInt32(imgOutputBig.Height * margineL / (double)100) : margineL; - int xPosOfWm = 0; - int yPosOfWm = 0; - switch (_picSettings.LogoPosizioneH.ToUpper()) + int xPosOfWm = 0; + int yPosOfWm = 0; + switch (picSettings.LogoPosizioneH.ToUpper()) + { + case "SINISTRA": + case "NESSUNA": { - case "SINISTRA": - case "NESSUNA": - { - xPosOfWm = MargineUsato; - break; - } - - case "CENTRO": - { - xPosOfWm = System.Convert.ToInt32((imgOutputBig.Width - NuovaSize.Width) / (double)2); - break; - } - - case "DESTRA": - { - xPosOfWm = ((imgOutputBig.Width - NuovaSize.Width) - MargineUsato); - break; - } - } - switch (_picSettings.LogoPosizioneV.ToUpper()) - { - case "ALTO": - case "NESSUNA": - { - yPosOfWm = MargineUsato; - break; - } - - case "CENTRO": - { - yPosOfWm = System.Convert.ToInt32((imgOutputBig.Height - NuovaSize.Height) / (double)2); - break; - } - - case "BASSO": - { - yPosOfWm = ((imgOutputBig.Height - NuovaSize.Height) - MargineUsato); - break; - } + xPosOfWm = margineUsato; + break; } - grWatermark.DrawImage(logo, new Rectangle(xPosOfWm, yPosOfWm, NuovaSize.Width, NuovaSize.Height), 0, 0, logo.Width, logo.Height, GraphicsUnit.Pixel, imageAttributes); - //grWatermark.Dispose(); + case "CENTRO": + { + xPosOfWm = System.Convert.ToInt32((imgOutputBig.Width - nuovaSize.Width) / (double)2); + break; + } + + case "DESTRA": + { + xPosOfWm = ((imgOutputBig.Width - nuovaSize.Width) - margineUsato); + break; + } } + switch (picSettings.LogoPosizioneV.ToUpper()) + { + case "ALTO": + case "NESSUNA": + { + yPosOfWm = margineUsato; + break; + } + + case "CENTRO": + { + yPosOfWm = System.Convert.ToInt32((imgOutputBig.Height - nuovaSize.Height) / (double)2); + break; + } + + case "BASSO": + { + yPosOfWm = ((imgOutputBig.Height - nuovaSize.Height) - margineUsato); + break; + } + } + + grWatermark.DrawImage(logo, new Rectangle(xPosOfWm, yPosOfWm, nuovaSize.Width, nuovaSize.Height), 0, 0, logo.Width, logo.Height, GraphicsUnit.Pixel, imageAttributes); + //grWatermark.Dispose(); } - private void SalvaFoto(Bitmap imgOutputBig, Size thumbSizeBig, string NomeFileBig, string NomeFileSmall, Size thumbSizeSmall, ImageFormat thisFormat) + private void SalvaFoto(Bitmap imgOutputBig, ImageState imgState, ImageFormat thisFormat) { using var image1Stream = new MemoryStream(); - if (_picSettings.FotoGrandeDimOrigina == false) + if (picSettings.FotoGrandeDimOrigina == false) { // attenzione non controlla se è png // imgOutputBig.Save(Path.Combine(_DestDir.FullName, "Temp_" & NomeFileBig), thisFormat) @@ -796,12 +698,12 @@ public class ImageCreatorSharp : IDisposable //imgOutputBig.Save(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig), thisFormat); image1Stream.Seek(0, SeekOrigin.Begin); using var g2 = Image.FromStream(image1Stream); - thumbSizeBig = g2.Width > g2.Height ? NewthumbSize(g2.Width, g2.Height, _picSettings.LarghezzaBig, "Larghezza") : NewthumbSize(g2.Width, g2.Height, _picSettings.AltezzaBig, "Altezza"); - using Bitmap imgOutputBig2 = new Bitmap(g2, thumbSizeBig.Width, thumbSizeBig.Height); + imgState.ThumbSizeBig = g2.Width > g2.Height ? NewthumbSize(g2.Width, g2.Height, picSettings.LarghezzaBig, "Larghezza") : NewthumbSize(g2.Width, g2.Height, picSettings.AltezzaBig, "Altezza"); + using var imgOutputBig2 = new Bitmap(g2, imgState.ThumbSizeBig.Width, imgState.ThumbSizeBig.Height); if (thisFormat.Equals(ImageFormat.Jpeg)) - SalvaImmagineCustomQuality(imgOutputBig2, Path.Combine(DestDir.FullName, NomeFileBig), _picSettings.JpegQuality); + SalvaImmagineCustomQuality(imgOutputBig2, Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), picSettings.JpegQuality); else - imgOutputBig2.Save(Path.Combine(DestDir.FullName, NomeFileBig), thisFormat); + imgOutputBig2.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), thisFormat); //imgOutputBig2.Dispose(); @@ -812,39 +714,35 @@ public class ImageCreatorSharp : IDisposable { // if (thisFormat.Equals(ImageFormat.Jpeg)) - SalvaImmagineCustomQuality(imgOutputBig, Path.Combine(DestDir.FullName, NomeFileBig), _picSettings.JpegQuality); + SalvaImmagineCustomQuality(imgOutputBig, Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), picSettings.JpegQuality); else - imgOutputBig.Save(Path.Combine(DestDir.FullName, NomeFileBig), thisFormat); + imgOutputBig.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), thisFormat); //imgOutputBig.Dispose(); } image1Stream.Seek(0, SeekOrigin.Begin); - if (_picSettings.CreaMiniature) - { - if (_picSettings.AggiungiScritteMiniature) - { - using System.Drawing.Image g1 = _picSettings.FotoGrandeDimOrigina ? (Image)imgOutputBig.Clone() : Image.FromStream(image1Stream); - //if (_picSettings.FotoGrandeDimOrigina == false) - // g1 = Image.FromStream(image1Stream); - ////g1 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig)); - //else - // g1 = (Image)imgOutputBig.Clone(); - //g1 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, NomeFileBig)); - using Bitmap imgOutputSmall = new Bitmap(g1, thumbSizeSmall.Width, thumbSizeSmall.Height); - if (string.Equals(_picSettings.DirectorySorgente, _picSettings.DirectoryDestinazione, StringComparison.OrdinalIgnoreCase)) - NomeFileSmall = NomeFileSmall.Substring(0, NomeFileSmall.Length - 4) + _picSettings.Codice + NomeFileSmall.Substring(NomeFileSmall.Length - 4); - // - if (thisFormat.Equals(ImageFormat.Jpeg)) - SalvaImmagineCustomQuality(imgOutputSmall, Path.Combine(DestDir.FullName, NomeFileSmall), _picSettings.JpegQualityMin); - else - imgOutputSmall.Save(Path.Combine(_DestDir.FullName, NomeFileSmall), thisFormat); + if (!picSettings.CreaMiniature) return; + if (!picSettings.AggiungiScritteMiniature) return; + using var g1 = picSettings.FotoGrandeDimOrigina ? (Image)imgOutputBig.Clone() : Image.FromStream(image1Stream); + //if (_picSettings.FotoGrandeDimOrigina == false) + // g1 = Image.FromStream(image1Stream); + ////g1 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig)); + //else + // g1 = (Image)imgOutputBig.Clone(); + //g1 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, NomeFileBig)); + using Bitmap imgOutputSmall = new Bitmap(g1, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height); + if (string.Equals(picSettings.DirectorySorgente, picSettings.DirectoryDestinazione, StringComparison.OrdinalIgnoreCase)) + imgState.NomeFileSmall = imgState.NomeFileSmall.Substring(0, imgState.NomeFileSmall.Length - 4) + picSettings.Codice + imgState.NomeFileSmall.Substring(imgState.NomeFileSmall.Length - 4); + // + if (thisFormat.Equals(ImageFormat.Jpeg)) + SalvaImmagineCustomQuality(imgOutputSmall, Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), picSettings.JpegQualityMin); + else + imgOutputSmall.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat); - //imgOutputSmall.Dispose(); + //imgOutputSmall.Dispose(); - //g1.Dispose(); - } - } + //g1.Dispose(); //if (File.Exists(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig))) // File.Delete(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig)); @@ -852,37 +750,35 @@ public class ImageCreatorSharp : IDisposable private void SalvaImmagineCustomQuality(Bitmap imageToSave, string nomeFileFinale, long quality) { - ImageCodecInfo JgpEncoder = GetEncoder(ImageFormat.Jpeg); - System.Drawing.Imaging.Encoder MyEncoder = System.Drawing.Imaging.Encoder.Quality; + var jgpEncoder = GetEncoder(ImageFormat.Jpeg); + var myEncoder = System.Drawing.Imaging.Encoder.Quality; - EncoderParameters MyEncoderParameters = new EncoderParameters(1); + using var myEncoderParameters = new EncoderParameters(1); - EncoderParameter MyEncoderParameter = new EncoderParameter(MyEncoder, _picSettings.JpegQuality); - MyEncoderParameters.Param[0] = MyEncoderParameter; - imageToSave.Save(nomeFileFinale, JgpEncoder, MyEncoderParameters); + var myEncoderParameter = new EncoderParameter(myEncoder, picSettings.JpegQuality); + myEncoderParameters.Param[0] = myEncoderParameter; + imageToSave.Save(nomeFileFinale, jgpEncoder, myEncoderParameters); //imageToSave.Dispose(); } private void MakeImageCustomQuality(Bitmap imageToSave, Stream destinationStream) { - ImageCodecInfo JgpEncoder = GetEncoder(ImageFormat.Jpeg); - System.Drawing.Imaging.Encoder MyEncoder = System.Drawing.Imaging.Encoder.Quality; + var jgpEncoder = GetEncoder(ImageFormat.Jpeg); + var myEncoder = System.Drawing.Imaging.Encoder.Quality; - EncoderParameters MyEncoderParameters = new EncoderParameters(1); + using var myEncoderParameters = new EncoderParameters(1); - EncoderParameter MyEncoderParameter = new EncoderParameter(MyEncoder, _picSettings.JpegQuality); - MyEncoderParameters.Param[0] = MyEncoderParameter; + var myEncoderParameter = new EncoderParameter(myEncoder, picSettings.JpegQuality); + myEncoderParameters.Param[0] = myEncoderParameter; destinationStream.Seek(0, SeekOrigin.Begin); - imageToSave.Save(destinationStream, JgpEncoder, MyEncoderParameters); + imageToSave.Save(destinationStream, jgpEncoder, myEncoderParameters); //imageToSave.Dispose(); } - - + private ImageCodecInfo GetEncoder(ImageFormat format) { - ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); - - + var codecs = ImageCodecInfo.GetImageDecoders(); + foreach (var codec in codecs) { if (codec.FormatID == format.Guid) @@ -896,87 +792,30 @@ public class ImageCreatorSharp : IDisposable /// ''' /// ''' Larghezza /// ''' Altezza - /// ''' - /// ''' + /// ''' + /// ''' /// ''' /// ''' - private Size NewthumbSize(int currentwidth, int currentheight, int MaxPixel, string TipoSize) + private Size NewthumbSize(int currentwidth, int currentheight, int maxPixel, string tipoSize) { // e // *** Larghezza, Altezza, Auto double tempMultiplier; - if (TipoSize.ToUpper() == "Larghezza".ToUpper()) - tempMultiplier = MaxPixel / (double)currentwidth; - else if (TipoSize.ToUpper() == "Altezza".ToUpper()) - tempMultiplier = MaxPixel / (double)currentheight; + if (tipoSize.ToUpper() == "Larghezza".ToUpper()) + tempMultiplier = maxPixel / (double)currentwidth; + else if (tipoSize.ToUpper() == "Altezza".ToUpper()) + tempMultiplier = maxPixel / (double)currentheight; else if (currentheight > currentwidth) - tempMultiplier = MaxPixel / (double)currentheight; + tempMultiplier = maxPixel / (double)currentheight; else - tempMultiplier = MaxPixel / (double)currentwidth; + tempMultiplier = maxPixel / (double)currentwidth; - Size NewSize = new Size(System.Convert.ToInt32(currentwidth * tempMultiplier), System.Convert.ToInt32(currentheight * tempMultiplier)); + var newSize = new Size(System.Convert.ToInt32(currentwidth * tempMultiplier), System.Convert.ToInt32(currentheight * tempMultiplier)); - return NewSize; + return newSize; } - public FileInfo WorkFile - { - get - { - return _workFile; - } - set - { - _workFile = value; - } - } - - public DirectoryInfo DestDir - { - get => _DestDir; - set => _DestDir = value; - } - - public DirectoryInfo SourceDir - { - get - { - return _SourceDir; - } - set - { - _SourceDir = value; - } - } - - public DirectoryInfo DestDirStart - { - get - { - return _DestDirStart; - } - set - { - _DestDirStart = value; - } - } - - public string NomeFileChild - { - get - { - return _NomeFileChild; - } - set - { - _NomeFileChild = value; - } - } - - public void Dispose() - { - - } -} + +} \ No newline at end of file diff --git a/MaddoShared/ImageState.cs b/MaddoShared/ImageState.cs new file mode 100644 index 0000000..0090ecb --- /dev/null +++ b/MaddoShared/ImageState.cs @@ -0,0 +1,45 @@ +using System; +using System.Drawing; +using System.IO; + +namespace MaddoShared; + +public class ImageState +{ + public bool FotoRuotaADestra { get; set; } = false; + public bool FotoRuotaASinistra { get; set; } = false; + + public string TempMinText { get; set; } = ""; + // public crFont1 As Font + public string NomeFileChild { get; set; } + + public DirectoryInfo SourceDir { get; set; } + public DirectoryInfo DestDirStart { get; set; } + public DirectoryInfo DestDir { get; set; } + + public FileInfo WorkFile { get; set; } + + public string TestoFirma { get; set; } + public string TestoFirmaV { get; set; } + public int AlphaScelta { get; set; } + public int DimensioneStandard { get; set; } + public int DimensioneStandardMiniatura { get; set; } + public DateTime DataFoto { get; set; } + public DateTime DataPartenzaI { get; set; } + public string TestoOrario { get; set; } + public string TestoFirmaPiccola { get; set; } + public Size ThumbSizeSmall { get; set; } + public Size ThumbSizeBig { get; set; } + public string NomeFileSmall{ get; set; } + public string NomeFileBig{ get; set; } + public string NomeFileBig2{ get; set; } + + public float YPosFromBottom{ get; set; } + public float YPosFromBottom1{ get; set; } + public float YPosFromBottom2{ get; set; } + public float YPosFromBottom3{ get; set; } + public float YPosFromBottom4{ get; set; } + + public Orientations Orientation{ get; set; } + public DateTime? CreationDate{ get; set; } +} \ No newline at end of file diff --git a/MaddoShared/MaddoShared.csproj b/MaddoShared/MaddoShared.csproj index ea64893..ffb858a 100644 --- a/MaddoShared/MaddoShared.csproj +++ b/MaddoShared/MaddoShared.csproj @@ -10,6 +10,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/imagecatalog/Program.cs b/imagecatalog/Program.cs index 854667a..dad280d 100644 --- a/imagecatalog/Program.cs +++ b/imagecatalog/Program.cs @@ -82,6 +82,7 @@ namespace ImageCatalog_2 services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddSingleton(); services.AddSingleton();