This commit is contained in:
Marco 2025-07-29 10:34:23 +02:00
commit 1cd71c54fc
5 changed files with 964 additions and 886 deletions

View file

@ -19,7 +19,6 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
{
public async Task CreaImmagineThread(ImageState imgState, Image logo)
{
try
{
await Task.Run(() =>
@ -56,14 +55,8 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
SalvaFoto(imgOutputBig, imgState, thisFormat);
});
// g.Dispose()
//GC.Collect();
}
// _picSettings.mainForm.stepProgressBar()
catch (Exception ex)
{
var e = ex.Demystify();
@ -80,7 +73,7 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
var found = img.Metadata?.ExifProfile?.TryGetValue(ExifTag.Orientation, out rotation) ?? false;
if (found )
if (found)
{
var intOrientation = rotation.Value.ToInt32();
imgState.Orientation = (Orientations)intOrientation;
@ -90,7 +83,8 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
var creationFound = img.Metadata?.ExifProfile?.TryGetValue(ExifTag.DateTimeOriginal, out date) ?? false;
if (creationFound)
{
var succ = DateTime.TryParseExact(date.Value, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out var crDate);
var succ = DateTime.TryParseExact(date.Value, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture,
DateTimeStyles.None, out var crDate);
if (succ)
{
imgState.CreationDate = crDate;
@ -111,27 +105,21 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
imgState.FotoRuotaADestra = false;
imgState.FotoRuotaASinistra = false;
if (picSettings.UsaRotazioneAutomatica == true)
if (picSettings.UsaRotazioneAutomatica && g.PropertyIdList.Length > 0)
{
if (g.PropertyIdList.Length > 0)
switch (imgState.Orientation)
{
//ExifReader DatiExif = new ExifReader((Bitmap)g);
switch (imgState.Orientation /*DatiExif.Orientation*/)
{
case Orientations.BottomLeft:
case Orientations.BottomRight:
case Orientations.LeftTop:
case Orientations.LftBottom:
imgState.FotoRuotaASinistra = true;
break;
case Orientations.RightBottom:
case Orientations.RightTop:
case Orientations.TopLeft:
case Orientations.TopRight:
break;
}
case Orientations.BottomLeft:
case Orientations.BottomRight:
case Orientations.LeftTop:
case Orientations.LftBottom:
imgState.FotoRuotaASinistra = true;
break;
case Orientations.RightBottom:
case Orientations.RightTop:
case Orientations.TopLeft:
case Orientations.TopRight:
break;
}
}
@ -149,7 +137,9 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
/// ''' <remarks></remarks>
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) return;
//ExifReader DatiExif = new ExifReader((Bitmap)g);
@ -170,7 +160,7 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
if (picSettings.UsaTempoGaraTestoApplicare != true) return;
var diff = imgState.DataFoto - imgState.DataPartenzaI;
//var diffA = diff.TotalSeconds * 10000000;
// var orario = new TimeSpan(0, Math.Abs(diff.Hours), Math.Abs(diff.Minutes), (int)diffA);
imgState.TestoFirma += $" {imgState.TestoOrario}{diff.Hours:00}:{diff.Minutes:00}:{diff.Seconds:00}";
imgState.TestoFirmaV += $" {imgState.TestoOrario}{diff.Hours:00}:{diff.Minutes:00}:{diff.Seconds:00}";
@ -209,7 +199,7 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
imgState.NomeFileBig = imgState.WorkFile.Name;
}
private void PrepareThumbnailSize(Image g, ImageState imgState)
private void PrepareThumbnailSize(Image g, ImageState imgState)
{
if (g.Width > g.Height)
{
@ -225,209 +215,403 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
}
}
private void CreaMiniature(Image g, ImageState imgState, Bitmap imgOutputBig, ImageFormat thisFormat)
// private void CreaMiniature(Image g, ImageState imgState, Bitmap imgOutputBig, ImageFormat thisFormat)
// {
// if (!picSettings.CreaMiniature || picSettings.AggiungiScritteMiniature)
// return;
//
// if (picSettings.TestoMin)
// imgState.TestoFirmaPiccola = imgState.NomeFileBig;
// else if (picSettings.AggNumTempMin)
// imgState.TestoFirmaPiccola = imgState.NomeFileBig + " ";
//
// Font crFont1 = null;
// Font crFont2 = null;
// var crSize1 = new SizeF();
// var crSize2 = new SizeF();
//
//
// 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 (imgState.TestoFirmaPiccola.Length > 0)
// {
// 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)
// {
// 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);
//
// 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));
// //FileSystem.Kill();
// }
// else
// {
// 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 var imgOutputSmall =
// new Bitmap(g, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height);
// imgOutputSmall.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat);
// //imgOutputSmall.Dispose();
// }
//
// crFont1?.Dispose();
// crFont2?.Dispose();
// }
private void CreaMiniature(Image sourceImage, ImageState imgState, Bitmap imgOutputBig, ImageFormat format)
{
if (!picSettings.CreaMiniature || picSettings.AggiungiScritteMiniature)
return;
PrepareSignatureText(imgState);
if (IsSameDirectory(picSettings.DirectorySorgente, picSettings.DirectoryDestinazione))
UpdateFileNameWithCode(imgState);
if (ShouldRenderText())
CreateMiniatureWithText(sourceImage, imgState, imgOutputBig, format);
else
CreateSimpleThumbnail(sourceImage, imgState, format);
}
private void PrepareSignatureText(ImageState imgState)
{
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 */;
var crSize1 = new SizeF();
var crSize2 = new SizeF();
private bool IsSameDirectory(string dir1, string dir2) =>
string.Equals(dir1, dir2, StringComparison.OrdinalIgnoreCase);
if (picSettings.CreaMiniature == true)
private void UpdateFileNameWithCode(ImageState imgState)
{
var name = imgState.NomeFileSmall;
imgState.NomeFileSmall = name[..^4] + picSettings.Codice + name[^4..];
}
private bool ShouldRenderText() =>
picSettings.UsaOrarioMiniatura || picSettings.TestoMin || picSettings.AggTempoGaraMin ||
picSettings.AggNumTempMin;
private void CreateSimpleThumbnail(Image image, ImageState imgState, ImageFormat format)
{
using var thumbnail = new Bitmap(image, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height);
thumbnail.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), format);
}
private void CreateMiniatureWithText(Image image, ImageState imgState, Bitmap sourceBitmap, ImageFormat format)
{
if (imgState.TestoFirmaPiccola.Length == 0)
{
if (picSettings.AggiungiScritteMiniature == false)
{
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 (imgState.TestoFirmaPiccola.Length > 0)
{
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)
{
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));
//FileSystem.Kill();
}
else
{
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 var imgOutputSmall = new Bitmap(g, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height);
imgOutputSmall.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat);
//imgOutputSmall.Dispose();
}
}
CreateSimpleThumbnail(image, imgState, format);
return;
}
crFont1?.Dispose();
crFont2?.Dispose();
using var imgOutputSmall = (Bitmap)sourceBitmap.Clone();
using var graphics = Graphics.FromImage(imgOutputSmall);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
imgState.DimensioneStandardMiniatura = 50;
using var font1 = CreateFont(picSettings.IlFont, imgState.DimensioneStandardMiniatura, picSettings.Grassetto);
var textSize = graphics.MeasureString(imgState.TestoFirmaPiccola, font1);
AdjustFontToFitWidth(graphics, image.Width, imgState, ref textSize);
SetVerticalPosition(image.Height, textSize.Height, imgState);
float xCenter = CalculateHorizontalAlignment(image.Width, textSize.Width);
using var stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
using var shadowBrush = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, 0, 0, 0));
using var textBrush = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, picSettings.FontColoreRGB));
imgState.DimensioneStandardMiniatura = picSettings.DimMin;
using var finalFont =
CreateFont(picSettings.IlFont, imgState.DimensioneStandardMiniatura, picSettings.Grassetto);
DrawText(graphics, imgState, xCenter, stringFormat, shadowBrush, textBrush, finalFont);
using var finalThumb =
new Bitmap(imgOutputSmall, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height);
finalThumb.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), format);
}
private Font CreateFont(string fontName, int size, bool bold) =>
new Font(fontName, size, bold ? FontStyle.Bold : FontStyle.Regular);
private void AdjustFontToFitWidth(Graphics g, int maxWidth, ImageState imgState, ref SizeF size)
{
int currentSize = imgState.DimensioneStandardMiniatura;
while (size.Width > maxWidth && currentSize > 5)
{
currentSize = (currentSize > 20) ? currentSize - 5 : currentSize - 1;
using var tempFont = CreateFont(picSettings.IlFont, currentSize, picSettings.Grassetto);
size = g.MeasureString(imgState.TestoFirmaPiccola, tempFont);
//tempFont.Dispose();
}
imgState.DimensioneStandardMiniatura = currentSize;
}
private void SetVerticalPosition(int imgHeight, float textHeight, ImageState imgState)
{
switch (picSettings.Posizione.ToUpper())
{
case "ALTO":
imgState.YPosFromBottom1 = picSettings.Margine;
imgState.YPosFromBottom4 = picSettings.MargVert;
break;
case "BASSO":
imgState.YPosFromBottom1 = (float)(imgHeight - textHeight - (imgHeight * picSettings.Margine / 100.0));
imgState.YPosFromBottom4 = (float)(imgHeight - textHeight - (imgHeight * picSettings.MargVert / 100.0));
break;
}
}
private float CalculateHorizontalAlignment(int imgWidth, float textWidth)
{
double halfWidth = textWidth / 2.0;
return picSettings.Allineamento.ToUpper() switch
{
"SINISTRA" => (float)Math.Min(picSettings.Margine + halfWidth, imgWidth / 2.0),
"DESTRA" => (float)Math.Max(imgWidth - picSettings.Margine - halfWidth, imgWidth / 2.0),
_ => imgWidth / 2.0f, // CENTRO or default
};
}
private void DrawText(Graphics g, ImageState imgState, float x, StringFormat format,
Brush shadowBrush, Brush textBrush, Font font)
{
string content = imgState.TestoFirmaPiccola;
if (picSettings.TestoMin)
{
content = imgState.NomeFileBig;
}
else if (picSettings.AggTempoGaraMin && picSettings.UsaTempoGaraTestoApplicare)
{
content = FormatTimeText(imgState, includeFileName: false);
}
else if (picSettings.AggNumTempMin)
{
content = FormatTimeText(imgState, includeFileName: true);
}
var offset = new PointF(x + 1, imgState.YPosFromBottom1 + 1);
var actual = new PointF(x, imgState.YPosFromBottom1);
g.DrawString(content, font, shadowBrush, offset, format);
g.DrawString(content, font, textBrush, actual, format);
}
private string FormatTimeText(ImageState imgState, bool includeFileName)
{
var diff = imgState.DataPartenzaI - imgState.DataFoto;
var ticks = (long)(diff.TotalSeconds * 10000000);
var time = new TimeSpan(ticks);
var formatted = $"{imgState.TestoOrario}{time:hh\\:mm\\:ss}";
return includeFileName
? $"{imgState.NomeFileBig}{Environment.NewLine}{formatted}"
: Environment.NewLine + formatted;
}
private void AggiungiTesto(Image g, ImageState imgState, Bitmap imgOutputBig)
@ -435,7 +619,7 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
using var grPhoto = Graphics.FromImage(imgOutputBig);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
Font crFont = null/* TODO Change to default(_) if this is not a reference type */;
Font crFont = null /* TODO Change to default(_) if this is not a reference type */;
if (picSettings.Grassetto == true)
crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard, FontStyle.Bold);
@ -466,11 +650,11 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
if (conta <= 5)
break;
}
while (true);
} while (true);
imgState.DimensioneStandard = conta;
}
switch (picSettings.Posizione.ToUpper())
{
case "ALTO":
@ -482,8 +666,12 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
case "BASSO":
{
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)));
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;
}
}
@ -512,8 +700,10 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
case "DESTRA":
{
xCenterOfImg = System.Convert.ToSingle((g.Width - picSettings.Margine - (larghezzaStandard / (double)2)));
xCenterOfImg3 = System.Convert.ToSingle((g.Width - picSettings.MargVert - (larghezzaStandard / (double)2)));
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)
@ -521,6 +711,7 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
break;
}
}
strFormat.Alignment = StringAlignment.Center;
using var semiTransBrush2 = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, 0, 0, 0));
@ -548,13 +739,17 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
//ExifReader DatiExif = new ExifReader((Bitmap)g);
imgState.DataFoto = imgState.CreationDate ?? DateTime.Now; //DatiExif.DateTimeOriginal;
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);
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(imgState.NomeFileBig, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, imgState.YPosFromBottom + 1), strFormat);
grPhoto.DrawString(imgState.NomeFileBig, crFont, semiTransBrush, new PointF(xCenterOfImg, imgState.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);
}
}
@ -564,23 +759,31 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
{
if (picSettings.TestoMin == false)
{
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);
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)
{
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);
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(imgState.TestoFirma, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, imgState.YPosFromBottom + 1), strFormat);
grPhoto.DrawString(imgState.TestoFirma, crFont, semiTransBrush, new PointF(xCenterOfImg, imgState.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))
{
imgState.NomeFileBig2 = imgState.NomeFileBig;
imgState.NomeFileBig = $"{imgState.NomeFileBig[..^4]}{picSettings.Codice}{imgState.NomeFileBig[^4..]}";
@ -614,7 +817,13 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
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
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 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);
@ -622,11 +831,14 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
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");
var nuovaSize = fattoreLarg > fattoreAlt
? NewthumbSize(logo.Width, logo.Height, fotoLogoW, "Larghezza")
: NewthumbSize(logo.Width, logo.Height, fotoLogoH, "Altezza");
var inPercentualeL = picSettings.LogoMargine.EndsWith('%');
var margineL = System.Convert.ToInt32(picSettings.LogoMargine);
var margineUsato = inPercentualeL ? System.Convert.ToInt32(imgOutputBig.Height * margineL / (double)100) : margineL;
var margineUsato =
inPercentualeL ? System.Convert.ToInt32(imgOutputBig.Height * margineL / (double)100) : margineL;
int xPosOfWm = 0;
int yPosOfWm = 0;
@ -651,6 +863,7 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
break;
}
}
switch (picSettings.LogoPosizioneV.ToUpper())
{
case "ALTO":
@ -673,16 +886,16 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
}
}
grWatermark.DrawImage(logo, new Rectangle(xPosOfWm, yPosOfWm, nuovaSize.Width, nuovaSize.Height), 0, 0, logo.Width, logo.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();
}
private void SalvaFoto(Bitmap imgOutputBig, ImageState imgState, ImageFormat thisFormat)
{
var fileName = Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig);
using var image1Stream = new MemoryStream();
if (picSettings.FotoGrandeDimOrigina == false)
{
@ -697,10 +910,13 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
{
imgOutputBig.Save(image1Stream, thisFormat);
}
//imgOutputBig.Save(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig), thisFormat);
image1Stream.Seek(0, SeekOrigin.Begin);
using var g2 = Image.FromStream(image1Stream);
imgState.ThumbSizeBig = g2.Width > g2.Height ? NewthumbSize(g2.Width, g2.Height, picSettings.LarghezzaBig, "Larghezza") : NewthumbSize(g2.Width, g2.Height, picSettings.AltezzaBig, "Altezza");
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 (!picSettings.OverwriteFiles && File.Exists(fileName))
@ -714,7 +930,6 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
else
imgOutputBig2.Save(fileName, thisFormat);
}
}
else
{
@ -730,19 +945,22 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
imgOutputBig.Save(fileName, thisFormat);
}
}
image1Stream.Seek(0, SeekOrigin.Begin);
if (!picSettings.CreaMiniature) return;
if (!picSettings.AggiungiScritteMiniature) return;
using var g1 = picSettings.FotoGrandeDimOrigina ? (Image)imgOutputBig.Clone() : Image.FromStream(image1Stream);
using var 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 (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);
var tnFileName = Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall);
if (!picSettings.OverwriteFiles && File.Exists(tnFileName))
@ -784,19 +1002,20 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
imageToSave.Save(destinationStream, jgpEncoder, myEncoderParameters);
//imageToSave.Dispose();
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
var codecs = ImageCodecInfo.GetImageDecoders();
foreach (var codec in codecs)
{
if (codec.FormatID == format.Guid)
return codec;
}
return null/* TODO Change to default(_) if this is not a reference type */;
return null /* TODO Change to default(_) if this is not a reference type */;
}
/// <summary>
/// ''' Calculate the Size of the New image
/// ''' </summary>
@ -822,10 +1041,9 @@ public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorShar
else
tempMultiplier = maxPixel / (double)currentwidth;
var 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;
}
}