using System;
using System.Drawing;
using System.IO;
using FFMpegCore.Enums;
using FFMpegCore.Helpers;
namespace FFMpegCore
{
public class ImageInfo
{
private FileInfo _file;
///
/// Create a image information object from a target path.
///
/// Image file information.
public ImageInfo(FileInfo fileInfo)
{
if (!fileInfo.Extension.ToLowerInvariant().EndsWith(FileExtension.Png))
{
throw new Exception("Image joining currently suppors only .png file types");
}
fileInfo.Refresh();
Size = fileInfo.Length / (1024 * 1024);
using (var image = Image.FromFile(fileInfo.FullName))
{
Width = image.Width;
Height = image.Height;
var cd = FFProbeHelper.Gcd(Width, Height);
Ratio = $"{Width / cd}:{Height / cd}";
}
if (!fileInfo.Exists)
throw new ArgumentException($"Input file {fileInfo.FullName} does not exist!");
_file = fileInfo;
}
///
/// Create a image information object from a target path.
///
/// Path to image.
public ImageInfo(string path) : this(new FileInfo(path)) { }
///
/// Aspect ratio.
///
public string Ratio { get; internal set; }
///
/// Height of the image file.
///
public int Height { get; internal set; }
///
/// Width of the image file.
///
public int Width { get; internal set; }
///
/// Image file size in MegaBytes (MB).
///
public double Size { get; internal set; }
///
/// Gets the name of the file.
///
public string Name => _file.Name;
///
/// Gets the full path of the file.
///
public string FullName => _file.FullName;
///
/// Gets the file extension.
///
public string Extension => _file.Extension;
///
/// Gets a flag indicating if the file is read-only.
///
public bool IsReadOnly => _file.IsReadOnly;
///
/// Gets a flag indicating if the file exists (no cache, per call verification).
///
public bool Exists => File.Exists(FullName);
///
/// Gets the creation date.
///
public DateTime CreationTime => _file.CreationTime;
///
/// Gets the parent directory information.
///
public DirectoryInfo Directory => _file.Directory;
///
/// Create a image information object from a file information object.
///
/// Image file information.
///
public static ImageInfo FromFileInfo(FileInfo fileInfo)
{
return FromPath(fileInfo.FullName);
}
///
/// Create a image information object from a target path.
///
/// Path to image.
///
public static ImageInfo FromPath(string path)
{
return new ImageInfo(path);
}
///
/// Pretty prints the image information.
///
///
public override string ToString()
{
return "Image Path : " + FullName + Environment.NewLine +
"Image Root : " + Directory.FullName + Environment.NewLine +
"Image Name: " + Name + Environment.NewLine +
"Image Extension : " + Extension + Environment.NewLine +
"Aspect Ratio : " + Ratio + Environment.NewLine +
"Resolution : " + Width + "x" + Height + Environment.NewLine +
"Size : " + Size + " MB";
}
///
/// Open a file stream.
///
/// Opens a file in a specified mode.
/// File stream of the image file.
public FileStream FileOpen(FileMode mode)
{
return _file.Open(mode);
}
///
/// Move file to a specific directory.
///
///
public void MoveTo(DirectoryInfo destination)
{
var newLocation = $"{destination.FullName}{Path.DirectorySeparatorChar}{Name}{Extension}";
_file.MoveTo(newLocation);
_file = new FileInfo(newLocation);
}
///
/// Delete the file.
///
public void Delete()
{
_file.Delete();
}
///
/// Converts image info to file info.
///
/// A new FileInfo instance.
public FileInfo ToFileInfo()
{
return new FileInfo(_file.FullName);
}
}
}