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; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PictureHandling { public partial class Form1 : Form { public RegistryKey CU { get; set; } public List pictureList = new List(); public List DeleteList { get; set; } = new List(); private bool noRegReact = false; public Form1() { InitializeComponent(); //TestRegistryPermission(); try { CU = Registry.CurrentUser; RegistryKey PictureHandling = CU.OpenSubKey(@"SOFTWARE\IdoIt4u\PictureHandling"); if (PictureHandling == null) { txtRootDir.Text = @"D:\OurPictures"; } else { txtRootDir.Text = (string)PictureHandling.GetValue(@"RootMap"); } } catch (Exception ex) { MessageBox.Show($"RegistryError: {ex}"); } } private void txtRootDir_TextChanged(object sender, EventArgs e) { if (noRegReact) return; try { RegistryKey IdoIt4u = CU.OpenSubKey(@"SOFTWARE\IdoIt4u", true); RegistryKey PictureHandling = null; if (IdoIt4u != null) { PictureHandling = IdoIt4u.OpenSubKey(@"PictureHandling", true); } if (PictureHandling == null) { if (IdoIt4u == null) { IdoIt4u = Registry.CurrentUser.CreateSubKey(@"Software\IdoIt4u", true); } else PictureHandling = IdoIt4u.CreateSubKey(@"PictureHandling", true); } else { noRegReact = true; txtRootDir.Text = txtRootDir.Text.Trim().EndsWith(@"\") ? txtRootDir.Text.Trim() : txtRootDir.Text.Trim() + @"\"; noRegReact = false; PictureHandling.SetValue(@"RootMap", txtRootDir.Text); if (pictureList.Count > 0 && txtRootDir.Text != pictureList[0].RootPosition) { btnReOrganize.Enabled = true; } } } catch (Exception ex) { MessageBox.Show($"RegistryError: {ex}"); } } private void btnReOrganize_Click(object sender, EventArgs e) { ReorganizeImages(); pictureList.Clear(); Deleting = true; lstPicFiles.DataSource = null; lstPicFiles.Refresh(); pictureBox1.Image = null; pictureBox1.Refresh(); foreach (var fname in DeleteList) { File.Delete(fname); } Deleting = false; } private void ReorganizeImages() { DirectoryInfo di = new DirectoryInfo(txtRootDir.Text); if (!di.Exists) di.Create(); // om den nya rotkatalogen saknas skapa den foreach (var localPic in pictureList) { if (localPic.CreatedDateChanged) { PropertyItem prp; try { prp = localPic.AktImage.GetPropertyItem(306); } catch (Exception ex) { if (ex.HResult == -2147024809) { prp = localPic.AktImage.PropertyItems[0]; prp.Id = 306; prp.Len = localPic.CreatedDate.Length; } else { throw; } } prp.Value = System.Text.ASCIIEncoding.ASCII.GetBytes(localPic.CreatedDate); localPic.AktImage.SetPropertyItem(prp); } if (localPic.RootPosition != txtRootDir.Text) { localPic.RootPosition = txtRootDir.Text; } 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); localPic.AktImage.Dispose(); DeleteList.Add(localPic.PictureFullPath); } btnReOrganize.Enabled = false; } private void lstPicFiles_SelectedIndexChanged(object sender, EventArgs e) { if (!Deleting) { string fName = ((ListBox)sender).SelectedValue.ToString(); var lp = ((ListBox)sender).SelectedItem; //var lp = lstPicFiles.SelectedValue; lblFileName.Text = fName; lblCreated.Text = ((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; using (var bmpTmp = new Bitmap(((LocalPicture)lp).PictureFullPath)) { pictureBox1.Image = new Bitmap(bmpTmp, w * ph / h,ph); } } } private string TakenDate(Image actPic) { string sPropDatTaken = ""; for (int i = 0; i < actPic.PropertyIdList.Length; i++) { if (actPic.PropertyIdList[i] == 306) { for (int j = 0; j < actPic.PropertyItems[i].Len; j++) { sPropDatTaken += Convert.ToChar(actPic.PropertyItems[i].Value[j]); } } } return sPropDatTaken; } private void btnFileDialog_Click(object sender, EventArgs e) { openFileDialog1.Title = "Välj bild-fil!"; openFileDialog1.InitialDirectory = "C:\\"; openFileDialog1.Filter = "Picture files (*.jpg)|*.jpg;*.jpeg|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; openFileDialog1.Multiselect = true; openFileDialog1.ShowDialog(); var fnames = openFileDialog1.FileNames; if (fnames.Length > 1) { var outPut = new List(); foreach (var fname in fnames) { outPut.Add(new FileInfo(fname)); } listLocalFiles(outPut); } else { string fName = openFileDialog1.FileName; if (fName.Trim() != "") { int fNamePos = fName.LastIndexOf(@"\") + 1; lblFileName.Text = fName.Substring(fNamePos); lblPath.Text = fName.Substring(0, fNamePos); listLocalFiles(); for (int i = 0; i < lstPicFiles.Items.Count; i++) { if (lstPicFiles.Items[i].ToString() == lblFileName.Text) { lstPicFiles.SelectedIndex = i; break; } } } } } private void listLocalFiles(List files = null) { if (files == null) { DirectoryInfo di = new DirectoryInfo(lblPath.Text); files = new List(); files.AddRange(di.GetFiles("*.jpg")); files.AddRange(di.GetFiles("*.jpeg")); } // Deleting = true; lstPicFiles.DataSource = null; pictureList = listBoxDataSource(files); lstPicFiles.DataSource = pictureList; lstPicFiles.DisplayMember = "PictureFileName"; lstPicFiles.ValueMember = "PictureFullPath"; Deleting = false; files.Clear(); files = null; btnReOrganize.Enabled = true; } public List listBoxDataSource(List fileInfos) { Cursor.Current = Cursors.WaitCursor; var outPut = new List(); 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); } Cursor.Current = Cursors.Default; return FillupDateGaps(outPut); } private List FillupDateGaps(List inPut) { 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); } } return outPut; } } }