Skip to content

Commit

Permalink
Доблено редактирование и созранение даты фотографии.
Browse files Browse the repository at this point in the history
  • Loading branch information
glebliutsko committed Nov 2, 2021
1 parent 907122a commit c74b1a8
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 48 deletions.
33 changes: 33 additions & 0 deletions PhotoDateEditor/Commands/RealyCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Windows.Input;

namespace PhotoDateEditor.Commands
{
class RealyCommand : ICommand
{
private Action<object> _execute;
private Func<object, bool> _canExecute;

public RealyCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute(parameter);
}

public void Execute(object parameter)
{
_execute(parameter);
}
}
}
49 changes: 30 additions & 19 deletions PhotoDateEditor/Image/ImageMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,45 @@

namespace PhotoDateEditor.Image
{
class ImageMetadata
public class ImageMetadata
{
public ImageFile Metadata { get; private set; }
public string PathToFile { get; private set; }
private ImageFile _fileMetadata;
public string PathToFile { get; }

public string FileName
public DateTime? CreateDateTime { get; set; }
public DateTime? ModifyImageDateTime { get; set; }
public DateTime? ModifyFileDateTime { get; set; }

public ImageMetadata(string pathToFile)
{
get
{
return Path.GetFileName(PathToFile);
}
PathToFile = pathToFile;
_fileMetadata = ImageFile.FromFile(PathToFile);

ReadMetadata();
}

public DateTime CreateDateTime
private void ReadMetadata()
{
get
{
return Metadata.Properties.Get<ExifDateTime>(ExifTag.DateTimeOriginal);
}
CreateDateTime = _fileMetadata.Properties.Get<ExifDateTime>(ExifTag.DateTimeOriginal)?.Value;
ModifyImageDateTime = _fileMetadata.Properties.Get<ExifDateTime>(ExifTag.DateTimeDigitized)?.Value;
ModifyFileDateTime = _fileMetadata.Properties.Get<ExifDateTime>(ExifTag.DateTime)?.Value;
}


public ImageMetadata(string pathToFile)
private void SaveProperty(ExifTag tag, DateTime? value)
{
Metadata = ImageFile.FromFile(pathToFile);
PathToFile = pathToFile;
if (value == null)
_fileMetadata.Properties.Remove(tag);
else
_fileMetadata.Properties.Set(tag, (DateTime)value);
}


public void SaveFile()
{
SaveProperty(ExifTag.DateTimeOriginal, CreateDateTime);
SaveProperty(ExifTag.DateTimeDigitized, ModifyImageDateTime);
SaveProperty(ExifTag.DateTime, ModifyFileDateTime);

_fileMetadata.Save(PathToFile);
}
}
}
}
83 changes: 83 additions & 0 deletions PhotoDateEditor/Image/ImageMetadataModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using PhotoDateEditor.Utils;
using System;
using System.IO;

namespace PhotoDateEditor.Image
{
public class ImageMetadataModel : AbstractPropertyChangedClass
{
private bool _isModified;
public bool IsModified
{
get => _isModified;
set
{
_isModified = value;
OnPropertyChanged(nameof(IsModified));
OnPropertyChanged(nameof(ViewFileName));
}
}

private ImageMetadata _metadata;

public string PathToFile { get => _metadata.PathToFile; }
public string FileName { get => Path.GetFileName(PathToFile); }
public string ViewFileName
{
get
{
if (IsModified)
return $"*{FileName}";
else
return FileName;
}
}

public DateTime? CreateDateTime
{
get => _metadata.CreateDateTime;
set
{
_metadata.CreateDateTime = value;
IsModified = true;
OnPropertyChanged(nameof(CreateDateTime));
}
}
public DateTime? ModifyImageDateTime
{
get => _metadata.ModifyImageDateTime;
set
{
_metadata.ModifyImageDateTime = value;
IsModified = true;
OnPropertyChanged(nameof(ModifyImageDateTime));
}
}
public DateTime? ModifyFileDateTime
{
get => _metadata.ModifyFileDateTime;
set
{
_metadata.ModifyFileDateTime = value;
IsModified = true;
OnPropertyChanged(nameof(ModifyFileDateTime));
}
}

public ImageMetadataModel(ImageMetadata metadata)
{
_metadata = metadata;
}

public ImageMetadataModel(string filename)
{
_metadata = new ImageMetadata(filename);
}

public void SaveFile()
{
IsModified = false;
_metadata.SaveFile();
}
}
}
18 changes: 18 additions & 0 deletions PhotoDateEditor/Utils/AbstractPropertyChangedClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhotoDateEditor.Utils
{
public abstract class AbstractPropertyChangedClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
36 changes: 36 additions & 0 deletions PhotoDateEditor/Utils/PathToImageConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace PhotoDateEditor.Utils
{
class PathToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BitmapImage result = null;
var path = (string)value;

if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
var image = new BitmapImage();

image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path); ;
image.EndInit();

result = image;
}

return result;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Two way conversion is not supported!");
}
}
}
18 changes: 18 additions & 0 deletions PhotoDateEditor/ViewModels/IImagesViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using PhotoDateEditor.Image;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PhotoDateEditor.ViewModels
{
public interface IImagesViewModel
{
public ObservableCollection<ImageMetadataModel> Images { get; set; }
public ImageMetadataModel SelectImage { get; set; }
public ICommand AddImagesCommand { get; }
}
}
63 changes: 55 additions & 8 deletions PhotoDateEditor/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,65 @@
using System;
using System.Collections.Generic;
using PhotoDateEditor.Commands;
using PhotoDateEditor.Image;
using PhotoDateEditor.Utils;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PhotoDateEditor.ViewModels
{
class MainWindowViewModel : INotifyPropertyChanged
class MainWindowViewModel : AbstractPropertyChangedClass, IImagesViewModel
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
private ObservableCollection<ImageMetadataModel> _images = new();
public ObservableCollection<ImageMetadataModel> Images
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
get => _images;
set
{
_images = value;
OnPropertyChanged(nameof(Images));
}
}

private ImageMetadataModel _selectImage;
public ImageMetadataModel SelectImage
{
get => _selectImage;
set
{
_selectImage = value;
OnPropertyChanged(nameof(SelectImage));
}
}

private ICommand _addImagesCommand;
public ICommand AddImagesCommand
{
get
{
return _addImagesCommand ??
(_addImagesCommand = new RealyCommand(obj =>
{
string[] fileNames = (string[])obj;

ImageMetadataModel[] images = fileNames.Select(x => new ImageMetadataModel(x)).ToArray();
foreach (var i in images)
Images.Add(i);
}));
}
}

private ICommand _saveCommand;
public ICommand SaveCommand
{
get
{
return _saveCommand ??
(_saveCommand = new RealyCommand(obj =>
{
SelectImage.SaveFile();
}, obj => SelectImage != null));
}
}
}
}
Loading

0 comments on commit c74b1a8

Please sign in to comment.