Programmet fungerar nu både via select av enskilda bilder (gt1) och alla jpg/jpeg

This commit is contained in:
2020-07-25 10:24:53 +02:00
parent 6b27788721
commit f06e73c5f1
2 changed files with 202 additions and 111 deletions

View File

@ -20,6 +20,8 @@ namespace PictureHandling
{
public RegistryKey CU { get; set; }
public List<LocalPicture> pictureList = new List<LocalPicture>();
public List<string> DeleteList { get; set; } = new List<string>();
private bool noRegReact = false;
public Form1()
{
InitializeComponent();
@ -47,6 +49,7 @@ namespace PictureHandling
private void txtRootDir_TextChanged(object sender, EventArgs e)
{
if (noRegReact) return;
try
{
RegistryKey IdoIt4u = CU.OpenSubKey(@"SOFTWARE\IdoIt4u", true);
@ -67,7 +70,14 @@ namespace PictureHandling
}
else
{
noRegReact = true;
txtRootDir.Text = txtRootDir.Text.Trim().EndsWith(@"\") ? txtRootDir.Text.Trim() : txtRootDir.Text.Trim() + @"\";
noRegReact = false;
PictureHandling.SetValue(@"RootMap", txtRootDir.Text);
if (pictureList.Count > 0 && txtRootDir.Text != pictureList[0].RootPosition)
{
btnReOrganize.Enabled = true;
}
}
}
catch (Exception ex)
@ -80,6 +90,17 @@ namespace PictureHandling
private void btnReOrganize_Click(object sender, EventArgs e)
{
ReorganizeImages();
pictureList.Clear();
Deleting = true;
lstPicFiles.DataSource = null;
lstPicFiles.Refresh();
pictureBox1.Image = null;
pictureBox1.Refresh();
foreach (var fname in DeleteList)
{
File.Delete(fname);
}
Deleting = false;
}
private void ReorganizeImages()
@ -100,7 +121,7 @@ namespace PictureHandling
}
catch (Exception ex)
{
if (ex.Message == "Det går inte att hitta egenskapen.")
if (ex.HResult == -2147024809)
{
prp = localPic.AktImage.PropertyItems[0];
prp.Id = 306;
@ -114,15 +135,25 @@ namespace PictureHandling
prp.Value = System.Text.ASCIIEncoding.ASCII.GetBytes(localPic.CreatedDate);
localPic.AktImage.SetPropertyItem(prp);
}
if (localPic.RootPosition != txtRootDir.Text)
{
localPic.RootPosition = txtRootDir.Text;
}
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);
localPic.AktImage.Dispose();
DeleteList.Add(localPic.PictureFullPath);
}
btnReOrganize.Enabled = false;
}
private void lstPicFiles_SelectedIndexChanged(object sender, EventArgs e)
@ -131,9 +162,11 @@ namespace PictureHandling
{
string fName = ((ListBox)sender).SelectedValue.ToString();
var lp = ((ListBox)sender).SelectedItem;
//var lp = lstPicFiles.SelectedValue;
lblFileName.Text = fName;
lblCreated.Text = "Skapad: " + ((LocalPicture)lp).CreatedDate;
lblCreated.Text = ((LocalPicture)lp).CreatedDate;
lblGroundPath.Text = ((LocalPicture)lp).AarManDir;
lblFinalPath.Text = ((LocalPicture)lp).AarManDayDir;
int w = ((LocalPicture)lp).AktImWidth;
@ -141,9 +174,13 @@ namespace PictureHandling
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;
}
using (var bmpTmp = new Bitmap(((LocalPicture)lp).PictureFullPath))
{
pictureBox1.Image = new Bitmap(bmpTmp, w * ph / h,ph);
}
}
}
private string TakenDate(Image actPic)
@ -159,7 +196,6 @@ namespace PictureHandling
}
}
}
//actPic.Dispose();
return sPropDatTaken;
}
@ -170,34 +206,50 @@ namespace PictureHandling
openFileDialog1.Filter = "Picture files (*.jpg)|*.jpg;*.jpeg|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Multiselect = true;
openFileDialog1.ShowDialog();
openFileDialog1.Multiselect = false;
string fName = openFileDialog1.FileName;
if (fName.Trim() != "")
var fnames = openFileDialog1.FileNames;
if (fnames.Length > 1)
{
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++)
var outPut = new List<FileInfo>();
foreach (var fname in fnames)
{
if (lstPicFiles.Items[i].ToString() == lblFileName.Text)
{
lstPicFiles.SelectedIndex = i;
break;
}
outPut.Add(new FileInfo(fname));
}
listLocalFiles(outPut);
}
else
{
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()
private void listLocalFiles(List<FileInfo> files = null)
{
DirectoryInfo di = new DirectoryInfo(lblPath.Text);
var files = new List<FileInfo>();
files.AddRange(di.GetFiles("*.jpg"));
files.AddRange(di.GetFiles("*.jpeg"));
if (files == null)
{
DirectoryInfo di = new DirectoryInfo(lblPath.Text);
files = new List<FileInfo>();
files.AddRange(di.GetFiles("*.jpg"));
files.AddRange(di.GetFiles("*.jpeg"));
}
//
Deleting = true;
lstPicFiles.DataSource = null;
@ -206,16 +258,17 @@ namespace PictureHandling
lstPicFiles.DisplayMember = "PictureFileName";
lstPicFiles.ValueMember = "PictureFullPath";
Deleting = false;
files.Clear();
files = null;
btnReOrganize.Enabled = true;
}
public List<LocalPicture> listBoxDataSource(List<FileInfo> fileInfos)
{
Cursor.Current = Cursors.WaitCursor;
var outPut = new List<LocalPicture>();
txtRootDir.Text = txtRootDir.Text.Trim().EndsWith(@"\") ? txtRootDir.Text.Trim() : txtRootDir.Text.Trim() + @"\";
foreach (var fi in fileInfos)
{
@ -237,12 +290,13 @@ namespace PictureHandling
outPut.Add(bufPicture);
}
Cursor.Current = Cursors.Default;
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)
{
@ -281,7 +335,6 @@ namespace PictureHandling
outPut.Add(lPic.Value);
}
}
Debug.WriteLine($"FillupDateGaps output has {outPut.Count} files.");
return outPut;
}
}