// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Data; namespace Spludlow.Drawing { public class ThumbNails { List SupportedExtentions; private string RootFilePath; private string RootWebPath; private Size BoxSize; private int Dpi; public ThumbNails(string configKey) { string config = Spludlow.Config.Get(configKey); string[] words = Spludlow.Text.Split(config, ',', true); if (words.Length != 5) throw new ApplicationException("Thumbs; Config Format 'FilePath, WebPath, Width, Height, DPI' :\t" + config); this.Setup(words[0], words[1], Int32.Parse(words[2]), Int32.Parse(words[3]), Int32.Parse(words[4])); } public ThumbNails(string rootFilePath, string rootWebPath, int width, int height) { this.Setup(rootFilePath, rootWebPath, width, height, 72); } public ThumbNails(string rootFilePath, string rootWebPath, int width, int height, int dpi) { this.Setup(rootFilePath, rootWebPath, width, height, dpi); } private void Setup(string rootFilePath, string rootWebPath, int width, int height, int dpi) { this.SupportedExtentions = new List(new string[] { ".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff", ".gif", }); this.RootFilePath = this.TrimTrailing(rootFilePath, '\\'); this.RootWebPath = this.TrimTrailing(rootWebPath, '/'); this.BoxSize = new Size(width, height); this.Dpi = 72; //if (Directory.Exists(this.RootFilePath) == false) // throw new ApplicationException("ThumbNails; Root Directory not there:\t" + this.RootFilePath); } private string TrimTrailing(string text, char trimChar) { while (text[text.Length - 1] == trimChar) text = text.Substring(0, text.Length - 1); return text; } public void AddPaths(DataTable table, string sourceFilenameColumnName, string targetWebPathColumnName, string targetFilePathColumnName, bool write) { foreach (DataRow row in table.Rows) { string sourceFilename = (string)row[sourceFilenameColumnName]; string[] webAndFilePath = this.WebAndFilePath(sourceFilename, write); if (targetWebPathColumnName != null) row[targetWebPathColumnName] = webAndFilePath[0]; if (targetFilePathColumnName != null) row[targetFilePathColumnName] = webAndFilePath[1]; } } public string[] WebAndFilePath(string sourceFilename, bool write) { string extention = Path.GetExtension(sourceFilename).ToLower(); StringBuilder resultFile = new StringBuilder(); resultFile.Append(this.RootFilePath); StringBuilder resultWeb = new StringBuilder(); resultWeb.Append(this.RootWebPath); if (this.SupportedExtentions.Contains(extention) == false) { resultFile.Append('\\'); resultWeb.Append('/'); string blankName = this.BoxSize.Width + "X" + this.BoxSize.Height + "-" + this.Dpi + extention + ".jpg"; resultFile.Append(blankName); resultWeb.Append(blankName); string blankFilename = resultFile.ToString(); string blankWebPath = resultWeb.ToString(); if (write == true && File.Exists(blankFilename) == false) this.MakeBlank(blankFilename, Color.LawnGreen, extention); return new string[] { blankWebPath, null }; // blankFilename never return path to a special as dont want to ever delete } string[] directoryParts = Path.GetDirectoryName(sourceFilename).Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); // Don't need to do?? foreach (string rawPart in directoryParts) { resultFile.Append('\\'); resultWeb.Append('/'); string part = rawPart; if (part.Length == 2 && part[1] == ':') part = part.Substring(0, 1); resultFile.Append(part); resultWeb.Append(part); if (write == true) { string directory = resultFile.ToString(); if (Directory.Exists(directory) == false) Directory.CreateDirectory(directory); } } resultFile.Append("\\"); resultWeb.Append('/'); string name = Path.GetFileNameWithoutExtension(sourceFilename) + ".jpg"; resultFile.Append(name); resultWeb.Append(name); string thumbFilename = resultFile.ToString(); string webPath = resultWeb.ToString(); if (write == true && File.Exists(thumbFilename) == false) { try { Spludlow.Drawing.Bitmaps.Resize(sourceFilename, this.BoxSize, thumbFilename, ImageFormat.Jpeg, PixelFormat.Format24bppRgb, this.Dpi); } catch (Exception ee) { Spludlow.Log.Error("ThumbNails; Resize source:" + sourceFilename + ", thumb:" + thumbFilename, ee); } } return new string[] { webPath, thumbFilename }; } private void MakeBlank(string filename, Color colour, string extention) { using (Bitmap bitmap = new Bitmap(this.BoxSize.Width, this.BoxSize.Height, PixelFormat.Format24bppRgb)) { bitmap.SetResolution(this.Dpi, this.Dpi); using (Graphics graphics = Graphics.FromImage(bitmap)) graphics.Clear(colour); bitmap.Save(filename, ImageFormat.Jpeg); } } } }