diff --git a/PictureHandling/Form1.Designer.cs b/PictureHandling/Form1.Designer.cs index bb7beb7..9b4ae4a 100644 --- a/PictureHandling/Form1.Designer.cs +++ b/PictureHandling/Form1.Designer.cs @@ -46,9 +46,6 @@ namespace PictureHandling /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.btnFileDialog = new System.Windows.Forms.Button(); this.lblPath = new System.Windows.Forms.Label(); @@ -61,6 +58,10 @@ namespace PictureHandling this.btnReOrganize = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.txtRootDir = new System.Windows.Forms.TextBox(); + this.label4 = new System.Windows.Forms.Label(); + this.lblGroundPath = new System.Windows.Forms.Label(); + this.label6 = new System.Windows.Forms.Label(); + this.lblFinalPath = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // @@ -75,39 +76,43 @@ namespace PictureHandling // // lblPath // - this.lblPath.Location = new System.Drawing.Point(160, 16); + this.lblPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.lblPath.Location = new System.Drawing.Point(167, 16); this.lblPath.Name = "lblPath"; - this.lblPath.Size = new System.Drawing.Size(528, 23); + this.lblPath.Size = new System.Drawing.Size(521, 17); this.lblPath.TabIndex = 1; // // label2 // this.label2.Location = new System.Drawing.Point(104, 16); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(56, 23); + this.label2.Size = new System.Drawing.Size(56, 17); this.label2.TabIndex = 2; this.label2.Text = "Path"; // // lblFileName // - this.lblFileName.Location = new System.Drawing.Point(160, 48); + this.lblFileName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.lblFileName.Location = new System.Drawing.Point(167, 33); this.lblFileName.Name = "lblFileName"; - this.lblFileName.Size = new System.Drawing.Size(248, 23); + this.lblFileName.Size = new System.Drawing.Size(210, 15); this.lblFileName.TabIndex = 3; // // label1 // - this.label1.Location = new System.Drawing.Point(104, 48); + this.label1.Location = new System.Drawing.Point(104, 33); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(56, 23); + this.label1.Size = new System.Drawing.Size(56, 15); this.label1.TabIndex = 4; this.label1.Text = "Filnamn"; // // pictureBox1 // - this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); + this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.pictureBox1.Location = new System.Drawing.Point(176, 88); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(512, 480); @@ -117,6 +122,7 @@ namespace PictureHandling // // lstPicFiles // + this.lstPicFiles.FormattingEnabled = true; this.lstPicFiles.Location = new System.Drawing.Point(16, 88); this.lstPicFiles.Name = "lstPicFiles"; this.lstPicFiles.Size = new System.Drawing.Size(128, 394); @@ -125,9 +131,10 @@ namespace PictureHandling // // lblCreated // - this.lblCreated.Location = new System.Drawing.Point(424, 48); + this.lblCreated.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.lblCreated.Location = new System.Drawing.Point(355, 30); this.lblCreated.Name = "lblCreated"; - this.lblCreated.Size = new System.Drawing.Size(264, 23); + this.lblCreated.Size = new System.Drawing.Size(333, 18); this.lblCreated.TabIndex = 7; this.lblCreated.Text = "Skapad: "; // @@ -158,10 +165,49 @@ namespace PictureHandling this.txtRootDir.Text = "\\\\TFOASUS\\OurPictures"; this.txtRootDir.TextChanged += new System.EventHandler(this.txtRootDir_TextChanged); // + // label4 + // + this.label4.Location = new System.Drawing.Point(104, 48); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(64, 15); + this.label4.TabIndex = 12; + this.label4.Text = "GrundPath"; + // + // lblGroundPath + // + this.lblGroundPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.lblGroundPath.Location = new System.Drawing.Point(166, 48); + this.lblGroundPath.Name = "lblGroundPath"; + this.lblGroundPath.Size = new System.Drawing.Size(174, 15); + this.lblGroundPath.TabIndex = 11; + // + // label6 + // + this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.label6.Location = new System.Drawing.Point(355, 48); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(51, 15); + this.label6.TabIndex = 14; + this.label6.Text = "SlutPath"; + // + // lblFinalPath + // + this.lblFinalPath.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.lblFinalPath.Location = new System.Drawing.Point(412, 48); + this.lblFinalPath.Name = "lblFinalPath"; + this.lblFinalPath.Size = new System.Drawing.Size(276, 15); + this.lblFinalPath.TabIndex = 13; + // // Form1 // - this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(704, 590); + this.Controls.Add(this.label6); + this.Controls.Add(this.lblFinalPath); + this.Controls.Add(this.label4); + this.Controls.Add(this.lblGroundPath); this.Controls.Add(this.txtRootDir); this.Controls.Add(this.label3); this.Controls.Add(this.btnReOrganize); @@ -173,14 +219,22 @@ namespace PictureHandling this.Controls.Add(this.label2); this.Controls.Add(this.lblPath); this.Controls.Add(this.btnFileDialog); - //this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form1"; this.Text = "Form1"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + } - + #endregion + + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Label lblGroundPath; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.Label lblFinalPath; } } diff --git a/PictureHandling/Form1.cs b/PictureHandling/Form1.cs index b60e42f..a816753 100644 --- a/PictureHandling/Form1.cs +++ b/PictureHandling/Form1.cs @@ -1,10 +1,12 @@ using Microsoft.Win32; using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; +using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Security.AccessControl; @@ -77,94 +79,49 @@ namespace PictureHandling private void btnReOrganize_Click(object sender, EventArgs e) { - pictureList.Clear(); - bool error_upd = false; - //if (txtRootDir.Text.Trim().Substring(txtRootDir.Text.Trim().Length - 1) != @"\") - // txtRootDir.Text = txtRootDir.Text.Trim() + @"\"; - - txtRootDir.Text = txtRootDir.Text.Trim().EndsWith(@"\") ? txtRootDir.Text.Trim() : txtRootDir.Text.Trim() + @"\"; + ReorganizeImages(); + } + private void ReorganizeImages() + { DirectoryInfo di = new DirectoryInfo(txtRootDir.Text); if (!di.Exists) di.Create(); // om den nya rotkatalogen saknas skapa den - Deleting = true; - // listboxen påverkas inte av denna - - pictureBox1.Image = null; - pictureBox1.Refresh(); - for (int i = lstPicFiles.Items.Count - 1; i > -1; i--) + foreach (var localPic in pictureList) { - var bufPicture = new LocalPicture(); - - //string lFileName = lstPicFiles.Items[i].ToString().Trim(); - bufPicture.PictureFileName= lstPicFiles.Items[i].ToString().Trim(); - - //string fName = lblPath.Text.Trim() + lFileName; - bufPicture.InitialPath = lblPath.Text.Trim(); - - lblFileName.Text = bufPicture.PictureFullPath; - - //Image actIm = Image.FromFile(fName, true); - bufPicture.AktImage = Image.FromFile(bufPicture.PictureFullPath); - - //int w = actIm.Width; - //int h = actIm.Height; - bufPicture.AktImWidth = bufPicture.AktImage.Width; - bufPicture.AktImHeight = bufPicture.AktImage.Height; - - - int pw = pictureBox1.Width; - int ph = pictureBox1.Height; - pictureBox1.SizeMode = PictureBoxSizeMode.Normal; - Bitmap to_bm = new Bitmap((Image)bufPicture.AktImage.Clone(), bufPicture.AktImWidth * ph / bufPicture.AktImHeight, ph); - pictureBox1.Image = to_bm; - string td = TakenDate(bufPicture.AktImage); - - //actIm.Dispose(); - if (td.Trim() != "") + if (localPic.CreatedDateChanged) { - lblCreated.Text = "Skapad: " + td; - td = td.Substring(0, 10); - DateTime dttd = DateTime.Parse(td.Replace(":", "-")); - string subDir1 = dttd.Year.ToString("0000") + "-" + dttd.Month.ToString("00") + @"\"; - string subDir2 = dttd.Year.ToString("0000") + "_" + dttd.Month.ToString("00") + "_" + dttd.Day.ToString("00") + @"\"; - DirectoryInfo dis = new DirectoryInfo(di.FullName + subDir1); - if (!dis.Exists) dis.Create(); - DirectoryInfo diss = new DirectoryInfo(dis.FullName + subDir2); - if (!diss.Exists) diss.Create(); - - FileInfo fi = new FileInfo(fName); - //Tommy - fi.CopyTo(diss.FullName + lFileName); - error_upd = false; - do + PropertyItem prp; + try { - error_upd = false; - try + prp = localPic.AktImage.GetPropertyItem(306); + } + catch (Exception ex) + { + if (ex.Message == "Det går inte att hitta egenskapen.") { - lstPicFiles.BeginUpdate(); + prp = localPic.AktImage.PropertyItems[0]; + prp.Id = 306; + prp.Len = localPic.CreatedDate.Length; } - catch + else { - error_upd = true; + throw; } } - while (error_upd); - lstPicFiles.Items.RemoveAt(i); - lstPicFiles.EndUpdate(); - lstPicFiles.Refresh(); - lblCreated.Refresh(); - lblFileName.Refresh(); - pictureBox1.Refresh(); - this.Refresh(); + prp.Value = System.Text.ASCIIEncoding.ASCII.GetBytes(localPic.CreatedDate); + localPic.AktImage.SetPropertyItem(prp); } + DirectoryInfo dis = new DirectoryInfo(localPic.AarManDir); + if (!dis.Exists) dis.Create(); + DirectoryInfo diss = new DirectoryInfo(localPic.AarManDayDir); + if (!diss.Exists) diss.Create(); + + localPic.AktImage.Save(localPic.ImageNewFullpath); } - Deleting = false; - lblCreated.Text = ""; - lblFileName.Text = ""; - lblPath.Text = ""; + btnReOrganize.Enabled = false; } @@ -172,19 +129,20 @@ namespace PictureHandling { if (!Deleting) { - string fName = ((ListBox)sender).SelectedItem.ToString(); + string fName = ((ListBox)sender).SelectedValue.ToString(); + var lp = ((ListBox)sender).SelectedItem; lblFileName.Text = fName; - Image actPic = Image.FromFile(lblPath.Text + fName, true); - lblCreated.Text = "Skapad: " + TakenDate(actPic); - int w = actPic.Width; - int h = actPic.Height; + + lblCreated.Text = "Skapad: " + ((LocalPicture)lp).CreatedDate; + lblGroundPath.Text = ((LocalPicture)lp).AarManDir; + lblFinalPath.Text = ((LocalPicture)lp).AarManDayDir; + int w = ((LocalPicture)lp).AktImWidth; + int h = ((LocalPicture)lp).AktImHeight; int pw = pictureBox1.Width; int ph = pictureBox1.Height; pictureBox1.SizeMode = PictureBoxSizeMode.Normal; - Bitmap to_bm = new Bitmap((Image)actPic.Clone(), w * ph / h, ph); + Bitmap to_bm = new Bitmap((Image)((LocalPicture)lp).AktImage.Clone(), w * ph / h, ph); pictureBox1.Image = to_bm; - actPic = null; - } } @@ -237,17 +195,94 @@ namespace PictureHandling private void listLocalFiles() { DirectoryInfo di = new DirectoryInfo(lblPath.Text); - List files = new List(); - files.AddRange( di.GetFiles("*.jpg")); + var files = new List(); + files.AddRange(di.GetFiles("*.jpg")); files.AddRange(di.GetFiles("*.jpeg")); // - lstPicFiles.Items.Clear(); - foreach (var item in files) - { - lstPicFiles.Items.Add(item.Name); - } + Deleting = true; + lstPicFiles.DataSource = null; + pictureList = listBoxDataSource(files); + lstPicFiles.DataSource = pictureList; + lstPicFiles.DisplayMember = "PictureFileName"; + lstPicFiles.ValueMember = "PictureFullPath"; + Deleting = false; + btnReOrganize.Enabled = true; } + public List listBoxDataSource(List fileInfos) + { + + var outPut = new List(); + + txtRootDir.Text = txtRootDir.Text.Trim().EndsWith(@"\") ? txtRootDir.Text.Trim() : txtRootDir.Text.Trim() + @"\"; + + foreach (var fi in fileInfos) + { + var bufPicture = new LocalPicture(); + + bufPicture.RootPosition = txtRootDir.Text; + + bufPicture.PictureFileName = fi.Name; + + bufPicture.InitialPath = fi.DirectoryName; + + bufPicture.AktImage = Image.FromFile(bufPicture.PictureFullPath); + + bufPicture.AktImWidth = bufPicture.AktImage.Width; + bufPicture.AktImHeight = bufPicture.AktImage.Height; + + bufPicture.CreatedDate = TakenDate(bufPicture.AktImage); + + outPut.Add(bufPicture); + } + + return FillupDateGaps(outPut); + } + + private List FillupDateGaps(List inPut) + { + Debug.WriteLine($"FillupDateGaps input has {inPut.Count} files."); + var picReg = new SortedList(); + foreach (var pict in inPut) + { + picReg.Add(pict.PictureFileName, pict); + } + var tmpPicture = new LocalPicture(); + tmpPicture.CreatedDate = ""; + Stack stack = new Stack(); + var outPut = new List(); + + foreach (KeyValuePair lPic in picReg) + { + if (lPic.Value.CreatedDate == "") + { + if (tmpPicture.CreatedDate != "") + { + lPic.Value.CreatedDate = tmpPicture.CreatedDate; + lPic.Value.CreatedDateChanged = true; + outPut.Add(lPic.Value); + } + else + { + stack.Push(lPic.Key); + } + } + else + { + tmpPicture = lPic.Value; + while (stack.Count > 0) + { + var picRegKey = stack.Pop(); + picReg[picRegKey].CreatedDate = tmpPicture.CreatedDate; + picReg[picRegKey].CreatedDateChanged = true; + outPut.Add(picReg[picRegKey]); + } + outPut.Add(lPic.Value); + } + } + Debug.WriteLine($"FillupDateGaps output has {outPut.Count} files."); + return outPut; + } } } diff --git a/PictureHandling/Form1.resx b/PictureHandling/Form1.resx new file mode 100644 index 0000000..9bad2f5 --- /dev/null +++ b/PictureHandling/Form1.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/PictureHandling/LocalPicture.cs b/PictureHandling/LocalPicture.cs index cc95315..4314717 100644 --- a/PictureHandling/LocalPicture.cs +++ b/PictureHandling/LocalPicture.cs @@ -10,16 +10,57 @@ namespace PictureHandling { public class LocalPicture { + public string PictureFileName { get; set; } public string InitialPath { get; set; } - public string PictureFullPath { - get { + public string PictureFullPath + { + get + { return Path.Combine(InitialPath, PictureFileName); } } public Image AktImage { get; set; } public int AktImWidth { get; set; } public int AktImHeight { get; set; } + public string CreatedDate { get; set; } + public bool CreatedDateChanged { get; set; } = false; + public string AarManDir + { + get + { + if (string.IsNullOrEmpty(CreatedDate)) return RootPosition; + else + { + DateTime dttd = DateTime.Parse(CreatedDate.Substring(0, 10).Replace(":", "-")); + string subDir = dttd.Year.ToString("0000") + "-" + dttd.Month.ToString("00") + @"\"; + return Path.Combine(RootPosition + subDir); + } + } + } + public string AarManDayDir + { + get + { + if (string.IsNullOrEmpty(CreatedDate)) return RootPosition; + else + { + DateTime dttd = DateTime.Parse(CreatedDate.Substring(0, 10).Replace(":", "-")); + string subDir = dttd.Year.ToString("0000") + "_" + dttd.Month.ToString("00") + "_" + dttd.Day.ToString("00") + @"\"; + return Path.Combine(AarManDir + subDir); + } + } + } + + public string ImageNewFullpath + { + get + { + return Path.Combine(AarManDayDir + PictureFileName); + } + } + public string RootPosition { get; set; } + } } diff --git a/PictureHandling/PictureHandling.csproj b/PictureHandling/PictureHandling.csproj index 9535afc..b652691 100644 --- a/PictureHandling/PictureHandling.csproj +++ b/PictureHandling/PictureHandling.csproj @@ -35,6 +35,11 @@ + + ..\packages\System.Collections.Specialized.4.3.0\lib\net46\System.Collections.Specialized.dll + True + True + @@ -56,6 +61,9 @@ + + Form1.cs + ResXFileCodeGenerator Resources.Designer.cs @@ -65,6 +73,7 @@ True Resources.resx + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/PictureHandling/packages.config b/PictureHandling/packages.config new file mode 100644 index 0000000..edfe0fb --- /dev/null +++ b/PictureHandling/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file