Nu fungerar hela inläsningen och reorganisationen !

This commit is contained in:
2020-07-22 00:13:48 +02:00
parent f9437f04be
commit 6b27788721
6 changed files with 373 additions and 107 deletions

View File

@ -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<FileInfo> files = new List<FileInfo>();
files.AddRange( di.GetFiles("*.jpg"));
var files = new List<FileInfo>();
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<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;
}
}
}