60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using System.Drawing.Imaging;
|
|
using Dalamud.Game.Command;
|
|
|
|
namespace Screenie;
|
|
|
|
internal class Command : IDisposable {
|
|
private Plugin Plugin { get; }
|
|
private const string CommandName = "/screenie";
|
|
|
|
internal Command(Plugin plugin) {
|
|
this.Plugin = plugin;
|
|
|
|
this.Plugin.CommandManager.AddHandler(CommandName, new CommandInfo(this.OnCommand));
|
|
}
|
|
|
|
public void Dispose() {
|
|
this.Plugin.CommandManager.RemoveHandler(CommandName);
|
|
}
|
|
|
|
private static ImageCodecInfo? GetEncoder(ImageFormat format) {
|
|
var codecs = ImageCodecInfo.GetImageEncoders();
|
|
return codecs.FirstOrDefault(codec => codec.FormatID == format.Guid);
|
|
}
|
|
|
|
|
|
private void OnCommand(string command, string arguments) {
|
|
using var bitmap = Photographer.Capture();
|
|
if (bitmap == null) {
|
|
return;
|
|
}
|
|
|
|
var encoder = GetEncoder(this.Plugin.Config.SaveFormat.ToImageFormat());
|
|
if (encoder == null) {
|
|
return;
|
|
}
|
|
|
|
var (param, ext) = this.Plugin.Config.SaveFormat switch {
|
|
Format.Jpg => (Encoder.Quality, "jpg"),
|
|
Format.Webp => (Encoder.Quality, "webp"),
|
|
Format.Png => (Encoder.Compression, "png"),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(this.Plugin.Config.SaveFormat), this.Plugin.Config.SaveFormat, null),
|
|
};
|
|
|
|
var parameters = new EncoderParameters(1) {
|
|
Param = [
|
|
new EncoderParameter(param, this.Plugin.Config.SaveFormatData),
|
|
],
|
|
};
|
|
|
|
Directory.CreateDirectory(this.Plugin.Config.SaveDirectory);
|
|
|
|
var path = Path.ChangeExtension(
|
|
Path.Join(this.Plugin.Config.SaveDirectory, "screenie"),
|
|
ext
|
|
);
|
|
using var stream = new FileStream(path, FileMode.Create, FileAccess.Write);
|
|
bitmap.Save(stream, encoder, parameters);
|
|
}
|
|
}
|