Catalog/MaddoShared/ImageCreatorSharp.cs

821 lines
37 KiB
C#
Raw Normal View History

2021-02-25 11:14:44 +01:00
using System;
using System.Diagnostics;
2025-07-24 14:33:48 +02:00
using System.Diagnostics.CodeAnalysis;
2021-02-25 11:14:44 +01:00
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
2025-07-28 11:25:46 +02:00
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
2021-02-25 11:14:44 +01:00
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
// Imports System.Threading
2025-07-28 11:25:46 +02:00
namespace MaddoShared;
2025-07-24 14:33:48 +02:00
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
2025-07-28 11:25:46 +02:00
public class ImageCreatorSharp(PicSettings picSettings, ILogger<ImageCreatorSharp> logger)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
public async Task CreaImmagineThread(ImageState imgState, Image logo)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
2021-02-25 11:14:44 +01:00
try
{
await Task.Run(() =>
{
2025-07-28 11:25:46 +02:00
logger.LogInformation("File: {FileInfo} Dest: {DirectoryInfo}", imgState.WorkFile, imgState.DestDir);
PreparaVariabili(imgState);
ExtractExif(imgState);
using var g = Image.FromFile(imgState.WorkFile.FullName);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
// Imposta testo extra
2025-07-28 11:25:46 +02:00
ImpostaTestoExtra(g, imgState);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
// Ruota l'immagine in base ai dati EXIF
2025-07-28 11:25:46 +02:00
Rotation(g, imgState);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
// Forza jpeg se è selezionata l'opzione
2025-07-28 11:25:46 +02:00
var thisFormat = g.RawFormat;
if (picSettings.UsaForzaJpg == true)
thisFormat = ImageFormat.Jpeg;
PrepareThumbnailSize(g, imgState);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
using var imgOutputBig = new Bitmap(g, imgState.ThumbSizeBig.Width, imgState.ThumbSizeBig.Height);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
imgOutputBig.SetResolution(g.HorizontalResolution, g.VerticalResolution);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
// Crea le miniature
2025-07-28 11:25:46 +02:00
CreaMiniature(g, imgState, imgOutputBig, thisFormat);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
AggiungiTesto(g, imgState, imgOutputBig);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
AggiungiLogo(imgOutputBig, imgState, logo);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
SalvaFoto(imgOutputBig, imgState, thisFormat);
2021-02-25 11:14:44 +01:00
});
// g.Dispose()
//GC.Collect();
}
2025-07-28 10:22:08 +02:00
// _picSettings.mainForm.stepProgressBar()
2021-02-25 11:14:44 +01:00
catch (Exception ex)
{
var e = ex.Demystify();
Console.WriteLine(e);
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
2025-07-28 11:25:46 +02:00
private void ExtractExif(ImageState imgState)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
using var img = SixLabors.ImageSharp.Image.Load(imgState.WorkFile.FullName);
imgState.Orientation = Orientations.TopLeft;
2024-10-14 19:54:29 +02:00
IExifValue<ushort> rotation = null;
2024-10-14 19:54:29 +02:00
var found = img.Metadata?.ExifProfile?.TryGetValue(ExifTag.Orientation, out rotation) ?? false;
2024-10-14 19:54:29 +02:00
if (found )
{
var intOrientation = rotation.Value.ToInt32();
2025-07-28 11:25:46 +02:00
imgState.Orientation = (Orientations)intOrientation;
}
2021-02-25 11:14:44 +01:00
IExifValue<string> date = null;
var creationFound = img.Metadata?.ExifProfile?.TryGetValue(ExifTag.DateTime, out date) ?? false;
if (creationFound)
{
var succ = DateTime.TryParse(date.ToString(), out var crDate);
if (succ)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
imgState.CreationDate = crDate;
2021-02-25 11:14:44 +01:00
}
else
{
2025-07-28 11:25:46 +02:00
imgState.CreationDate = null;
2021-02-25 11:14:44 +01:00
}
}
else
{
2025-07-28 11:25:46 +02:00
imgState.CreationDate = null;
2021-02-25 11:14:44 +01:00
}
}
2025-07-28 11:25:46 +02:00
private void Rotation(System.Drawing.Image g, ImageState imgState)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
imgState.FotoRuotaADestra = false;
imgState.FotoRuotaASinistra = false;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
if (picSettings.UsaRotazioneAutomatica == true)
2021-02-25 11:14:44 +01:00
{
if (g.PropertyIdList.Length > 0)
{
//ExifReader DatiExif = new ExifReader((Bitmap)g);
2025-07-28 11:25:46 +02:00
switch (imgState.Orientation /*DatiExif.Orientation*/)
2021-02-25 11:14:44 +01:00
{
2025-07-28 09:59:58 +02:00
case Orientations.BottomLeft:
case Orientations.BottomRight:
case Orientations.LeftTop:
case Orientations.LftBottom:
2025-07-28 11:25:46 +02:00
imgState.FotoRuotaASinistra = true;
2021-02-25 11:14:44 +01:00
break;
2025-07-28 09:59:58 +02:00
case Orientations.RightBottom:
case Orientations.RightTop:
case Orientations.TopLeft:
case Orientations.TopRight:
2021-02-25 11:14:44 +01:00
break;
}
}
}
2025-07-28 11:25:46 +02:00
if (imgState.FotoRuotaASinistra)
2021-02-25 11:14:44 +01:00
g.RotateFlip(RotateFlipType.Rotate270FlipNone);
2025-07-28 11:25:46 +02:00
if (imgState.FotoRuotaADestra)
2021-02-25 11:14:44 +01:00
g.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
2025-07-28 11:25:46 +02:00
2021-02-25 11:14:44 +01:00
/// <summary>
/// ''' Aggiunge Orario, tempo gara e altri
/// ''' </summary>
/// ''' <param name="g">Image</param>
2025-07-28 11:25:46 +02:00
/// <param name="imgState"></param>
2021-02-25 11:14:44 +01:00
/// ''' <remarks></remarks>
2025-07-28 11:25:46 +02:00
private void ImpostaTestoExtra(Image g, ImageState imgState)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
if (picSettings.UsaOrarioTestoApplicare | picSettings.UsaTempoGaraTestoApplicare | picSettings.UsaOrarioMiniatura | picSettings.TestoMin | picSettings.AggTempoGaraMin | picSettings.AggNumTempMin)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
imgState.TestoFirma +=
$" {imgState.DataFoto.ToShortDateString()} {imgState.DataFoto.ToLongTimeString()}";
imgState.TestoFirmaV +=
$" {imgState.DataFoto.ToShortDateString()} {imgState.DataFoto.ToLongTimeString()}";
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
if (picSettings.UsaTempoGaraTestoApplicare != true) return;
var diff = imgState.DataPartenzaI - imgState.DataFoto;
var diffA = diff.TotalSeconds * 10000000;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
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}";
2021-02-25 11:14:44 +01:00
}
else
{
2025-07-28 11:25:46 +02:00
imgState.TestoFirma = picSettings.TestoFirmaStart;
imgState.TestoFirmaV = picSettings.TestoFirmaStartV;
2021-02-25 11:14:44 +01:00
}
}
/// <summary>
/// ''' Prepara diverse variabili azzerandole, elaborandole e prendendole dalle impostazioni
/// ''' </summary>
/// ''' <remarks></remarks>
2025-07-28 11:25:46 +02:00
private void PreparaVariabili(ImageState imgState)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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;
2021-02-25 11:14:44 +01:00
// nomeFileSmall = Suffisso & NomeFileChild
// nomeFileBig = NomeFileChild
2025-07-28 11:25:46 +02:00
imgState.NomeFileSmall = picSettings.Suffisso + imgState.WorkFile.Name;
imgState.NomeFileBig = imgState.WorkFile.Name;
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
private void PrepareThumbnailSize(Image g, ImageState imgState)
2021-02-25 11:14:44 +01:00
{
if (g.Width > g.Height)
{
2025-07-28 11:25:46 +02:00
imgState.ThumbSizeSmall = NewthumbSize(g.Width, g.Height, picSettings.LarghezzaSmall, "Larghezza");
var sizeOrig = new Size(g.Width, g.Height);
imgState.ThumbSizeBig = sizeOrig;
2021-02-25 11:14:44 +01:00
}
else
{
2025-07-28 11:25:46 +02:00
imgState.ThumbSizeSmall = NewthumbSize(g.Width, g.Height, picSettings.AltezzaSmall, "Altezza");
var sizeOrig = new Size(g.Width, g.Height);
imgState.ThumbSizeBig = sizeOrig;
2021-02-25 11:14:44 +01:00
}
}
2025-07-28 11:25:46 +02:00
private void CreaMiniature(Image g, ImageState imgState, Bitmap imgOutputBig, ImageFormat thisFormat)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
if (picSettings.TestoMin)
imgState.TestoFirmaPiccola = imgState.NomeFileBig;
else if (picSettings.AggNumTempMin)
imgState.TestoFirmaPiccola = imgState.NomeFileBig + " ";
2021-02-25 11:14:44 +01:00
// 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 */;
2025-07-28 11:25:46 +02:00
var crSize1 = new SizeF();
var crSize2 = new SizeF();
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
if (picSettings.CreaMiniature == true)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
if (picSettings.AggiungiScritteMiniature == false)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
if (imgState.TestoFirmaPiccola.Length > 0)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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);
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
crSize1 = grPhoto1.MeasureString(imgState.TestoFirmaPiccola, crFont1);
crSize2 = grPhoto1.MeasureString(imgState.TestoFirma, crFont1);
var larghezzaStandard1 = System.Convert.ToInt32(crSize1.Width);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
if (crSize1.Width > System.Convert.ToSingle(g.Width))
{
int Conta = imgState.DimensioneStandardMiniatura;
do
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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))
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
larghezzaStandard1 = System.Convert.ToInt32(crSize1.Width);
break;
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
if (Conta <= 5)
break;
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
while (true);
imgState.DimensioneStandardMiniatura = Conta;
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
switch (picSettings.Posizione.ToUpper())
{
case "ALTO":
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
imgState.YPosFromBottom1 = (picSettings.Margine);
imgState.YPosFromBottom4 = (picSettings.MargVert);
break;
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
case "BASSO":
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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;
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
float xCenterOfImg1 = 0;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
using StringFormat strFormat1 = new StringFormat();
switch (picSettings.Allineamento.ToUpper())
{
case "SINISTRA":
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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;
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
case "CENTRO":
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2));
break;
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
case "DESTRA":
{
xCenterOfImg1 = System.Convert.ToSingle((g.Width - picSettings.Margine - (larghezzaStandard1 / (double)2)));
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
if ((larghezzaStandard1 / (double)2) > (g.Width / (double)2) - picSettings.Margine)
xCenterOfImg1 = System.Convert.ToSingle((g.Width / (double)2));
break;
}
}
strFormat1.Alignment = StringAlignment.Center;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
using var semiTransBrush21 = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, 0, 0, 0));
using var semiTransBrush1 = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, picSettings.FontColoreRGB));
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
// quick fix
imgState.DimensioneStandardMiniatura = picSettings.DimMin;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
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;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
var orario = new TimeSpan(0, 0, (int)diffA);/* new TimeSpan(DateTime.DateDiff(DateInterval.Second, dataPartenzaI, dataFoto) * 10000000);*/
string tempstr = "";
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
tempstr += Environment.NewLine + imgState.TestoOrario + orario.Hours.ToString("00") + ":" + orario.Minutes.ToString("00") + ":" + orario.Seconds.ToString("00");
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02: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;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
tempstr += Environment.NewLine + imgState.TestoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00");
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02: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);
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
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();
2021-02-25 11:14:44 +01:00
//File.Delete(Path.Combine(DestDir.FullName, "Temp_" + nomeFileSmall));
//FileSystem.Kill();
}
else
{
2025-07-28 11:25:46 +02:00
using var imgOutputSmall = new Bitmap(g, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height);
imgOutputSmall.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat);
2025-07-24 14:33:48 +02:00
//imgOutputSmall.Dispose();
2021-02-25 11:14:44 +01:00
}
}
else
{
2025-07-28 11:25:46 +02:00
using var imgOutputSmall = new Bitmap(g, imgState.ThumbSizeSmall.Width, imgState.ThumbSizeSmall.Height);
imgOutputSmall.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileSmall), thisFormat);
2025-07-24 14:33:48 +02:00
//imgOutputSmall.Dispose();
2021-02-25 11:14:44 +01:00
}
}
}
crFont1?.Dispose();
crFont2?.Dispose();
}
2025-07-28 11:25:46 +02:00
private void AggiungiTesto(Image g, ImageState imgState, Bitmap imgOutputBig)
2021-02-25 11:14:44 +01:00
{
2025-07-24 14:33:48 +02:00
using var grPhoto = Graphics.FromImage(imgOutputBig);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
Font crFont = null/* TODO Change to default(_) if this is not a reference type */;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
if (picSettings.Grassetto == true)
crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard, FontStyle.Bold);
2025-07-24 14:33:48 +02:00
else
2025-07-28 11:25:46 +02:00
crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard);
var crSize = grPhoto.MeasureString(imgState.TestoFirma, crFont);
var larghezzaStandard = System.Convert.ToInt32(crSize.Width);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
if (crSize.Width > System.Convert.ToSingle(g.Width))
{
2025-07-28 11:25:46 +02:00
int conta = imgState.DimensioneStandard;
2025-07-24 14:33:48 +02:00
do
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
if (conta > 20)
conta -= 5;
2025-07-24 14:33:48 +02:00
else
2025-07-28 11:25:46 +02:00
conta -= 1;
if (picSettings.Grassetto == true)
crFont = new Font(picSettings.IlFont, conta, FontStyle.Bold);
2025-07-24 14:33:48 +02:00
else
2025-07-28 11:25:46 +02:00
crFont = new Font(picSettings.IlFont, conta);
crSize = grPhoto.MeasureString(imgState.TestoFirma, crFont);
2025-07-24 14:33:48 +02:00
if (crSize.Width < System.Convert.ToSingle(g.Width))
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
larghezzaStandard = System.Convert.ToInt32(crSize.Width);
2025-07-24 14:33:48 +02:00
break;
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
2025-07-28 11:25:46 +02:00
if (conta <= 5)
2025-07-24 14:33:48 +02:00
break;
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
while (true);
2025-07-28 11:25:46 +02:00
imgState.DimensioneStandard = conta;
2025-07-24 14:33:48 +02:00
}
2025-07-28 11:25:46 +02:00
switch (picSettings.Posizione.ToUpper())
2025-07-24 14:33:48 +02:00
{
case "ALTO":
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
imgState.YPosFromBottom = (picSettings.Margine);
imgState.YPosFromBottom3 = (picSettings.MargVert);
2025-07-24 14:33:48 +02:00
break;
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
case "BASSO":
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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)));
2025-07-24 14:33:48 +02:00
break;
}
}
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
float xCenterOfImg = 0;
float xCenterOfImg3 = 0;
2025-07-28 11:25:46 +02:00
using var strFormat = new StringFormat();
switch (picSettings.Allineamento.ToUpper())
2025-07-24 14:33:48 +02:00
{
case "SINISTRA":
{
2025-07-28 11:25:46 +02:00
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)
2025-07-24 14:33:48 +02:00
xCenterOfImg = System.Convert.ToSingle((g.Width / (double)2));
2025-07-28 11:25:46 +02:00
if ((larghezzaStandard / (double)2) > (g.Width / (double)2) - picSettings.MargVert)
2025-07-24 14:33:48 +02:00
xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2));
break;
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
case "CENTRO":
{
xCenterOfImg = System.Convert.ToSingle((g.Width / (double)2));
break;
}
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
case "DESTRA":
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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)
2025-07-24 14:33:48 +02:00
xCenterOfImg = System.Convert.ToSingle((g.Width / (double)2));
2025-07-28 11:25:46 +02:00
if ((larghezzaStandard / (double)2) > (g.Width / (double)2) - picSettings.MargVert)
2025-07-24 14:33:48 +02:00
xCenterOfImg3 = System.Convert.ToSingle((g.Width / (double)2));
break;
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
}
2025-07-28 11:25:46 +02:00
strFormat.Alignment = StringAlignment.Center;
2025-07-24 14:33:48 +02:00
2025-07-28 11:25:46 +02:00
using var semiTransBrush2 = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, 0, 0, 0));
2025-07-24 14:33:48 +02:00
// Dim semiTransBrush As SolidBrush = New SolidBrush(Color.FromArgb(AlphaScelta, _FontColoreR, _FontColoreG, _FontColoreB))
2025-07-28 11:25:46 +02:00
using var semiTransBrush = new SolidBrush(Color.FromArgb(imgState.AlphaScelta, picSettings.FontColoreRGB));
2025-07-24 14:33:48 +02:00
2025-07-28 11:25:46 +02:00
if (imgState.FotoRuotaADestra | imgState.FotoRuotaASinistra)
2025-07-24 14:33:48 +02:00
{
2025-07-28 11:25:46 +02:00
if (picSettings.Grassetto == true)
crFont = new Font(picSettings.IlFont, picSettings.DimVert, FontStyle.Bold);
2021-02-25 11:14:44 +01:00
else
2025-07-28 11:25:46 +02:00
crFont = new Font(picSettings.IlFont, picSettings.DimVert);
2025-07-24 14:33:48 +02:00
}
2025-07-28 11:25:46 +02:00
else if (picSettings.Grassetto == true)
crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard, FontStyle.Bold);
2025-07-24 14:33:48 +02:00
else
2025-07-28 11:25:46 +02:00
crFont = new Font(picSettings.IlFont, imgState.DimensioneStandard);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
// qui scrive il testo (nomefilebig)
2025-07-28 11:25:46 +02:00
if (picSettings.TestoNome)
2025-07-24 14:33:48 +02:00
{
2025-07-28 11:25:46 +02:00
if (picSettings.NomeData & g.PropertyIdList.Length > 0)
2021-02-25 11:14:44 +01:00
{
2025-07-24 14:33:48 +02:00
//ExifReader DatiExif = new ExifReader((Bitmap)g);
2025-07-28 11:25:46 +02:00
imgState.DataFoto = imgState.CreationDate ?? DateTime.Now; //DatiExif.DateTimeOriginal;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
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);
2025-07-24 14:33:48 +02:00
}
else
{
2025-07-28 11:25:46 +02:00
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);
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
if (picSettings.TestoNome == false)
2025-07-24 14:33:48 +02:00
{
2025-07-28 11:25:46 +02:00
if (imgState.FotoRuotaADestra | imgState.FotoRuotaASinistra)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
if (picSettings.TestoMin == false)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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);
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
if (picSettings.TestoMin == true)
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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);
2021-02-25 11:14:44 +01:00
}
}
2025-07-24 14:33:48 +02:00
else
2021-02-25 11:14:44 +01:00
{
2025-07-28 11:25:46 +02:00
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);
2021-02-25 11:14:44 +01:00
}
}
2025-07-28 11:25:46 +02:00
if (string.Equals(picSettings.DirectorySorgente, picSettings.DirectoryDestinazione, StringComparison.OrdinalIgnoreCase))
2025-07-24 14:33:48 +02:00
{
2025-07-28 11:25:46 +02:00
imgState.NomeFileBig2 = imgState.NomeFileBig;
imgState.NomeFileBig = $"{imgState.NomeFileBig[..^4]}{picSettings.Codice}{imgState.NomeFileBig[^4..]}";
2025-07-24 14:33:48 +02:00
}
//grPhoto.Dispose();
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
crFont?.Dispose();
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
private void AggiungiLogo(Bitmap imgOutputBig, ImageState imgState, Image logo)
2021-02-25 11:14:44 +01:00
{
// imgOutputBig
2025-07-28 11:25:46 +02:00
if (!(picSettings.LogoAggiungi == true & File.Exists(picSettings.LogoNomeFile))) return;
// using var ImmagineLogo = Image.FromFile(_picSettings.LogoNomeFile);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
var logoColoreTrasparente = Color.White;
2025-07-24 14:33:48 +02:00
2025-07-28 11:25:46 +02:00
// * Load this Bitmap into a new Graphic Object
using var grWatermark = Graphics.FromImage(imgOutputBig);
using ImageAttributes imageAttributes = new ImageAttributes();
2025-07-24 14:33:48 +02:00
2025-07-28 11:25:46 +02:00
// * The first step replace the background color with one that is transparent (Alpha=0, R=0, G=0, B=0)
var colorMap = new ColorMap
{
2025-07-24 14:33:48 +02:00
// * background this will be the color we search for and replace with transparency
2025-07-28 11:25:46 +02:00
OldColor = logoColoreTrasparente,
NewColor = Color.FromArgb(0, 0, 0, 0)
};
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
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);
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");
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())
{
case "SINISTRA":
case "NESSUNA":
2025-07-24 14:33:48 +02:00
{
2025-07-28 11:25:46 +02:00
xPosOfWm = margineUsato;
break;
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
case "CENTRO":
{
xPosOfWm = System.Convert.ToInt32((imgOutputBig.Width - nuovaSize.Width) / (double)2);
break;
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
case "DESTRA":
{
xPosOfWm = ((imgOutputBig.Width - nuovaSize.Width) - margineUsato);
break;
2025-07-24 14:33:48 +02:00
}
2025-07-28 11:25:46 +02:00
}
switch (picSettings.LogoPosizioneV.ToUpper())
{
case "ALTO":
case "NESSUNA":
2025-07-24 14:33:48 +02:00
{
2025-07-28 11:25:46 +02:00
yPosOfWm = margineUsato;
break;
}
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
case "CENTRO":
{
yPosOfWm = System.Convert.ToInt32((imgOutputBig.Height - nuovaSize.Height) / (double)2);
break;
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
2025-07-28 11:25:46 +02:00
case "BASSO":
{
yPosOfWm = ((imgOutputBig.Height - nuovaSize.Height) - margineUsato);
break;
}
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
grWatermark.DrawImage(logo, new Rectangle(xPosOfWm, yPosOfWm, nuovaSize.Width, nuovaSize.Height), 0, 0, logo.Width, logo.Height, GraphicsUnit.Pixel, imageAttributes);
//grWatermark.Dispose();
2021-02-25 11:14:44 +01:00
}
2025-07-28 11:25:46 +02:00
private void SalvaFoto(Bitmap imgOutputBig, ImageState imgState, ImageFormat thisFormat)
2021-02-25 11:14:44 +01:00
{
2025-07-24 14:33:48 +02:00
using var image1Stream = new MemoryStream();
2025-07-28 11:25:46 +02:00
if (picSettings.FotoGrandeDimOrigina == false)
2021-02-25 11:14:44 +01:00
{
2025-07-24 14:33:48 +02:00
// attenzione non controlla se è png
// imgOutputBig.Save(Path.Combine(_DestDir.FullName, "Temp_" & NomeFileBig), thisFormat)
if (thisFormat.Equals(ImageFormat.Jpeg))
2021-02-25 11:14:44 +01:00
{
2025-07-24 14:33:48 +02:00
MakeImageCustomQuality(imgOutputBig, image1Stream);
2021-02-25 11:14:44 +01:00
}
2025-07-28 10:22:08 +02:00
//SalvaImmagineCustomQuality(imgOutputBig, Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig), _picSettings.jpegQuality);
2021-02-25 11:14:44 +01:00
else
{
2025-07-24 14:33:48 +02:00
imgOutputBig.Save(image1Stream, thisFormat);
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
//imgOutputBig.Save(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig), thisFormat);
2021-02-25 11:14:44 +01:00
image1Stream.Seek(0, SeekOrigin.Begin);
2025-07-24 14:33:48 +02:00
using var g2 = Image.FromStream(image1Stream);
2025-07-28 11:25:46 +02:00
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);
2025-07-24 14:33:48 +02:00
if (thisFormat.Equals(ImageFormat.Jpeg))
2025-07-28 11:25:46 +02:00
SalvaImmagineCustomQuality(imgOutputBig2, Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), picSettings.JpegQuality);
2025-07-24 14:33:48 +02:00
else
2025-07-28 11:25:46 +02:00
imgOutputBig2.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), thisFormat);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
//imgOutputBig2.Dispose();
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
//imgOutputBig.Dispose();
//g2.Dispose();
}
else
{
//
if (thisFormat.Equals(ImageFormat.Jpeg))
2025-07-28 11:25:46 +02:00
SalvaImmagineCustomQuality(imgOutputBig, Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), picSettings.JpegQuality);
2025-07-24 14:33:48 +02:00
else
2025-07-28 11:25:46 +02:00
imgOutputBig.Save(Path.Combine(imgState.DestDir.FullName, imgState.NomeFileBig), thisFormat);
2021-02-25 11:14:44 +01:00
2025-07-24 14:33:48 +02:00
//imgOutputBig.Dispose();
}
image1Stream.Seek(0, SeekOrigin.Begin);
2025-07-28 11:25:46 +02:00
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);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
//imgOutputSmall.Dispose();
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
//g1.Dispose();
2025-07-24 14:33:48 +02:00
//if (File.Exists(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig)))
// File.Delete(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig));
2021-02-25 11:14:44 +01:00
}
private void SalvaImmagineCustomQuality(Bitmap imageToSave, string nomeFileFinale, long quality)
{
2025-07-28 11:25:46 +02:00
var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
var myEncoder = System.Drawing.Imaging.Encoder.Quality;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
using var myEncoderParameters = new EncoderParameters(1);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
var myEncoderParameter = new EncoderParameter(myEncoder, picSettings.JpegQuality);
myEncoderParameters.Param[0] = myEncoderParameter;
imageToSave.Save(nomeFileFinale, jgpEncoder, myEncoderParameters);
2021-02-25 11:14:44 +01:00
//imageToSave.Dispose();
}
private void MakeImageCustomQuality(Bitmap imageToSave, Stream destinationStream)
{
2025-07-28 11:25:46 +02:00
var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
var myEncoder = System.Drawing.Imaging.Encoder.Quality;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
using var myEncoderParameters = new EncoderParameters(1);
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
var myEncoderParameter = new EncoderParameter(myEncoder, picSettings.JpegQuality);
myEncoderParameters.Param[0] = myEncoderParameter;
2021-02-25 11:14:44 +01:00
destinationStream.Seek(0, SeekOrigin.Begin);
2025-07-28 11:25:46 +02:00
imageToSave.Save(destinationStream, jgpEncoder, myEncoderParameters);
2021-02-25 11:14:44 +01:00
//imageToSave.Dispose();
}
2025-07-28 11:25:46 +02:00
2021-02-25 11:14:44 +01:00
private ImageCodecInfo GetEncoder(ImageFormat format)
{
2025-07-28 11:25:46 +02:00
var codecs = ImageCodecInfo.GetImageDecoders();
2021-02-25 11:14:44 +01:00
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 */;
}
2025-07-24 14:33:48 +02:00
2021-02-25 11:14:44 +01:00
/// <summary>
/// ''' Calculate the Size of the New image
/// ''' </summary>
/// ''' <param name="currentwidth">Larghezza</param>
/// ''' <param name="currentheight">Altezza</param>
2025-07-28 11:25:46 +02:00
/// ''' <param name="maxPixel"></param>
/// ''' <param name="tipoSize"></param>
2021-02-25 11:14:44 +01:00
/// ''' <returns></returns>
/// ''' <remarks></remarks>
2025-07-28 11:25:46 +02:00
private Size NewthumbSize(int currentwidth, int currentheight, int maxPixel, string tipoSize)
2021-02-25 11:14:44 +01:00
{
// e
// *** Larghezza, Altezza, Auto
double tempMultiplier;
2025-07-28 11:25:46 +02:00
if (tipoSize.ToUpper() == "Larghezza".ToUpper())
tempMultiplier = maxPixel / (double)currentwidth;
else if (tipoSize.ToUpper() == "Altezza".ToUpper())
tempMultiplier = maxPixel / (double)currentheight;
2021-02-25 11:14:44 +01:00
else if (currentheight > currentwidth)
2025-07-28 11:25:46 +02:00
tempMultiplier = maxPixel / (double)currentheight;
2021-02-25 11:14:44 +01:00
else
2025-07-28 11:25:46 +02:00
tempMultiplier = maxPixel / (double)currentwidth;
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
var newSize = new Size(System.Convert.ToInt32(currentwidth * tempMultiplier), System.Convert.ToInt32(currentheight * tempMultiplier));
2021-02-25 11:14:44 +01:00
2025-07-28 11:25:46 +02:00
return newSize;
2021-02-25 11:14:44 +01:00
}
2025-07-24 14:33:48 +02:00
2025-07-28 11:25:46 +02:00
}