Files
PictureRepositApp/PictureReposit/Form1.cs
2021-04-20 23:55:50 +02:00

195 lines
5.2 KiB
C#

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PictureReposit
{
public partial class Form1 : Form
{
private bool Deleting = false;
public Form1()
{
InitializeComponent();
RegistryKey PictureHandling = Registry.LocalMachine.OpenSubKey(@"Software\Tfoman\PictureHandling");
if (PictureHandling == null)
{
txtRootDir.Text = @"D:\Bildlabb\LabRotCat";
}
else
{
txtRootDir.Text = (string)PictureHandling.GetValue(@"RootMap");
}
}
private void btnFileDialog_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Välj bild-fil!";
openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Filter = "Picture files (*.jpg)|*.jpg|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);
FileInfo[] files = di.GetFiles("*.jpg");
lstPicFiles.Items.Clear();
for (int i = 0; i < files.Length; i++)
{
lstPicFiles.Items.Add(files[i].Name);
}
files = di.GetFiles("*.jpeg");
for (int i = 0; i < files.Length; i++)
{
lstPicFiles.Items.Add(files[i].Name);
}
btnReOrganize.Enabled = true;
}
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 btnReOrganize_Click(object sender, EventArgs e)
{
bool error_upd = false;
if (txtRootDir.Text.Trim().Substring(txtRootDir.Text.Trim().Length - 1) != @"\")
txtRootDir.Text = txtRootDir.Text.Trim() + @"\";
DirectoryInfo di = new DirectoryInfo(txtRootDir.Text);
if (!di.Exists) di.Create();
Deleting = true;
pictureBox1.Image = null;
pictureBox1.Refresh();
for (int i = lstPicFiles.Items.Count - 1; i > -1; i--)
{
string lFileName = lstPicFiles.Items[i].ToString().Trim();
string fName = lblPath.Text.Trim() + lFileName;
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();
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 txtRootDir_TextChanged(object sender, EventArgs e)
{
}
}
}