From a666ac81dbf39f713a5a340135924120d6665fe1 Mon Sep 17 00:00:00 2001 From: RicardoTM05 <168048518+RicardoTM05@users.noreply.github.com> Date: Sun, 20 Apr 2025 03:49:42 -0600 Subject: [PATCH 1/3] Added JPG Quality option. --- FinalShot/FinalShot.cs | 56 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/FinalShot/FinalShot.cs b/FinalShot/FinalShot.cs index 3c51c8a..e08986f 100644 --- a/FinalShot/FinalShot.cs +++ b/FinalShot/FinalShot.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; using Rainmeter; @@ -34,6 +35,7 @@ internal class Measure private string finishAction = ""; // Action to execute after screenshot is taken private Rainmeter.API api; // Rainmeter API reference public static bool showCursor; // Include cursor in the screenshot + public static int jpegQuality; // JPEG quality for saving images // Predefined coordinates for -ps command. private int predefX; @@ -77,7 +79,7 @@ public void Reload(Rainmeter.API api, ref double maxValue) savePath = api.ReadString("SavePath", ""); finishAction = api.ReadString("ScreenshotFinishAction", ""); showCursor = api.ReadInt("ShowCursor", 0) > 0; - + jpegQuality = api.ReadInt("JpgQuality", 70); predefX = api.ReadInt("PredefX", 0); predefY = api.ReadInt("PredefY", 0); predefWidth = api.ReadInt("PredefWidth", 0); @@ -204,13 +206,42 @@ private void SaveImage(Bitmap bitmap) { try { - bitmap.Save(savePath, ImageFormat.Png); - Logger.Log("Image saved to: " + savePath); + ImageFormat format = GetImageFormat(savePath); + + if (format.Equals(ImageFormat.Jpeg)) + { + var encoder = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid); + var encoderParams = new EncoderParameters(1); + encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, Measure.jpegQuality); + bitmap.Save(savePath, encoder, encoderParams); + } + else + { + bitmap.Save(savePath, format); + } } catch (Exception ex) { - api.Log(API.LogType.Error, "Error saving image: " + ex.Message); - Logger.Log("Error saving image: " + ex.Message); + Logger.Log("Error saving custom screenshot: " + ex.Message); + } + } + + private ImageFormat GetImageFormat(string path) + { + string ext = Path.GetExtension(path).ToLowerInvariant(); + switch (ext) + { + case ".jpg": + case ".jpeg": + return ImageFormat.Jpeg; + case ".png": + return ImageFormat.Png; + case ".bmp": + return ImageFormat.Bmp; + case ".tiff": + return ImageFormat.Tiff; + default: + return ImageFormat.Png; // fallback } } @@ -412,15 +443,24 @@ private void SaveImage(Bitmap bitmap) try { ImageFormat format = GetImageFormat(savePath); - bitmap.Save(savePath, format); - Logger.Log("Custom screenshot saved to: " + savePath); + + if (format.Equals(ImageFormat.Jpeg)) + { + var encoder = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid); + var encoderParams = new EncoderParameters(1); + encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, Measure.jpegQuality); + bitmap.Save(savePath, encoder, encoderParams); + } + else + { + bitmap.Save(savePath, format); + } } catch (Exception ex) { Logger.Log("Error saving custom screenshot: " + ex.Message); } } - private ImageFormat GetImageFormat(string path) { string ext = Path.GetExtension(path).ToLowerInvariant(); From cb779874a643827d426292e42c8d7f25ae01a392 Mon Sep 17 00:00:00 2001 From: NSTechBytes Date: Sun, 20 Apr 2025 17:15:06 +0500 Subject: [PATCH 2/3] removed already defination variables. --- FinalShot/FinalShot.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/FinalShot/FinalShot.cs b/FinalShot/FinalShot.cs index ffc7533..cf5b932 100644 --- a/FinalShot/FinalShot.cs +++ b/FinalShot/FinalShot.cs @@ -36,8 +36,6 @@ internal class Measure public static bool showCursor; public static int jpegQuality; private string finishAction = ""; - private Rainmeter.API api; - public static bool showCursor; private int predefX; private int predefY; private int predefWidth; From 4ad886411899f1c61ed91406ea13c7de88fc1e84 Mon Sep 17 00:00:00 2001 From: NSTechBytes Date: Sun, 20 Apr 2025 17:19:21 +0500 Subject: [PATCH 3/3] remove duplicate definition of FinishAction --- FinalShot/FinalShot.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/FinalShot/FinalShot.cs b/FinalShot/FinalShot.cs index cf5b932..c312ca6 100644 --- a/FinalShot/FinalShot.cs +++ b/FinalShot/FinalShot.cs @@ -35,7 +35,6 @@ internal class Measure private Rainmeter.API api; public static bool showCursor; public static int jpegQuality; - private string finishAction = ""; private int predefX; private int predefY; private int predefWidth; @@ -270,7 +269,6 @@ public CustomScreenshotForm(string savePath, Action finishAction) this.Cursor = Cursors.Cross; this.StartPosition = FormStartPosition.Manual; this.Location = SystemInformation.VirtualScreen.Location; - this.MouseDown += OnMouseDown; this.MouseMove += OnMouseMove; this.MouseUp += OnMouseUp;