From 15f0d4d5b8f9b8fbe42fc9699e4384f94ea0209c Mon Sep 17 00:00:00 2001 From: Maddo Date: Mon, 7 Nov 2016 20:58:16 +0100 Subject: [PATCH] Basic flow --- CatalogLib/CatalogLib.csproj | 1 + CatalogLib/ImageCreator2.cs | 166 ++++++++++++++++++++++ CatalogLib/PicSettings.cs | 221 +++++++++++++++++++++++++----- MaddoLibrary | 2 +- WPFCatalog/MainWindow.xaml | 15 +- WPFCatalog/MainWindowViewModel.cs | 143 +++++++++++++++++-- 6 files changed, 495 insertions(+), 53 deletions(-) create mode 100644 CatalogLib/ImageCreator2.cs diff --git a/CatalogLib/CatalogLib.csproj b/CatalogLib/CatalogLib.csproj index f3a34a3..113d6b9 100644 --- a/CatalogLib/CatalogLib.csproj +++ b/CatalogLib/CatalogLib.csproj @@ -51,6 +51,7 @@ + diff --git a/CatalogLib/ImageCreator2.cs b/CatalogLib/ImageCreator2.cs new file mode 100644 index 0000000..14a7bf1 --- /dev/null +++ b/CatalogLib/ImageCreator2.cs @@ -0,0 +1,166 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CatalogVbLib; + +namespace CatalogLib +{ + public class ImageCreator2 + { + private PicSettings _picSettings = PicSettings.Instance; + + private FileInfo _workFile; + + private Image _workingImage; + + private Rotazione _rotation; + + private ImageFormat _currentFormat; + + private Size _newImageSize; + + private Bitmap _outputImage; + + private DirectoryInfo _destDir; + + private string _nomeFileBig; + + public void Start(FileInfo workFile) + { + _workFile = workFile; + + _workingImage = Image.FromFile(workFile.FullName); + + _destDir = new DirectoryInfo(_picSettings.DirectoryDestinazione); + _nomeFileBig = _workFile.Name; + + CalculateImageSize(); + + ElaborazioneTesto(); + + ElaborazioneRotazione(); + + SetImageFormat(); + + + FinalElaboration(); + + //CreaMiniature + } + + private void FinalElaboration() + { + _outputImage = new Bitmap(_workingImage, _newImageSize.Width, _newImageSize.Height); + _outputImage.SetResolution(_workingImage.HorizontalResolution, _workingImage.VerticalResolution); + + + SavePic(_outputImage, Path.Combine(_destDir.FullName, _nomeFileBig)); + } + + private void SavePic(Bitmap imageToSave, string fileName) + { + var selectedEncoder = GetEncoder(_currentFormat); + var encoder = System.Drawing.Imaging.Encoder.Quality; + var encoderParameters = new EncoderParameters(1); + + + if (Equals(_currentFormat, ImageFormat.Jpeg)) + { + var encoderParameter = new EncoderParameter(encoder, _picSettings.CompressioneJpeg); + encoderParameters.Param[0] = encoderParameter; + } + + imageToSave.Save(fileName, selectedEncoder,encoderParameters); + imageToSave.Dispose(); + } + + private ImageCodecInfo GetEncoder(ImageFormat format) + { + var codecs = ImageCodecInfo.GetImageDecoders(); + + return codecs.FirstOrDefault(c => c.FormatID == format.Guid); + } + + private void ElaborazioneTesto() + { + if (_picSettings.EnableText) + { + // todo: elaborazione testo + } + } + + private void CalculateImageSize() + { + _newImageSize = new Size(_workingImage.Width, _workingImage.Height); + } + + private void ElaborazioneRotazione() + { + + _rotation = Rotazione.Normale; + + if (_picSettings.UsaRotazioneAutomatica) + { + // ci sono dati exif + if (_workingImage.PropertyIdList.Length > 0) + { + ExifReader DatiExif = new ExifReader((Bitmap)_workingImage); + + switch (DatiExif.Orientation) + { + case ExifReader.Orientations.BottomLeft: + + break; + case ExifReader.Orientations.BottomRight: + + break; + case ExifReader.Orientations.LeftTop: + + break; + case ExifReader.Orientations.LftBottom: + //FotoRuotaASinistra = true; + _rotation = Rotazione.Sinistra; + break; + case ExifReader.Orientations.RightBottom: + + break; + case ExifReader.Orientations.RightTop: + + break; + case ExifReader.Orientations.TopLeft: + + break; + case ExifReader.Orientations.TopRight: + + break; + } + } + } + + if (_rotation == Rotazione.Sinistra) + { + _workingImage.RotateFlip(RotateFlipType.Rotate270FlipNone); + } + else + if (_rotation == Rotazione.Destra) + { + _workingImage.RotateFlip(RotateFlipType.Rotate90FlipNone); + } + } + + private void SetImageFormat() + { + _currentFormat = _workingImage.RawFormat; + if (_picSettings.GeneraleForzaJPG) + { + _currentFormat = ImageFormat.Jpeg; + } + } + + } +} diff --git a/CatalogLib/PicSettings.cs b/CatalogLib/PicSettings.cs index 3e9f305..fd08514 100644 --- a/CatalogLib/PicSettings.cs +++ b/CatalogLib/PicSettings.cs @@ -15,7 +15,10 @@ namespace CatalogLib { private static PicSettings _instance = new PicSettings(); - public static PicSettings Instance { get { return _instance; } } + public static PicSettings Instance + { + get { return _instance; } + } private Dictionary _settingsDict = new Dictionary(); @@ -27,8 +30,13 @@ namespace CatalogLib public string SerializeSettings() { return JsonConvert.SerializeObject(_settingsDict); + } - + public void DeserializeSettings(string serializedData) + { + _settingsDict = JsonConvert.DeserializeObject>(serializedData); + + } public void Set(string key, object value) @@ -43,14 +51,14 @@ namespace CatalogLib } } - public void SetString(string key, string value ) + public void SetString(string key, string value) { Set(key, value); } public void SetInt(string key, int value) { - Set(key,value); + Set(key, value); } public void SetBool(string key, bool value) @@ -65,46 +73,84 @@ namespace CatalogLib return _settingsDict.ContainsKey(key); } - public int GetInt(string key) + public int GetInt(string key, int defaultValue = 0) { - return _settingsDict.ContainsKey(key) ? (int)_settingsDict[key] : 0; - } + if (!_settingsDict.ContainsKey(key)) + { + SetInt(key, defaultValue); + } - public string GetString(string key) - { - return _settingsDict.ContainsKey(key) ? (string)_settingsDict[key] : string.Empty; - } + if (_settingsDict[key] is int) return (int)_settingsDict[key]; + Debug.WriteLine($"Error while parsing {key}"); + //return defaultValue; + + int r; - public bool GetBool(string key) - { - return _settingsDict.ContainsKey(key) && (bool)_settingsDict[key]; + if (int.TryParse(_settingsDict[key].ToString(), out r)) + { + SetInt(key, r); + + } + else + { + SetInt(key, defaultValue); + } + return (int) _settingsDict[key]; + //return (int) _settingsDict[key]; + + //return _settingsDict.ContainsKey(key) ? (int)_settingsDict[key] : defaultValue; } - public object GetObject(string key) + public string GetString(string key, string defaultValue = "") { - return _settingsDict.ContainsKey(key) ? _settingsDict[key] : null; + if (!_settingsDict.ContainsKey(key)) + { + SetString(key, defaultValue); + } + return (string) _settingsDict[key]; + + //return _settingsDict.ContainsKey(key) ? (string)_settingsDict[key] : defaultValue; + } + + public bool GetBool(string key, bool defaultValue = false) + { + if (!_settingsDict.ContainsKey(key)) + { + SetBool(key, defaultValue); + return defaultValue; + } + return (bool) _settingsDict[key]; + + + + //return _settingsDict.ContainsKey(key) && (bool)_settingsDict[key]; + } + + + public object GetObject(string key, object defaultValue = null) + { + if (!_settingsDict.ContainsKey(key)) + { + Set(key, defaultValue); + return defaultValue; + } + return _settingsDict[key]; + + //return _settingsDict.ContainsKey(key) ? _settingsDict[key] : defaultValue; } public void SetDefaults() { - //Set("DirSorgente", string.Empty); - //Set("DirDestinazione", string.Empty); - //Set("DirAggiornaSottodirectory", false); - //Set("DirCreaSottoCartelle", false); - //Set(); + } public bool DirAggiornaSottoDirectory { - get { return this.GetBool("DirAggiornaSottoDirectory"); } - set - { - this.SetBool("DirAggiornaSottoDirectory", value); - - } + get { return this.GetBool("DirAggiornaSottoDirectory", true); } + set { this.SetBool("DirAggiornaSottoDirectory", value); } } public bool Grassetto @@ -187,14 +233,121 @@ namespace CatalogLib public string TestoFirmaStart { get; set; } public string TestoFirmaStartV { get; set; } public bool UsaForzaJpg { get; set; } - public string DirectorySorgente { get { return GetString("DirSorgente"); } set { SetString("DirSorgente", value); } } - public string DirectoryDestinazione { get { return GetString("DirDestinazione"); } set { SetString("DirDestinazione", value);} } + + public string DirectorySorgente + { + get { return GetString("DirSorgente"); } + set { SetString("DirSorgente", value); } + } + + public string DirectoryDestinazione + { + get { return GetString("DirDestinazione"); } + set { SetString("DirDestinazione", value); } + } + public float Margine { get; set; } public float MargVert { get; set; } public string Allineamento { get; set; } - public bool GeneraleForzaJPG { get { return GetBool("GeneraleForzaJPG"); } set { SetBool("GeneraleForzaJPG", value);} } - public bool GeneraleRotazioneAutomatica { get { return GetBool("GeneraleRotazioneAutomatica"); } set { SetBool("GeneraleRotazioneAutomatica", value); } } - public bool GeneraleSovrascriviFile { get { return GetBool("GeneraleSovrascriviFile"); } set { SetBool("GeneraleSovrascriviFile", value); } } - + + public bool GeneraleForzaJPG + { + get { return GetBool("GeneraleForzaJPG", true); } + set { SetBool("GeneraleForzaJPG", value); } + } + + public bool GeneraleRotazioneAutomatica + { + get { return GetBool("GeneraleRotazioneAutomatica"); } + set { SetBool("GeneraleRotazioneAutomatica", value); } + } + + public bool GeneraleSovrascriviFile + { + get { return GetBool("GeneraleSovrascriviFile"); } + set { SetBool("GeneraleSovrascriviFile", value); } + } + + public bool SubdirCreaSottoCartelle + { + get { return GetBool("SubdirCreaSottoCartelle"); } + set { SetBool("SubdirCreaSottoCartelle", value); } + } + + public int SubdirIntervalloFile + { + get { return GetInt("SubdirIntervalloFile", 99); } + set { SetInt("SubdirIntervalloFile", value); } + } + + public string SubdirSuffisso + { + get { return GetString("SubdirSuffisso"); } + set { SetString("SubdirSuffisso", value); } + } + + public int SubdirCifreContatore + { + get { return GetInt("SubdirCifreContatore", 2); } + set { SetInt("SubdirCifreContatore", value); } + } + + public bool SubdirNumerazioneProgressiva + { + get { return GetBool("SubdirNumerazioneProgressiva", true); } + set { SetBool("SubdirNumerazioneProgressiva", value); } + } + + public bool SubdirNumerazioneFiles + { + get { return GetBool("SubdirNumerazioneFiles", false); } + set { SetBool("SubdirNumerazioneFiles", value); } + } + + public int FotoAltezza + { + get { return GetInt("FotoAltezza", 2240); } + set { SetInt("FotoAltezza", value); } + } + + public int FotoLarghezza + { + get { return GetInt("FotoLarghezza", 2240); } + set { SetInt("FotoLarghezza", value); } + } + + public int CompressioneJpeg + { + get { return GetInt("CompressioneJpeg", 85); } + set { SetInt("CompressioneJpeg", value); } + } + + public bool FotoMantieniDimensioni + { + get { return GetBool("FotoMantieniDimensioni", true); } + set { SetBool("FotoMantieniDimensioni", value); } + } + + public string FotoSuffisso + { + get { return GetString("FotoSuffisso"); } + set { SetString("FotoSuffisso", value); } + } + + public bool EnableText + { + get { return GetBool("EnableText", false); } + set { SetBool("EnableText", value); } + } + public bool EnableThumbnails + { + get { return GetBool("EnableThumbnails", false); } + set { SetBool("EnableThumbnails", value); } + } + public bool EnableLogo + { + get { return GetBool("EnableLogo", false); } + set { SetBool("EnableLogo", value); } + } } -} +} \ No newline at end of file diff --git a/MaddoLibrary b/MaddoLibrary index e5db4a0..e44a212 160000 --- a/MaddoLibrary +++ b/MaddoLibrary @@ -1 +1 @@ -Subproject commit e5db4a03bbb6404b34ec679126503941b2fffa4c +Subproject commit e44a21295ec64e8b9caa22a9eb22af18fac6f7e6 diff --git a/WPFCatalog/MainWindow.xaml b/WPFCatalog/MainWindow.xaml index 1af2a3a..2bad153 100644 --- a/WPFCatalog/MainWindow.xaml +++ b/WPFCatalog/MainWindow.xaml @@ -101,7 +101,7 @@ - + @@ -170,7 +170,16 @@