public bool SaveOri() { try { string mess1 = ""; if (_oriCurrent == -100){ mess1 = T._("This file has no Exif orientation."); MessageBox.Show(mess1, T._("Save not possible"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } if (_oriCurrent == _oriInitial){ mess1 = T._("Orientation not changed"); MessageBox.Show(mess1, T._("Save not possible"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } byte oriByte; switch(_oriCurrent) { case 1: oriByte = 6; // 90 l break; case 2: oriByte = 3; // 180 break; case 3: oriByte = 8; // 270 break; default: oriByte = 1; // 0 break; } ushort ori = 0; using (var reader = new ExifReader(_currentPath)) { if (reader.GetTagValue(ExifTags.Orientation, out ori)) { reader.SaveOrient(oriByte); } } // set original date after update DateTime dtOriginal = DateTime.MinValue; ExifRead.ExifODate(out dtOriginal, _currentPath); System.IO.File.SetLastWriteTime(_currentPath, dtOriginal); //Debug.WriteLine("Orient ini: {0}, current {1}, byte {2} ", _oriInitial, _oriCurrent, ori); return true; } catch (Exception e) { MessageBox.Show(T._("Error for update") + "\n" + e.Message, T._("Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } }
//------------------------------ bar functions ---------------------------------------------------------- public void ScanImagesBar( ) { List<ImgFile> imList; _il.ImgListOut(out imList); DateTime dtOriginal = DateTime.MinValue; foreach (ImgFile imf in imList) { string picPath = imf.fName; DateTime dtCreation = File.GetCreationTime(picPath); DateTime dtChanged = File.GetLastWriteTime(picPath); DateTime dtFile; if (dtCreation < dtChanged){ dtFile = dtCreation; } else { dtFile = dtChanged; } imf.fDate = dtFile; ExifRead.ExifODate(out dtOriginal, picPath); imf.fDateOriginal = dtOriginal; } }
// ------------------------------ BackgroundWorker ---------------------------------------------------------- public bool ScanImages(BackgroundWorker bw) // called by: BackgroundWorker1DoWork // shown with ExifDataShow { var dashImgList = new List <string>(); bool allDirs = false; if (allDirs) { dashImgList = Directory.GetFiles(_imgDir, "*.*", SearchOption.AllDirectories) .Where(file => _validExtensions.Any(file.ToLower().EndsWith)) .ToList(); } else { dashImgList = Directory.GetFiles(_imgDir) .Where(file => _validExtensions.Any(file.ToLower().EndsWith)) .ToList(); } FilenameComparer fc = new FilenameComparer(); dashImgList.Sort(fc); int exType; string orientation; string model; DateTime dtOriginal; int timeOfD; string expotime; string fnumber; string fLength; bool flash; string exposi; string lens; string scene; bool gps; bool face; string exError; DateTime nullDate = DateTime.MinValue; int maxTicks = dashImgList.Count() / 100; if (maxTicks < 1) { maxTicks = 1; } bw.ReportProgress(maxTicks, T._("Start")); int fCount = 0; DateTime priorDate = DateTime.MaxValue; var spanDict = new Dictionary <int, int>(); ExifRead.ResetBox(); foreach (string picPath in dashImgList) { ExifRead.CheckExif(out exType, out orientation, out model, out dtOriginal, out timeOfD, out expotime, out fnumber, out fLength, out flash, out exposi, out lens, out scene, out gps, out face, out exError, picPath); DateTime dtCreation = File.GetCreationTime(picPath); DateTime dtChanged = File.GetLastWriteTime(picPath); DateTime dtFile; if (dtCreation < dtChanged) { dtFile = dtCreation; } else { dtFile = dtChanged; } exList.Add(new Exif(exType, model, dtOriginal, timeOfD, expotime, fnumber, fLength, flash, exposi, lens, scene, gps, face, picPath, dtFile)); if (exError != "") { errorList.Add(exError); } fCount++; int mod = fCount % 100; if (mod == 0) { bw.ReportProgress(-1, ""); } if (dtOriginal != nullDate) { //Debug.WriteLine("path: " + picPath + " " + dtOriginal.ToString()); _dateCount++; if (_minDate > dtOriginal) { _minDate = dtOriginal; } if (_maxDate < dtOriginal) { _maxDate = dtOriginal; } if (_dateCount > 1) { TimeSpan span = dtOriginal.Subtract(priorDate); int spanSec = Math.Abs((int)span.TotalSeconds); spanDict.Add(fCount, spanSec); } priorDate = dtOriginal; } if (dicExift.ContainsKey(exType)) { dicExift[exType] += 1; } else { dicExift[exType] = 1; } if (dicModel.ContainsKey(model)) { dicModel[model] += 1; } else { dicModel[model] = 1; } if (dicToD.ContainsKey(timeOfD)) { dicToD[timeOfD] += 1; } else { dicToD[timeOfD] = 1; } if (dicExpot.ContainsKey(expotime)) { dicExpot[expotime] += 1; } else { dicExpot[expotime] = 1; } if (dicFNum.ContainsKey(fnumber)) { dicFNum[fnumber] += 1; } else { dicFNum[fnumber] = 1; } if (dicFLen.ContainsKey(fLength)) { dicFLen[fLength] += 1; } else { dicFLen[fLength] = 1; } if (flash) { ++_flashCount; } if (dicExposi.ContainsKey(exposi)) { dicExposi[exposi] += 1; } else { dicExposi[exposi] = 1; } if (dicLens.ContainsKey(lens)) { dicLens[lens] += 1; } else { dicLens[lens] = 1; } if (dicScene.ContainsKey(scene)) { dicScene[scene] += 1; } else { dicScene[scene] = 1; } if (gps) { ++_gpsCount; } if (face) { ++_faceCount; } } return(true); }
public bool PicLoad(string pPath) { //Debug.WriteLine("pic load: " + pPath); try { if (!File.Exists(pPath)){ picBox.SizeMode = PictureBoxSizeMode.CenterImage; picBox.Image = picBox.ErrorImage; return false; } //Stopwatch sw1 = new Stopwatch(); //sw1.Start(); bool showOk = ShowExif(); //sw1.Stop(); if (!showOk){ // exif form not shown ExifRead.ExifOrient(ref _exifType, ref _orientationStr, _currentPath); } //Debug.WriteLine("Ticks: " + sw1.Elapsed.Ticks.ToString()); //var t = new DateTime(sw1.Elapsed.Ticks); //Debug.WriteLine("Exif time: {0:D2}s:{1:D5}ms", t.Second, t.Millisecond); //Image myImg; _oriInitial = -100; using (FileStream stream = new FileStream(pPath, FileMode.Open, FileAccess.Read)) { _myImg = Image.FromStream(stream); // abort for gif if (_orientationStr.Equals("right side, top (rotate 90 cw)")){ _myImg.RotateFlip(RotateFlipType.Rotate90FlipNone); _oriInitial = 1; } else if (_orientationStr.Equals("bottom, right side (rotate 180)")){ _myImg.RotateFlip(RotateFlipType.Rotate180FlipNone); _oriInitial = 2; } else if (_orientationStr.Equals("left side, bottom (rotate 270 cw)")){ _myImg.RotateFlip(RotateFlipType.Rotate270FlipNone); _oriInitial = 3; } else if (_orientationStr.Equals("top, left side (horizontal / normal)")){ _oriInitial = 0; } stream.Close(); } _oriCurrent = _oriInitial; GC.Collect(); Application.DoEvents(); // like delphi processMessages //using (Image bmpTemp = new Bitmap(pPath)) // abort for invalid jpg //{ // _myImg = new Bitmap(bmpTemp); // if(bmpTemp != null) // ((IDisposable)bmpTemp).Dispose(); //} //GC.Collect(); string ext = Path.GetExtension(pPath).ToLower(); if (ext == ".gif"){ SetCommand('g', ""); picBox.Image = Image.FromFile(pPath); // workaround, only direct load makes gif animation, but file can't be renamed gifImage = new GifImage(pPath); _isGif = true; } else { SetCommand('h', ""); picBox.Image = _myImg; picBox.Enabled = true; _isGif = false; } int scalePerc = PicSetSize(); //Debug.WriteLine("Size: {0}x{1}", picBox.Image.Width, picBox.Image.Height); SetStatusText(-2, String.Format("Size: {0} x {1} ({2} %)", picBox.Image.Width, picBox.Image.Height, scalePerc)); if (_wType != WinType.second){ Settings.Default.LastImage = pPath; } Show2ndPic(_priorPath); //Debug.WriteLine("pic end " + pPath); _barClick = true; return true; } catch (Exception e) { picBox.Image = null; MessageBox.Show(T._("File is invalid") + "\n " + pPath + "\n " + e.Message, T._("Invalid file"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } }
public void CalcBar( ) { List<ImgFile> imList; _il.ImgListOut(out imList); DateTime dtOriginal = DateTime.MinValue; DateTime nullDate = DateTime.MinValue; DateTime minDate = DateTime.MaxValue; DateTime maxDate = DateTime.MinValue; int fCount = 0; int fCountExif = -1; int dateCount = 0; DateTime priorDate = DateTime.MinValue; var spanDict = new Dictionary<int, int>(); _posList.Clear(); _priorList.Clear(); _rangeDict.Clear(); // dict for time gaps foreach (ImgFile imf in imList) { string picPath = imf.fName; dtOriginal = imf.fDateOriginal; fCount++; string fName = Path.GetFileName(picPath); int pPos = fName.IndexOf("+"); if (pPos > -1){ _posList.Add(fCount); } if (dtOriginal != nullDate){ //Debug.WriteLine("path: " + picPath + " " + dtOriginal.ToString()); fCountExif = fCount; dateCount++; imf.fDateOriginal = dtOriginal; if (priorDate > dtOriginal){ //Debug.WriteLine("prior date: " + fCount); _priorList.Add(fCount); } if (minDate > dtOriginal) minDate = dtOriginal; if (maxDate < dtOriginal) maxDate = dtOriginal; if (dateCount > 1){ TimeSpan span = dtOriginal.Subtract(priorDate); int spanSec = Math.Abs((int) span.TotalSeconds); spanDict.Add(fCount, spanSec); } priorDate = dtOriginal; } else { // no exif date if (fCountExif == fCount - 1){ // gap to exif date spanDict.Add(fCount, Int32.MaxValue); } } } // span values if (dateCount > 0){ TimeSpan imgSpan = maxDate.Subtract(minDate); //Debug.WriteLine("min: " + minDate.ToString() + " max: " + maxDate.ToString()); //Debug.WriteLine("range: " + imgSpan.ToString()); int mean = (int) imgSpan.TotalSeconds / dateCount; long sumVar = 0; foreach (KeyValuePair<int, int> sd in spanDict) { if (sd.Value != Int32.MaxValue){ long var = (long) Math.Pow((mean - sd.Value), 2); sumVar += var; //Debug.WriteLine("F Num: " + dfn.Key + " " + dfn.Value); } } int stdDev = (int) Math.Sqrt(sumVar / dateCount); //Debug.WriteLine("mean / std: {0}/{1}", mean, stdDev); int breakVal = mean + stdDev * 2; //int wi = 2; if (imgSpan.TotalDays > 730){ _rangeType = 1; // years } else if (imgSpan.TotalDays > 60){ _rangeType = 2; // months } else if (imgSpan.TotalDays > 1){ _rangeType = 3; // days } else { _rangeType = 4; // hours } int i = 0; // largest breaks string p0Path = ""; if (_il.DirPathPos(ref p0Path, 1)){ ExifRead.ExifODate(out _r0Date, p0Path); } foreach (KeyValuePair<int, int> sd in spanDict.OrderByDescending(key=> key.Value)) { i++; //Debug.WriteLine("pic no / dist: {0}/{1}", sd.Key, sd.Value); if (sd.Value < breakVal) break; string piPath = ""; DateTime dOriginal = nullDate; if (_il.DirPathPos(ref piPath, sd.Key)){ ExifRead.ExifODate(out dOriginal, piPath); } _rangeDict.Add(sd.Key, dOriginal); } } }