Dispose images

This commit is contained in:
Marco 2025-07-24 14:33:48 +02:00
commit 55713c340b
3 changed files with 394 additions and 322 deletions

View file

@ -40,7 +40,7 @@ namespace MaddoShared
stopwatch.Start();
// todo immagini counter
//todo set label
await CreaImmaginiParallel(options, results, updateEvent, cancellationToken);
await ProcessImagesParallel(options, results, updateEvent, cancellationToken);
// todo set finito label
stopwatch.Stop();
@ -49,6 +49,107 @@ namespace MaddoShared
$"{stopwatch.Elapsed.Hours}h {stopwatch.Elapsed.Minutes}m ${stopwatch.Elapsed.Seconds}s ({stopwatch.Elapsed.TotalSeconds}s)";
}
public async Task ProcessImagesParallel(
Options options,
ConcurrentBag<string> results,
EventHandler<Tuple<string, int>> updateEvent,
CancellationToken cancellationToken = default)
{
List<FileData> dataToProcess = GetFilesToProcess(options);
int threads = options.MaxThreads == 0 ? Environment.ProcessorCount * 2 : options.MaxThreads;
Func<FileData, Task> processFile = async fileData =>
{
using var imgCreator = new ImageCreatorSharp(fileData.File, fileData.Directory);
await imgCreator.CreaImmagineThread(fileData.File.Name);
results.Add(fileData.File.Name);
try
{
updateEvent?.Invoke(this, new Tuple<string, int>(fileData.File.Name, dataToProcess.Count));
}
catch (Exception e)
{
logger.LogError(e, "Error in reporting update");
throw;
}
// finally
// {
// imgCreator = null;
// }
};
if (options.LinearExecution)
{
foreach (var fileData in dataToProcess)
await processFile(fileData);
}
else
{
var chunks = options.ChunksSize > 0
? SplitList(dataToProcess, options.ChunksSize)
: new List<List<FileData>> { dataToProcess };
foreach (var chunk in chunks)
{
await chunk.ParallelForEachAsync(
processFile,
maxDegreeOfParallelism: threads,
false,
cancellationToken);
chunk.Clear();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: false, compacting: false);
}
}
}
private List<FileData> GetFilesToProcess(Options options)
{
if (options.AggiornaSottodirectory && options.CreaSottocartelle)
{
var helper = new FileHelperSharp();
return 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
});
}
var files = Directory.EnumerateFiles(
options.SourcePath,
"*.jpg",
options.AggiornaSottodirectory ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
return 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));
// var destDir = new FileInfo(newFilePath).Directory!;
// destDir.Create(); // Ensure exists
//
// return new FileData(fInfo, destDir);
}).ToList();
}
public async Task CreaImmaginiParallel(Options options, ConcurrentBag<string> results,
EventHandler<Tuple<string, int>> updateEvent,
CancellationToken cancellationToken = default(CancellationToken))
@ -115,7 +216,7 @@ namespace MaddoShared
{
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;

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
@ -19,7 +20,8 @@ using SixLabors.ImageSharp.Metadata.Profiles.Exif;
// Imports System.Threading
public class ImageCreatorSharp
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
public class ImageCreatorSharp : IDisposable
{
private bool FotoRuotaADestra = false;
private bool FotoRuotaASinistra = false;
@ -93,41 +95,34 @@ public class ImageCreatorSharp
preparaVariabili();
ExtractExif();
// Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Path.Combine(SourceDir.FullName, NomeFileChild))
using (Image g = Image.FromFile(WorkFile.FullName))
{
using Image g = Image.FromFile(WorkFile.FullName);
// Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(WorkFile.FullName)
// Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(WorkFile.FullName)
// Imposta testo extra
impostaTestoExtra(g);
// Imposta testo extra
impostaTestoExtra(g);
// Ruota l'immagine in base ai dati EXIF
Rotation(g);
// Ruota l'immagine in base ai dati EXIF
Rotation(g);
// Forza jpeg se è selezionata l'opzione
System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;
if (PicSettings.UsaForzaJpg == true)
thisFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
// Forza jpeg se è selezionata l'opzione
System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;
if (PicSettings.UsaForzaJpg == true)
thisFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
prepareThumbnailSize(g);
prepareThumbnailSize(g);
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);
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);
// Crea le miniature
creaMiniature(g, imgOutputBig, thisFormat);
AggiungiTesto(g, imgOutputBig);
AggiungiTesto(g, imgOutputBig);
aggiungiLogo(imgOutputBig);
aggiungiLogo(imgOutputBig);
SalvaFoto(imgOutputBig, thumbSizeBig, nomeFileBig, nomeFileSmall, thumbSizeSmall, thisFormat);
}
}
SalvaFoto(imgOutputBig, thumbSizeBig, nomeFileBig, nomeFileSmall, thumbSizeSmall, thisFormat);
});
// g.Dispose()
@ -497,20 +492,16 @@ public class ImageCreatorSharp
}
else
{
using (Bitmap imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height))
{
imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat);
//imgOutputSmall.Dispose();
}
using var imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height);
imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat);
//imgOutputSmall.Dispose();
}
}
else
{
using (Bitmap imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height))
{
imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat);
//imgOutputSmall.Dispose();
}
using Bitmap imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height);
imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat);
//imgOutputSmall.Dispose();
}
}
}
@ -521,169 +512,165 @@ public class ImageCreatorSharp
private void AggiungiTesto(Image g, Bitmap imgOutputBig)
{
using (var grPhoto = Graphics.FromImage(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);
else
crFont = new Font(PicSettings.IlFont, DimensioneStandard);
crSize = grPhoto.MeasureString(testoFirma, crFont);
LarghezzaStandard = System.Convert.ToInt32(crSize.Width);
if (crSize.Width > System.Convert.ToSingle(g.Width))
{
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);
else
crFont = new Font(PicSettings.IlFont, DimensioneStandard);
crSize = grPhoto.MeasureString(testoFirma, crFont);
LarghezzaStandard = System.Convert.ToInt32(crSize.Width);
if (crSize.Width > System.Convert.ToSingle(g.Width))
{
int Conta = DimensioneStandard;
do
{
if (Conta > 20)
Conta -= 5;
else
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);
if (crSize.Width < System.Convert.ToSingle(g.Width))
{
LarghezzaStandard = System.Convert.ToInt32(crSize.Width);
break;
}
if (Conta <= 5)
break;
}
while (true);
DimensioneStandard = Conta;
}
switch (PicSettings.Posizione.ToUpper())
{
case "ALTO":
{
yPosFromBottom = (PicSettings.Margine);
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)));
break;
}
}
float xCenterOfImg = 0;
float xCenterOfImg3 = 0;
StringFormat 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((g.Width / (double)2));
if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - PicSettings.MargVert)
xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2));
break;
}
case "CENTRO":
{
xCenterOfImg = System.Convert.ToSingle((g.Width / (double)2));
break;
}
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 / (double)2));
if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - PicSettings.MargVert)
xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2));
break;
}
}
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(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));
if (FotoRuotaADestra | FotoRuotaASinistra)
int Conta = DimensioneStandard;
do
{
if (Conta > 20)
Conta -= 5;
else
Conta -= 1;
if (PicSettings.Grassetto == true)
crFont = new Font(PicSettings.IlFont, PicSettings.DimVert, FontStyle.Bold);
crFont = new Font(PicSettings.IlFont, Conta, FontStyle.Bold);
else
crFont = new Font(PicSettings.IlFont, PicSettings.DimVert);
}
else if (PicSettings.Grassetto == true)
crFont = new Font(PicSettings.IlFont, DimensioneStandard, FontStyle.Bold);
else
crFont = new Font(PicSettings.IlFont, DimensioneStandard);
// qui scrive il testo (nomefilebig)
if (PicSettings.TestoNome)
{
if (PicSettings.NomeData & g.PropertyIdList.Length > 0)
crFont = new Font(PicSettings.IlFont, Conta);
crSize = grPhoto.MeasureString(testoFirma, crFont);
if (crSize.Width < System.Convert.ToSingle(g.Width))
{
//ExifReader DatiExif = new ExifReader((Bitmap)g);
dataFoto = _creationDate ?? DateTime.Now; //DatiExif.DateTimeOriginal;
LarghezzaStandard = System.Convert.ToInt32(crSize.Width);
break;
}
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);
}
else
{
grPhoto.DrawString(nomeFileBig, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat);
grPhoto.DrawString(nomeFileBig, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);
}
if (Conta <= 5)
break;
}
if (PicSettings.TestoNome == false)
{
if (FotoRuotaADestra | FotoRuotaASinistra)
{
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);
}
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);
}
}
else
{
grPhoto.DrawString(testoFirma, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat);
grPhoto.DrawString(testoFirma, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);
}
}
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);
}
//grPhoto.Dispose();
crFont?.Dispose();
while (true);
DimensioneStandard = Conta;
}
switch (PicSettings.Posizione.ToUpper())
{
case "ALTO":
{
yPosFromBottom = (PicSettings.Margine);
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)));
break;
}
}
float xCenterOfImg = 0;
float xCenterOfImg3 = 0;
StringFormat 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((g.Width / (double)2));
if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - PicSettings.MargVert)
xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2));
break;
}
case "CENTRO":
{
xCenterOfImg = System.Convert.ToSingle((g.Width / (double)2));
break;
}
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 / (double)2));
if ((LarghezzaStandard / (double)2) > (g.Width / (double)2) - PicSettings.MargVert)
xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2));
break;
}
}
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(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));
if (FotoRuotaADestra | FotoRuotaASinistra)
{
if (PicSettings.Grassetto == true)
crFont = new Font(PicSettings.IlFont, PicSettings.DimVert, FontStyle.Bold);
else
crFont = new Font(PicSettings.IlFont, PicSettings.DimVert);
}
else if (PicSettings.Grassetto == true)
crFont = new Font(PicSettings.IlFont, DimensioneStandard, FontStyle.Bold);
else
crFont = new Font(PicSettings.IlFont, DimensioneStandard);
// qui scrive il testo (nomefilebig)
if (PicSettings.TestoNome)
{
if (PicSettings.NomeData & g.PropertyIdList.Length > 0)
{
//ExifReader DatiExif = new ExifReader((Bitmap)g);
dataFoto = _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);
}
else
{
grPhoto.DrawString(nomeFileBig, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat);
grPhoto.DrawString(nomeFileBig, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);
}
}
if (PicSettings.TestoNome == false)
{
if (FotoRuotaADestra | FotoRuotaASinistra)
{
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);
}
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);
}
}
else
{
grPhoto.DrawString(testoFirma, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat);
grPhoto.DrawString(testoFirma, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);
}
}
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);
}
//grPhoto.Dispose();
crFont?.Dispose();
}
@ -694,7 +681,7 @@ public class ImageCreatorSharp
// imgOutputBig
if (PicSettings.LogoAggiungi == true & File.Exists(PicSettings.LogoNomeFile))
{
Image ImmagineLogo = Image.FromFile(PicSettings.LogoNomeFile);
using var ImmagineLogo = Image.FromFile(PicSettings.LogoNomeFile);
Color LogoColoreTrasparente = Color.White;
// Dim bmWatermark As Bitmap
@ -704,185 +691,169 @@ public class ImageCreatorSharp
// bmWatermark.SetResolution(imgOutputBig.HorizontalResolution, imgOutputBig.VerticalResolution)
// * Load this Bitmap into a new Graphic Object
using (Graphics grWatermark = Graphics.FromImage(imgOutputBig))
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);
ColorMap[] 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);
int FotoLogoH = PicSettings.LogoAltezza;
int FotoLogoW = PicSettings.LogoLarghezza;
double FattoreAlt = ImmagineLogo.Height / (double)FotoLogoH;
double FattoreLarg = ImmagineLogo.Width / (double)FotoLogoW;
Size NuovaSize;
if (FattoreLarg > FattoreAlt)
NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.Height, FotoLogoW, "Larghezza");
else
NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.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;
int xPosOfWm = 0;
int yPosOfWm = 0;
switch (PicSettings.LogoPosizioneH.ToUpper())
{
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);
ColorMap[] 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);
int FotoLogoH = PicSettings.LogoAltezza;
int FotoLogoW = PicSettings.LogoLarghezza;
double FattoreAlt = ImmagineLogo.Height / (double)FotoLogoH;
double FattoreLarg = ImmagineLogo.Width / (double)FotoLogoW;
Size NuovaSize;
if (FattoreLarg > FattoreAlt)
NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.Height, FotoLogoW, "Larghezza");
else
NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.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;
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(ImmagineLogo, new Rectangle(xPosOfWm, yPosOfWm, NuovaSize.Width, NuovaSize.Height), 0, 0, ImmagineLogo.Width, ImmagineLogo.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(ImmagineLogo, new Rectangle(xPosOfWm, yPosOfWm, NuovaSize.Width, NuovaSize.Height), 0, 0, ImmagineLogo.Width, ImmagineLogo.Height, GraphicsUnit.Pixel, imageAttributes);
//grWatermark.Dispose();
}
}
private void SalvaFoto(Bitmap imgOutputBig, Size thumbSizeBig, string NomeFileBig, string NomeFileSmall, Size thumbSizeSmall, ImageFormat thisFormat)
{
using (var image1Stream = new MemoryStream())
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)
if (thisFormat.Equals(ImageFormat.Jpeg))
{
// attenzione non controlla se è png
// imgOutputBig.Save(Path.Combine(_DestDir.FullName, "Temp_" & NomeFileBig), thisFormat)
if (thisFormat.Equals(ImageFormat.Jpeg))
{
MakeImageCustomQuality(imgOutputBig, image1Stream);
}
//SalvaImmagineCustomQuality(imgOutputBig, Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig), PicSettings.jpegQuality);
else
{
imgOutputBig.Save(image1Stream, thisFormat);
}
//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))
{
if (thisFormat.Equals(ImageFormat.Jpeg))
SalvaImmagineCustomQuality(imgOutputBig2, Path.Combine(DestDir.FullName, NomeFileBig), PicSettings.jpegQuality);
else
imgOutputBig2.Save(Path.Combine(DestDir.FullName, NomeFileBig), thisFormat);
//imgOutputBig2.Dispose();
}
}
//imgOutputBig.Dispose();
//g2.Dispose();
MakeImageCustomQuality(imgOutputBig, image1Stream);
}
//SalvaImmagineCustomQuality(imgOutputBig, Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig), PicSettings.jpegQuality);
else
{
imgOutputBig.Save(image1Stream, thisFormat);
}
//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);
if (thisFormat.Equals(ImageFormat.Jpeg))
SalvaImmagineCustomQuality(imgOutputBig2, Path.Combine(DestDir.FullName, NomeFileBig), PicSettings.jpegQuality);
else
imgOutputBig2.Save(Path.Combine(DestDir.FullName, NomeFileBig), thisFormat);
//imgOutputBig2.Dispose();
//imgOutputBig.Dispose();
//g2.Dispose();
}
else
{
//
if (thisFormat.Equals(ImageFormat.Jpeg))
SalvaImmagineCustomQuality(imgOutputBig, Path.Combine(DestDir.FullName, NomeFileBig), PicSettings.jpegQuality);
else
imgOutputBig.Save(Path.Combine(DestDir.FullName, 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(imgOutputBig, Path.Combine(DestDir.FullName, NomeFileBig), PicSettings.jpegQuality);
SalvaImmagineCustomQuality(imgOutputSmall, Path.Combine(DestDir.FullName, NomeFileSmall), PicSettings.jpegQualityMin);
else
imgOutputBig.Save(Path.Combine(DestDir.FullName, NomeFileBig), thisFormat);
imgOutputSmall.Save(Path.Combine(_DestDir.FullName, NomeFileSmall), thisFormat);
//imgOutputBig.Dispose();
//imgOutputSmall.Dispose();
//g1.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);
//imgOutputSmall.Dispose();
}
//g1.Dispose();
}
}
}
//if (File.Exists(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig)))
// File.Delete(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig));
}
//if (File.Exists(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig)))
// File.Delete(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig));
}
private void SalvaImmagineCustomQuality(Bitmap imageToSave, string nomeFileFinale, long quality)
@ -925,12 +896,7 @@ public class ImageCreatorSharp
}
return null/* TODO Change to default(_) if this is not a reference type */;
}
/// <summary>
/// ''' Calculate the Size of the New image
/// ''' </summary>
@ -1014,4 +980,9 @@ public class ImageCreatorSharp
_NomeFileChild = value;
}
}
public void Dispose()
{
}
}

View file

@ -1676,7 +1676,7 @@ namespace ImageCatalog
imageCreationOptions.NumerazioneType = GetNumerazioneEnum();
imageCreationOptions.SourcePath = Model.SourcePath;
imageCreationOptions.DestinationPath = Model.DestinationPath;
await imgStf.CreaImmaginiParallel(imageCreationOptions, _results, UiUpdateEvent);
await imgStf.ProcessImagesParallel(imageCreationOptions, _results, UiUpdateEvent);
// Await CreaImmaginiParallel(txtSorgente.Text, txtDestinazione.Text)