Using MetaDataExtractor, and a lot of changes
This commit is contained in:
11
PictureReposit/Form1.Designer.cs
generated
11
PictureReposit/Form1.Designer.cs
generated
@ -41,6 +41,7 @@ namespace PictureReposit
|
|||||||
this.btnReOrganize = new System.Windows.Forms.Button();
|
this.btnReOrganize = new System.Windows.Forms.Button();
|
||||||
this.label3 = new System.Windows.Forms.Label();
|
this.label3 = new System.Windows.Forms.Label();
|
||||||
this.txtRootDir = new System.Windows.Forms.TextBox();
|
this.txtRootDir = new System.Windows.Forms.TextBox();
|
||||||
|
this.pbReorgProcess = new System.Windows.Forms.ProgressBar();
|
||||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
|
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
|
||||||
this.SuspendLayout();
|
this.SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -155,11 +156,20 @@ namespace PictureReposit
|
|||||||
this.txtRootDir.Text = "D:\\Bildlabb\\LabRotCat";
|
this.txtRootDir.Text = "D:\\Bildlabb\\LabRotCat";
|
||||||
this.txtRootDir.TextChanged += new System.EventHandler(this.txtRootDir_TextChanged);
|
this.txtRootDir.TextChanged += new System.EventHandler(this.txtRootDir_TextChanged);
|
||||||
//
|
//
|
||||||
|
// pbReorgProcess
|
||||||
|
//
|
||||||
|
this.pbReorgProcess.Location = new System.Drawing.Point(183, 72);
|
||||||
|
this.pbReorgProcess.Name = "pbReorgProcess";
|
||||||
|
this.pbReorgProcess.Size = new System.Drawing.Size(654, 79);
|
||||||
|
this.pbReorgProcess.TabIndex = 11;
|
||||||
|
this.pbReorgProcess.Visible = false;
|
||||||
|
//
|
||||||
// Form1
|
// Form1
|
||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
this.ClientSize = new System.Drawing.Size(869, 642);
|
this.ClientSize = new System.Drawing.Size(869, 642);
|
||||||
|
this.Controls.Add(this.pbReorgProcess);
|
||||||
this.Controls.Add(this.txtRootDir);
|
this.Controls.Add(this.txtRootDir);
|
||||||
this.Controls.Add(this.label3);
|
this.Controls.Add(this.label3);
|
||||||
this.Controls.Add(this.btnReOrganize);
|
this.Controls.Add(this.btnReOrganize);
|
||||||
@ -193,6 +203,7 @@ namespace PictureReposit
|
|||||||
private System.Windows.Forms.Button btnReOrganize;
|
private System.Windows.Forms.Button btnReOrganize;
|
||||||
private System.Windows.Forms.Label label3;
|
private System.Windows.Forms.Label label3;
|
||||||
private System.Windows.Forms.TextBox txtRootDir;
|
private System.Windows.Forms.TextBox txtRootDir;
|
||||||
|
private System.Windows.Forms.ProgressBar pbReorgProcess;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Win32;
|
using MetadataExtractor;
|
||||||
|
using Microsoft.Win32;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
@ -64,22 +65,16 @@ namespace PictureReposit
|
|||||||
private void listLocalFiles()
|
private void listLocalFiles()
|
||||||
{
|
{
|
||||||
DirectoryInfo di = new DirectoryInfo(lblPath.Text);
|
DirectoryInfo di = new DirectoryInfo(lblPath.Text);
|
||||||
FileInfo[] files = di.GetFiles("*.jpg");
|
FileInfo[] files = new string[] { "*.png", "*.jpg", "*.jpeg", "*.img", "*.mov" }
|
||||||
|
.SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
lstPicFiles.Items.Clear();
|
lstPicFiles.Items.Clear();
|
||||||
for (int i = 0; i < files.Length; i++)
|
for (int i = 0; i < files.Length; i++)
|
||||||
{
|
{
|
||||||
lstPicFiles.Items.Add(files[i].Name);
|
lstPicFiles.Items.Add(files[i].Name);
|
||||||
}
|
}
|
||||||
files = di.GetFiles("*.jpeg");
|
|
||||||
for (int i = 0; i < files.Length; i++)
|
|
||||||
{
|
|
||||||
lstPicFiles.Items.Add(files[i].Name);
|
|
||||||
}
|
|
||||||
files = di.GetFiles("*.png");
|
|
||||||
for (int i = 0; i < files.Length; i++)
|
|
||||||
{
|
|
||||||
lstPicFiles.Items.Add(files[i].Name);
|
|
||||||
}
|
|
||||||
btnReOrganize.Enabled = true;
|
btnReOrganize.Enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,8 +84,16 @@ namespace PictureReposit
|
|||||||
{
|
{
|
||||||
string fName = ((ListBox)sender).SelectedItem.ToString();
|
string fName = ((ListBox)sender).SelectedItem.ToString();
|
||||||
lblFileName.Text = fName;
|
lblFileName.Text = fName;
|
||||||
Image actPic = Image.FromFile(lblPath.Text + fName, true);
|
Image actPic = null;
|
||||||
lblCreated.Text = "Skapad: " + TakenDate(actPic);
|
if (fName.ToUpper().EndsWith(".MOV"))
|
||||||
|
{
|
||||||
|
actPic = Image.FromFile("IMG_2994.jpg", true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
actPic = Image.FromFile(lblPath.Text + fName, true);
|
||||||
|
}
|
||||||
|
lblCreated.Text = "Skapad: " + GetOrigCreateDateTimeFromMediaFile(new FileInfo(lblPath.Text + fName)).ToString(); ;
|
||||||
int w = actPic.Width;
|
int w = actPic.Width;
|
||||||
int h = actPic.Height;
|
int h = actPic.Height;
|
||||||
int pw = pictureBox1.Width;
|
int pw = pictureBox1.Width;
|
||||||
@ -103,111 +106,87 @@ namespace PictureReposit
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 btnReOrganize_Click(object sender, EventArgs e)
|
private void btnReOrganize_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
bool error_upd = false;
|
|
||||||
bool slaskDir = false;
|
bool slaskDir = false;
|
||||||
int slaskFilNr = 0;
|
int slaskFilNr = 0;
|
||||||
|
string fromPath = lblPath.Text.Trim();
|
||||||
|
pbReorgProcess.Visible = true;
|
||||||
|
pictureBox1.Visible = false;
|
||||||
if (txtRootDir.Text.Trim().Substring(txtRootDir.Text.Trim().Length - 1) != @"\")
|
if (txtRootDir.Text.Trim().Substring(txtRootDir.Text.Trim().Length - 1) != @"\")
|
||||||
txtRootDir.Text = txtRootDir.Text.Trim() + @"\";
|
txtRootDir.Text = txtRootDir.Text.Trim() + @"\";
|
||||||
DirectoryInfo di = new DirectoryInfo(txtRootDir.Text);
|
DirectoryInfo di = new DirectoryInfo(txtRootDir.Text);
|
||||||
if (!di.Exists) di.Create();
|
if (!di.Exists) di.Create();
|
||||||
Deleting = true;
|
Deleting = true;
|
||||||
|
|
||||||
pictureBox1.Image = null;
|
pictureBox1.Image = null;
|
||||||
pictureBox1.Refresh();
|
pictureBox1.Refresh();
|
||||||
for (int i = lstPicFiles.Items.Count - 1; i > -1; i--)
|
try
|
||||||
{
|
{
|
||||||
string lFileName = lstPicFiles.Items[i].ToString().Trim();
|
pbReorgProcess.Maximum = lstPicFiles.Items.Count;
|
||||||
string fName = lblPath.Text.Trim() + lFileName;
|
pbReorgProcess.Minimum = 0;
|
||||||
lblFileName.Text = fName;
|
|
||||||
Image actIm = Image.FromFile(fName, true);
|
|
||||||
int w = actIm.Width;
|
|
||||||
int h = actIm.Height;
|
|
||||||
int pw = pictureBox1.Width;
|
|
||||||
int ph = pictureBox1.Height;
|
|
||||||
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
|
|
||||||
Bitmap to_bm = new Bitmap((Image)actIm.Clone(), w * ph / h, ph);
|
|
||||||
pictureBox1.Image = to_bm;
|
|
||||||
string td = TakenDate(actIm);
|
|
||||||
|
|
||||||
actIm.Dispose();
|
for (int i = lstPicFiles.Items.Count - 1; i > -1; i--)
|
||||||
if (td.Trim() != "")
|
|
||||||
{
|
{
|
||||||
|
pbReorgProcess.Value = lstPicFiles.Items.Count-i;
|
||||||
|
string lFileName = lstPicFiles.Items[i].ToString().Trim();
|
||||||
|
string fName = fromPath + lFileName;
|
||||||
|
lblFileName.Text = fName;
|
||||||
|
FileInfo fi = new FileInfo(fName);
|
||||||
|
string td = GetOrigCreateDateTimeFromMediaFile(fi).ToString();
|
||||||
lblCreated.Text = "Skapad: " + td;
|
lblCreated.Text = "Skapad: " + td;
|
||||||
td = td.Substring(0, 10);
|
if (td.Trim() != DateTime.MinValue.ToString())
|
||||||
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;
|
lblCreated.Text = "Skapad: " + td;
|
||||||
try
|
td = td.Substring(0, 10);
|
||||||
{
|
DateTime dttd = DateTime.Parse(td);
|
||||||
lstPicFiles.BeginUpdate();
|
string subDir1 = dttd.Year.ToString("0000") + "-" + dttd.Month.ToString("00") + @"\";
|
||||||
}
|
string subDir2 = dttd.Year.ToString("0000") + "_" + dttd.Month.ToString("00") + "_" + dttd.Day.ToString("00") + @"\";
|
||||||
catch
|
DirectoryInfo dis = new DirectoryInfo(di.FullName + subDir1);
|
||||||
{
|
if (!dis.Exists) dis.Create();
|
||||||
error_upd = true;
|
DirectoryInfo diss = new DirectoryInfo(dis.FullName + subDir2);
|
||||||
}
|
if (!diss.Exists) diss.Create();
|
||||||
|
lblPath.Text = diss.FullName;
|
||||||
|
lblFileName.Refresh();
|
||||||
|
lblCreated.Refresh();
|
||||||
|
lblPath.Refresh();
|
||||||
|
fi.CopyTo(diss.FullName + lFileName, true);
|
||||||
}
|
}
|
||||||
while (error_upd);
|
else
|
||||||
lstPicFiles.Items.RemoveAt(i);
|
|
||||||
lstPicFiles.EndUpdate();
|
|
||||||
lstPicFiles.Refresh();
|
|
||||||
lblCreated.Refresh();
|
|
||||||
lblFileName.Refresh();
|
|
||||||
pictureBox1.Refresh();
|
|
||||||
this.Refresh();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
DirectoryInfo dis = new DirectoryInfo(di.FullName + "NoDateInExif\\");
|
|
||||||
if (!slaskDir)
|
|
||||||
{
|
{
|
||||||
if (!dis.Exists)
|
DirectoryInfo dis = new DirectoryInfo(di.FullName + "NoDateInExif\\");
|
||||||
|
if (!slaskDir)
|
||||||
{
|
{
|
||||||
dis.Create();
|
if (!dis.Exists)
|
||||||
set_Last_number(dis.FullName, 0);
|
{
|
||||||
|
dis.Create();
|
||||||
|
set_Last_number(dis.FullName, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
slaskFilNr = get_Last_number(dis.FullName);
|
||||||
|
}
|
||||||
|
slaskDir = true;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
slaskFilNr = get_Last_number(dis.FullName);
|
|
||||||
}
|
|
||||||
slaskDir = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
FileInfo fi = new FileInfo(fName);
|
slaskFilNr++;
|
||||||
//Tommy
|
fi.CopyTo(dis.FullName + slaskFilNr.ToString() + "_" + lFileName);
|
||||||
slaskFilNr++;
|
}
|
||||||
fi.CopyTo(dis.FullName + slaskFilNr.ToString() + "_" + lFileName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"Error msg: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (slaskFilNr > 0)
|
||||||
|
{
|
||||||
|
set_Last_number(di.FullName + "NoDateInExif\\", slaskFilNr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lstPicFiles.Items.Clear();
|
||||||
Deleting = false;
|
Deleting = false;
|
||||||
lblCreated.Text = "";
|
lblCreated.Text = "";
|
||||||
lblFileName.Text = "";
|
lblFileName.Text = "";
|
||||||
@ -217,6 +196,8 @@ namespace PictureReposit
|
|||||||
{
|
{
|
||||||
set_Last_number(di.FullName + "NoDateInExif\\", slaskFilNr);
|
set_Last_number(di.FullName + "NoDateInExif\\", slaskFilNr);
|
||||||
}
|
}
|
||||||
|
pbReorgProcess.Visible = false;
|
||||||
|
pictureBox1.Visible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void txtRootDir_TextChanged(object sender, EventArgs e)
|
private void txtRootDir_TextChanged(object sender, EventArgs e)
|
||||||
@ -250,11 +231,99 @@ namespace PictureReposit
|
|||||||
outPut++;
|
outPut++;
|
||||||
return outPut;
|
return outPut;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void set_Last_number(string path, int lastNr)
|
private void set_Last_number(string path, int lastNr)
|
||||||
{
|
{
|
||||||
var lstNrFile = Path.Combine(path, "lastNrFile.txt");
|
var lstNrFile = Path.Combine(path, "lastNrFile.txt");
|
||||||
var SavedNumber = lastNr.ToString();
|
var SavedNumber = lastNr.ToString();
|
||||||
File.WriteAllText(lstNrFile, SavedNumber);
|
File.WriteAllText(lstNrFile, SavedNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------
|
||||||
|
// Nya funktioner för hantering av original datum
|
||||||
|
//------------------------------------------------------------------------
|
||||||
|
|
||||||
|
DateTime GetOrigCreateDateTimeFromMediaFile(FileInfo file)
|
||||||
|
{
|
||||||
|
DateTime origDateTime = DateTime.MinValue;
|
||||||
|
var creationDate = getCreationDate(file);
|
||||||
|
if (creationDate != "")
|
||||||
|
{
|
||||||
|
if (file.Extension.ToLower() != ".mov")
|
||||||
|
{
|
||||||
|
creationDate = prepDateString(creationDate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
creationDate = prepMovDateString(creationDate);
|
||||||
|
}
|
||||||
|
origDateTime = DateTime.Parse(creationDate);
|
||||||
|
}
|
||||||
|
return origDateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private string prepMovDateString(string creationDate)
|
||||||
|
{
|
||||||
|
var x = creationDate.Split(" ");
|
||||||
|
return $"{x[0]} {x[2]} {x[1]} {x[4]} {x[3]}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private string prepDateString(string creationDate)
|
||||||
|
{
|
||||||
|
creationDate = creationDate.Replace(" ", "T");
|
||||||
|
var x = creationDate.Split("T");
|
||||||
|
x[0] = x[0].Replace(":", "-");
|
||||||
|
return x[0] + "T" + x[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
private string getCreationDate(FileInfo file)
|
||||||
|
{
|
||||||
|
|
||||||
|
string output = "";
|
||||||
|
switch (file.Extension.ToLower())
|
||||||
|
{
|
||||||
|
case ".jpg":
|
||||||
|
case ".jpeg":
|
||||||
|
{
|
||||||
|
output = getExifValue("Exif IFD0", "Date/Time", file);
|
||||||
|
if (output == "")
|
||||||
|
{
|
||||||
|
output = getExifValue("Exif SubIFD", "Date/Time Original", file);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ".png":
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ".mov":
|
||||||
|
{
|
||||||
|
output = getExifValue("QuickTime Movie Header", "Created", file);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string getExifValue(string key1, string key2, FileInfo fileInfo)
|
||||||
|
{
|
||||||
|
string output = "";
|
||||||
|
foreach (var directory in ImageMetadataReader.ReadMetadata(fileInfo.FullName))
|
||||||
|
{
|
||||||
|
if (directory.Name == key1)
|
||||||
|
{
|
||||||
|
foreach (var tag in directory.Tags)
|
||||||
|
{
|
||||||
|
if (tag.Name == key2)
|
||||||
|
{
|
||||||
|
output = tag.Description;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
PictureReposit/IMG_2994.jpg
Normal file
BIN
PictureReposit/IMG_2994.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.0 MiB |
@ -6,4 +6,12 @@
|
|||||||
<UseWindowsForms>true</UseWindowsForms>
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="IMG_2994.jpg">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="MetadataExtractor" Version="2.4.3" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
Reference in New Issue
Block a user