Catalog/ImageCatalogCS/ImageCreator.cs

1057 lines
38 KiB
C#
Raw Permalink Normal View History

2016-07-04 16:45:45 +02:00
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using ImageCatalogCS;
using System.Windows.Forms;
//using System.Drawing;
//Imports System.Threading
public class ImageCreator
{
#region "dichiarazioni"
private bool FotoRuotaADestra = false;
private bool FotoRuotaASinistra = false;
private string TempMinText = "";
//Private crFont1 As Font
private string _NomeFileChild;
private DirectoryInfo _SourceDir;
private DirectoryInfo _DestDirStart;
private DirectoryInfo _DestDir;
private FileInfo _workFile;
private string testoFirma;
private string testoFirmaV;
private int alphaScelta;
private int DimensioneStandard;
private int DimensioneStandardMiniatura;
private DateTime dataFoto;
private DateTime dataPartenzaI;
private string testoOrario;
private string testoFirmaPiccola;
private Size thumbSizeSmall;
private Size thumbSizeBig;
private string nomeFileSmall;
private string nomeFileBig;
private string nomeFileBig2;
private float yPosFromBottom;
private float yPosFromBottom1;
private float yPosFromBottom2;
private float yPosFromBottom3;
private float yPosFromBottom4;
#endregion
public ImageCreator()
{
}
public ImageCreator(string nomeFileChild, DirectoryInfo sourceDir, DirectoryInfo destDir, DirectoryInfo destDirStart)
{
this._NomeFileChild = nomeFileChild;
this._SourceDir = sourceDir;
this._DestDir = destDir;
this._DestDirStart = destDirStart;
this._workFile = new FileInfo(nomeFileChild);
}
public ImageCreator(string nomeFileChild, DirectoryInfo sourceDir, DirectoryInfo destDir)
{
this._NomeFileChild = nomeFileChild;
this._DestDir = destDir;
}
public ImageCreator(FileInfo file, DirectoryInfo destination)
{
this._workFile = file;
this._DestDir = destination;
}
public void CreaImmagineThread(string Info)
{
try
{
preparaVariabili();
//Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Path.Combine(SourceDir.FullName, NomeFileChild))
System.Drawing.Image g = System.Drawing.Image.FromFile(WorkFile.FullName);
// Imposta testo extra
impostaTestoExtra(g);
// Ruota l'immagine in base ai dati EXIF
Rotation(ref g);
// 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);
Bitmap imgOutputBig = new Bitmap(g, thumbSizeBig.Width, thumbSizeBig.Height);
imgOutputBig.SetResolution(g.HorizontalResolution, g.VerticalResolution);
// Crea le miniature
creaMiniature(g, imgOutputBig, thisFormat);
aggiungiTesto(g, imgOutputBig);
aggiungiLogo(imgOutputBig);
salvaFoto(imgOutputBig, thumbSizeBig, nomeFileBig, nomeFileSmall, thumbSizeSmall, thisFormat);
g.Dispose();
GC.Collect();
//PicSettings.mainForm.stepProgressBar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Rotation(ref System.Drawing.Image g)
{
FotoRuotaADestra = false;
FotoRuotaASinistra = false;
if (PicSettings.UsaRotazioneAutomatica)
{
// ci sono dati exif
if (g.PropertyIdList.Length > 0)
{
ExifReader DatiExif = new ExifReader((Bitmap)g);
switch (DatiExif.Orientation)
{
case ExifReader.Orientations.BottomLeft:
break;
case ExifReader.Orientations.BottomRight:
break;
case ExifReader.Orientations.LeftTop:
break;
case ExifReader.Orientations.LftBottom:
FotoRuotaASinistra = true;
break;
case ExifReader.Orientations.RightBottom:
break;
case ExifReader.Orientations.RightTop:
break;
case ExifReader.Orientations.TopLeft:
break;
case ExifReader.Orientations.TopRight:
break;
}
}
}
if (FotoRuotaASinistra == true)
{
g.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
if (FotoRuotaADestra == true)
{
g.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
}
/// <summary>
/// Aggiunge Orario, tempo gara e altri
/// </summary>
/// <param name="g">Image</param>
/// <remarks></remarks>
private void impostaTestoExtra(Image g)
{
if (PicSettings.UsaOrarioTestoApplicare | PicSettings.UsaTempoGaraTestoApplicare | PicSettings.UsaOrarioMiniatura | PicSettings.TestoMin | PicSettings.AggTempoGaraMin | PicSettings.AggNumTempMin)
{
// ci sono dati exif
if (g.PropertyIdList.Length > 0)
{
ExifReader DatiExif = new ExifReader((Bitmap)g);
dataFoto = DatiExif.DateTimeOriginal;
testoFirma = PicSettings.TestoFirmaStart;
testoFirmaV = PicSettings.TestoFirmaStartV;
if (dataFoto.Year != 1)
{
testoFirmaPiccola = dataFoto.ToShortTimeString();
if (PicSettings.UsaOrarioTestoApplicare == true)
{
testoFirma += " " + dataFoto.ToShortDateString() + " " + dataFoto.ToLongTimeString();
testoFirmaV += " " + dataFoto.ToShortDateString() + " " + dataFoto.ToLongTimeString();
}
if (PicSettings.UsaTempoGaraTestoApplicare == true)
{
TimeSpan Orario = dataFoto.Subtract(dataPartenzaI);
//TimeSpan Orario = new TimeSpan( DateAndTime.DateDiff(DateInterval.Second, dataPartenzaI, dataFoto) * 10000000);
testoFirma += " " + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00");
testoFirmaV += " " + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00");
}
}
}
}
else
{
testoFirma = PicSettings.TestoFirmaStart;
testoFirmaV = PicSettings.TestoFirmaStartV;
}
}
/// <summary>
/// Prepara diverse variabili azzerandole, elaborandole e prendendole dalle impostazioni
/// </summary>
/// <remarks></remarks>
private void preparaVariabili()
{
alphaScelta = Convert.ToInt32((255 * (100 - PicSettings.Trasparenza) / 100));
testoFirma = "";
testoFirmaV = "";
dataPartenzaI = PicSettings.DataPartenza;
testoOrario = PicSettings.TestoOrario;
if (testoOrario.Length > 0)
testoOrario += " ";
testoFirmaPiccola = "";
thumbSizeSmall = new Size();
thumbSizeBig = new Size();
nomeFileSmall = "";
nomeFileBig2 = "";
nomeFileBig = "";
DimensioneStandard = PicSettings.dimStandard;
DimensioneStandardMiniatura = PicSettings.dimStandardMiniatura;
//nomeFileSmall = Suffisso & NomeFileChild
//nomeFileBig = NomeFileChild
nomeFileSmall = PicSettings.Suffisso + WorkFile.Name;
nomeFileBig = WorkFile.Name;
}
private void prepareThumbnailSize(Image g)
{
if (g.Width > g.Height)
{
thumbSizeSmall = NewthumbSize(g.Width, g.Height, PicSettings.LarghezzaSmall, "Larghezza");
Size SizeOrig = new Size(g.Width, g.Height);
thumbSizeBig = SizeOrig;
}
else
{
thumbSizeSmall = NewthumbSize(g.Width, g.Height, PicSettings.AltezzaSmall, "Altezza");
Size SizeOrig = new Size(g.Width, g.Height);
thumbSizeBig = SizeOrig;
}
}
private void creaMiniature(Image g, Bitmap imgOutputBig, ImageFormat thisFormat)
{
if (PicSettings.TestoMin)
{
testoFirmaPiccola = nomeFileBig;
}
else if (PicSettings.AggNumTempMin)
{
testoFirmaPiccola = nomeFileBig + " ";
}
//Dim yPosFromBottom4 As Single
Font crFont1 = null;
Font crFont2 = null;
SizeF crSize1 = new SizeF();
SizeF crSize2 = new SizeF();
if (PicSettings.CreaMiniature == true)
{
if (PicSettings.AggiungiScritteMiniature == false)
{
if (string.Equals(PicSettings.directorySorgente.ToUpper(),
PicSettings.directoryDestinazione.ToUpper()))
{
nomeFileSmall = nomeFileSmall.Substring(0, nomeFileSmall.Length - 4) + PicSettings.Codice + nomeFileSmall.Substring(nomeFileSmall.Length - 4);
}
if (PicSettings.UsaOrarioMiniatura | PicSettings.TestoMin | PicSettings.AggTempoGaraMin | PicSettings.AggNumTempMin)
{
if (testoFirmaPiccola.Length > 0)
{
Bitmap imgOutputSmall = default(Bitmap);
imgOutputSmall = (Bitmap)imgOutputBig.Clone();
Graphics grPhoto1 = default(Graphics);
grPhoto1 = Graphics.FromImage(imgOutputSmall);
grPhoto1.SmoothingMode = SmoothingMode.AntiAlias;
int LarghezzaStandard1 = 0;
//quick fix
DimensioneStandardMiniatura = 50;
if (PicSettings.Grassetto == true)
{
crFont1 = new Font(PicSettings.IlFont, DimensioneStandardMiniatura, FontStyle.Bold);
crFont2 = new Font(PicSettings.IlFont, DimensioneStandard, FontStyle.Bold);
}
else
{
crFont1 = new Font(PicSettings.IlFont, DimensioneStandardMiniatura);
crFont2 = new Font(PicSettings.IlFont, DimensioneStandard);
}
crSize1 = grPhoto1.MeasureString(testoFirmaPiccola, crFont1);
crSize2 = grPhoto1.MeasureString(testoFirma, crFont1);
LarghezzaStandard1 = Convert.ToInt32(crSize1.Width);
if (crSize1.Width > Convert.ToSingle(g.Width))
{
int Conta = DimensioneStandardMiniatura;
do
{
if (Conta > 20)
{
Conta -= 5;
}
else
{
Conta -= 1;
}
if (PicSettings.Grassetto == true)
{
crFont1 = new Font(PicSettings.IlFont, Conta, FontStyle.Bold);
}
else
{
crFont1 = new Font(PicSettings.IlFont, Conta);
}
crSize1 = grPhoto1.MeasureString(testoFirmaPiccola, crFont1);
if (crSize1.Width < Convert.ToSingle(g.Width))
{
LarghezzaStandard1 = Convert.ToInt32(crSize1.Width);
break; // TODO: might not be correct. Was : Exit Do
}
if (Conta <= 5)
break; // TODO: might not be correct. Was : Exit Do
} while (true);
DimensioneStandardMiniatura = Conta;
}
switch (PicSettings.Posizione.ToUpper())
{
case "ALTO":
yPosFromBottom1 = (PicSettings.Margine);
yPosFromBottom4 = (PicSettings.margVert);
break;
case "BASSO":
yPosFromBottom1 = Convert.ToSingle((g.Height - crSize1.Height - (g.Height * PicSettings.Margine / 100)));
yPosFromBottom4 = Convert.ToSingle((g.Height - crSize1.Height - (g.Height * PicSettings.margVert / 100)));
break;
}
float xCenterOfImg1 = 0;
StringFormat StrFormat1 = new StringFormat();
switch (PicSettings.Allineamento.ToUpper())
{
case "SINISTRA":
xCenterOfImg1 = Convert.ToSingle((PicSettings.Margine + (LarghezzaStandard1 / 2)));
if ((LarghezzaStandard1 / 2) > (g.Width / 2) - PicSettings.Margine)
{
xCenterOfImg1 = Convert.ToSingle((g.Width / 2));
}
break;
case "CENTRO":
xCenterOfImg1 = Convert.ToSingle((g.Width / 2));
break;
case "DESTRA":
xCenterOfImg1 = Convert.ToSingle((g.Width - PicSettings.Margine - (LarghezzaStandard1 / 2)));
if ((LarghezzaStandard1 / 2) > (g.Width / 2) - PicSettings.Margine)
{
xCenterOfImg1 = Convert.ToSingle((g.Width / 2));
}
break;
}
StrFormat1.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush21 = new SolidBrush(Color.FromArgb(alphaScelta, 0, 0, 0));
SolidBrush semiTransBrush1 = new SolidBrush(Color.FromArgb(alphaScelta, PicSettings.fontColoreRGB));
//quick fix
DimensioneStandardMiniatura = PicSettings.DimMin;
if (PicSettings.Grassetto == true)
{
crFont1 = new Font(PicSettings.IlFont, DimensioneStandardMiniatura, FontStyle.Bold);
}
else
{
crFont1 = new Font(PicSettings.IlFont, DimensioneStandardMiniatura);
}
//asdgadfhdfhjgfsjgfjygfdhsdafa
if (PicSettings.TestoMin)
{
grPhoto1.DrawString(nomeFileBig, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1);
grPhoto1.DrawString(nomeFileBig, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1);
}
else if (PicSettings.AggTempoGaraMin & PicSettings.UsaTempoGaraTestoApplicare)
{
//TimeSpan Orario = new TimeSpan(DateAndTime.DateDiff(DateInterval.Second, dataPartenzaI, dataFoto) * 10000000);
TimeSpan Orario = dataFoto.Subtract(dataPartenzaI);
string tempstr = "";
tempstr += Environment.NewLine + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00");
grPhoto1.DrawString(tempstr, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1);
grPhoto1.DrawString(tempstr, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1);
}
else if (PicSettings.AggNumTempMin)
{
TimeSpan Orario = dataFoto.Subtract(dataPartenzaI);
//TimeSpan Orario = new TimeSpan(DateAndTime.DateDiff(DateInterval.Second, dataPartenzaI, dataFoto) * 10000000);
string tempstr = "";
tempstr += nomeFileBig;
tempstr += Environment.NewLine + testoOrario + Orario.Hours.ToString("00") + ":" + Orario.Minutes.ToString("00") + ":" + Orario.Seconds.ToString("00");
grPhoto1.DrawString(tempstr, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1);
grPhoto1.DrawString(tempstr, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1);
}
else
{
grPhoto1.DrawString(testoFirmaPiccola, crFont1, semiTransBrush21, new PointF(xCenterOfImg1 + 1, yPosFromBottom1 + 1), StrFormat1);
grPhoto1.DrawString(testoFirmaPiccola, crFont1, semiTransBrush1, new PointF(xCenterOfImg1, yPosFromBottom1), StrFormat1);
}
// Salva la miniatura
imgOutputSmall.Save(Path.Combine(DestDir.FullName, "Temp_" + nomeFileSmall), thisFormat);
System.Drawing.Image g2 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, "Temp_" + nomeFileSmall));
Bitmap imgOutputSmall2 = new Bitmap(g2, thumbSizeSmall.Width, thumbSizeSmall.Height);
imgOutputSmall2.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat);
imgOutputSmall2.Dispose();
imgOutputSmall.Dispose();
g2.Dispose();
File.Delete((Path.Combine(DestDir.FullName, "Temp_" + nomeFileSmall)));
}
else
{
Bitmap imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height);
imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat);
imgOutputSmall.Dispose();
}
}
else
{
Bitmap imgOutputSmall = new Bitmap(g, thumbSizeSmall.Width, thumbSizeSmall.Height);
imgOutputSmall.Save(Path.Combine(DestDir.FullName, nomeFileSmall), thisFormat);
imgOutputSmall.Dispose();
}
}
}
}
private void aggiungiTesto(Image g, Bitmap imgOutputBig)
{
Graphics grPhoto = default(Graphics);
grPhoto = Graphics.FromImage(imgOutputBig);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
Font crFont = null;
SizeF crSize = new SizeF();
int LarghezzaStandard = 0;
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 = Convert.ToInt32(crSize.Width);
if (crSize.Width > 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 < Convert.ToSingle(g.Width))
{
LarghezzaStandard = Convert.ToInt32(crSize.Width);
break; // TODO: might not be correct. Was : Exit Do
}
if (Conta <= 5)
break; // TODO: might not be correct. Was : Exit Do
} while (true);
DimensioneStandard = Conta;
}
switch (PicSettings.Posizione.ToUpper())
{
case "ALTO":
yPosFromBottom = (PicSettings.Margine);
yPosFromBottom3 = (PicSettings.margVert);
break;
case "BASSO":
yPosFromBottom = Convert.ToSingle((g.Height - crSize.Height - (g.Height * PicSettings.Margine / 100)));
yPosFromBottom3 = Convert.ToSingle((g.Height - crSize.Height - (g.Height * PicSettings.margVert / 100)));
break;
}
float xCenterOfImg = 0;
float xCenterOfImg3 = 0;
StringFormat StrFormat = new StringFormat();
switch (PicSettings.Allineamento.ToUpper())
{
case "SINISTRA":
xCenterOfImg = Convert.ToSingle((PicSettings.Margine + (LarghezzaStandard / 2)));
xCenterOfImg3 = Convert.ToSingle((PicSettings.margVert + (LarghezzaStandard / 2)));
if ((LarghezzaStandard / 2) > (g.Width / 2) - PicSettings.Margine)
{
xCenterOfImg = Convert.ToSingle((g.Width / 2));
}
if ((LarghezzaStandard / 2) > (g.Width / 2) - PicSettings.margVert)
{
xCenterOfImg3 = Convert.ToSingle((g.Width / 2));
}
break;
case "CENTRO":
xCenterOfImg = Convert.ToSingle((g.Width / 2));
break;
case "DESTRA":
xCenterOfImg = Convert.ToSingle((g.Width - PicSettings.Margine - (LarghezzaStandard / 2)));
xCenterOfImg3 = Convert.ToSingle((g.Width - PicSettings.margVert - (LarghezzaStandard / 2)));
if ((LarghezzaStandard / 2) > (g.Width / 2) - PicSettings.Margine)
{
xCenterOfImg = Convert.ToSingle((g.Width / 2));
}
if ((LarghezzaStandard / 2) > (g.Width / 2) - PicSettings.margVert)
{
xCenterOfImg3 = Convert.ToSingle((g.Width / 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 = 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 (PicSettings.directorySorgente.ToUpper() == PicSettings.directoryDestinazione.ToUpper())
{
nomeFileBig2 = nomeFileBig;
nomeFileBig = nomeFileBig.Substring(0, nomeFileBig.Length - 4) + PicSettings.Codice + nomeFileBig.Substring(nomeFileBig.Length - 4);
}
grPhoto.Dispose();
}
private void aggiungiLogo(Bitmap imgOutputBig)
{
//imgOutputBig
if (PicSettings.LogoAggiungi == true & File.Exists(PicSettings.LogoNomeFile))
{
Image ImmagineLogo = Image.FromFile(PicSettings.LogoNomeFile);
Color LogoColoreTrasparente = Color.White;
//Dim bmWatermark As Bitmap
//* Create a Bitmap based on the previously modified photograph Bitmap
//bmWatermark = New Bitmap(imgOutputBig)
//bmWatermark.SetResolution(imgOutputBig.HorizontalResolution, imgOutputBig.VerticalResolution)
//* Load this Bitmap into a new Graphic Object
Graphics grWatermark = Graphics.FromImage(imgOutputBig);
//* To achieve a translucent watermark we will apply (2) color manipulations
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 = { 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 float[] {
1f,
0f,
0f,
0f,
0f
},
new float[] {
0f,
1f,
0f,
0f,
0f
},
new float[] {
0f,
0f,
1f,
0f,
0f
},
new float[] {
0f,
0f,
0f,
Convert.ToSingle(PicSettings.LogoTrasparenza) / 100,
0f
},
new float[] {
0f,
0f,
0f,
0f,
1f
}
};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
int FotoLogoH = PicSettings.LogoAltezza;
int FotoLogoW = PicSettings.LogoLarghezza;
double FattoreAlt = ImmagineLogo.Height / FotoLogoH;
double FattoreLarg = ImmagineLogo.Width / FotoLogoW;
Size NuovaSize = default(Size);
if (FattoreLarg > FattoreAlt)
{
NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.Height, FotoLogoW, "Larghezza");
}
else
{
NuovaSize = NewthumbSize(ImmagineLogo.Width, ImmagineLogo.Height, FotoLogoH, "Altezza");
}
int MargineUsato = 0;
int MargineL = 0;
bool InPercentualeL = false;
if (PicSettings.LogoMargine.EndsWith("%") == true)
{
InPercentualeL = true;
}
else
{
InPercentualeL = false;
}
MargineL = Convert.ToInt32(PicSettings.LogoMargine);
if (InPercentualeL == true)
{
MargineUsato = Convert.ToInt32(imgOutputBig.Height * MargineL / 100);
}
else
{
MargineUsato = MargineL;
}
int xPosOfWm = 0;
int yPosOfWm = 0;
switch (PicSettings.LogoPosizioneH.ToUpper())
{
case "SINISTRA":
case "NESSUNA":
xPosOfWm = MargineUsato;
break;
case "CENTRO":
xPosOfWm = Convert.ToInt32((imgOutputBig.Width - NuovaSize.Width) / 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 = Convert.ToInt32((imgOutputBig.Height - NuovaSize.Height) / 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)
{
if (PicSettings.FotoGrandeDimOrigina == false)
{
//attenzione non controlla se è png
//imgOutputBig.Save(Path.Combine(_DestDir.FullName, "Temp_" & NomeFileBig), thisFormat)
if (thisFormat.Equals(ImageFormat.Jpeg))
{
salvaImmagineCustomQuality(imgOutputBig, Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig), PicSettings.jpegQuality);
}
else
{
imgOutputBig.Save(Path.Combine(_DestDir.FullName, "Temp_" + NomeFileBig), thisFormat);
}
System.Drawing.Image g2 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig));
if (g2.Width > g2.Height)
{
thumbSizeBig = NewthumbSize(g2.Width, g2.Height, PicSettings.LarghezzaBig, "Larghezza");
}
else
{
thumbSizeBig = NewthumbSize(g2.Width, g2.Height, PicSettings.AltezzaBig, "Altezza");
}
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();
}
if (PicSettings.CreaMiniature)
{
if (PicSettings.AggiungiScritteMiniature == true)
{
System.Drawing.Image g1 = null;
if (PicSettings.FotoGrandeDimOrigina == false)
{
g1 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig));
}
else
{
g1 = System.Drawing.Image.FromFile(Path.Combine(DestDir.FullName, NomeFileBig));
}
Bitmap imgOutputSmall = new Bitmap(g1, thumbSizeSmall.Width, thumbSizeSmall.Height);
if (PicSettings.directorySorgente.ToUpper() == PicSettings.directoryDestinazione.ToUpper())
{
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)) == true)
{
File.Delete(Path.Combine(DestDir.FullName, "Temp_" + NomeFileBig));
}
}
private void salvaImmagineCustomQuality(Bitmap imageToSave, string nomeFileFinale, long quality)
{
ImageCodecInfo JgpEncoder = GetEncoder(ImageFormat.Jpeg);
Encoder MyEncoder = Encoder.Quality;
EncoderParameters MyEncoderParameters = new EncoderParameters(1);
EncoderParameter MyEncoderParameter = new EncoderParameter(MyEncoder, PicSettings.jpegQuality);
MyEncoderParameters.Param[0] = MyEncoderParameter;
imageToSave.Save(nomeFileFinale, JgpEncoder, MyEncoderParameters);
imageToSave.Dispose();
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo codec = null;
foreach (ImageCodecInfo codec_loopVariable in codecs)
{
codec = codec_loopVariable;
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
/// <summary>
/// Calculate the Size of the New image
/// </summary>
/// <param name="currentwidth">Larghezza</param>
/// <param name="currentheight">Altezza</param>
/// <param name="MaxPixel"></param>
/// <param name="TipoSize"></param>
/// <returns></returns>
/// <remarks></remarks>
private Size NewthumbSize(int currentwidth, int currentheight, int MaxPixel, string TipoSize)
{
// e
//*** Larghezza, Altezza, Auto
double tempMultiplier = 0;
if (TipoSize.ToUpper() == "Larghezza".ToUpper())
{
tempMultiplier = MaxPixel / currentwidth;
}
else if (TipoSize.ToUpper() == "Altezza".ToUpper())
{
tempMultiplier = MaxPixel / currentheight;
}
else
{
// portrait
if (currentheight > currentwidth)
{
tempMultiplier = MaxPixel / currentheight;
}
else
{
tempMultiplier = MaxPixel / currentwidth;
}
}
Size NewSize = new Size(Convert.ToInt32(currentwidth * tempMultiplier), Convert.ToInt32(currentheight * tempMultiplier));
return NewSize;
}
public FileInfo WorkFile
{
get { return _workFile; }
set { _workFile = value; }
}
public DirectoryInfo DestDir
{
get { return _DestDir; }
set { _DestDir = value; }
}
public DirectoryInfo SourceDir
{
get { return _SourceDir; }
set { _SourceDir = value; }
}
public DirectoryInfo DestDirStart
{
get { return _DestDirStart; }
set { _DestDirStart = value; }
}
public string NomeFileChild
{
get { return _NomeFileChild; }
set { _NomeFileChild = value; }
}
}