using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; 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 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) { 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 { PictureHandling.SetValue(@"RootMap", txtRootDir.Text); } } catch (Exception ex) { MessageBox.Show($"RegistryError: {ex}"); } } 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() + @"\"; 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--) { 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() != "") { 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 { error_upd = false; try { lstPicFiles.BeginUpdate(); } catch { error_upd = true; } } while (error_upd); lstPicFiles.Items.RemoveAt(i); lstPicFiles.EndUpdate(); lstPicFiles.Refresh(); lblCreated.Refresh(); lblFileName.Refresh(); pictureBox1.Refresh(); this.Refresh(); } } Deleting = false; lblCreated.Text = ""; lblFileName.Text = ""; lblPath.Text = ""; btnReOrganize.Enabled = false; } private void lstPicFiles_SelectedIndexChanged(object sender, EventArgs e) { if (!Deleting) { string fName = ((ListBox)sender).SelectedItem.ToString(); lblFileName.Text = fName; Image actPic = Image.FromFile(lblPath.Text + fName, true); lblCreated.Text = "Skapad: " + TakenDate(actPic); int w = actPic.Width; int h = actPic.Height; int pw = pictureBox1.Width; int ph = pictureBox1.Height; pictureBox1.SizeMode = PictureBoxSizeMode.Normal; Bitmap to_bm = new Bitmap((Image)actPic.Clone(), w * ph / h, ph); pictureBox1.Image = to_bm; actPic = null; } } 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]); } } } //actPic.Dispose(); 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.ShowDialog(); openFileDialog1.Multiselect = false; 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() { DirectoryInfo di = new DirectoryInfo(lblPath.Text); List 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); } btnReOrganize.Enabled = true; } } }