289 lines
9.8 KiB
C#
289 lines
9.8 KiB
C#
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<LocalPicture> pictureList = new List<LocalPicture>();
|
|
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)
|
|
{
|
|
ReorganizeImages();
|
|
}
|
|
|
|
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.Message == "Det går inte att hitta egenskapen.")
|
|
{
|
|
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);
|
|
}
|
|
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);
|
|
}
|
|
|
|
btnReOrganize.Enabled = false;
|
|
}
|
|
|
|
private void lstPicFiles_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (!Deleting)
|
|
{
|
|
string fName = ((ListBox)sender).SelectedValue.ToString();
|
|
var lp = ((ListBox)sender).SelectedItem;
|
|
lblFileName.Text = fName;
|
|
|
|
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)((LocalPicture)lp).AktImage.Clone(), w * ph / h, ph);
|
|
pictureBox1.Image = to_bm;
|
|
}
|
|
}
|
|
|
|
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);
|
|
var files = new List<FileInfo>();
|
|
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;
|
|
|
|
btnReOrganize.Enabled = true;
|
|
}
|
|
|
|
public List<LocalPicture> listBoxDataSource(List<FileInfo> fileInfos)
|
|
{
|
|
|
|
var outPut = new List<LocalPicture>();
|
|
|
|
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<LocalPicture> FillupDateGaps(List<LocalPicture> inPut)
|
|
{
|
|
Debug.WriteLine($"FillupDateGaps input has {inPut.Count} files.");
|
|
var picReg = new SortedList<string, LocalPicture>();
|
|
foreach (var pict in inPut)
|
|
{
|
|
picReg.Add(pict.PictureFileName, pict);
|
|
}
|
|
var tmpPicture = new LocalPicture();
|
|
tmpPicture.CreatedDate = "";
|
|
Stack<string> stack = new Stack<string>();
|
|
var outPut = new List<LocalPicture>();
|
|
|
|
foreach (KeyValuePair<string, LocalPicture> 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;
|
|
}
|
|
}
|
|
}
|