Skip to content

Commit 79cf23e

Browse files
authored
Merge pull request #2 from RicardoTM05/main
Added JPG Quality option.
2 parents 776f903 + 4ad8864 commit 79cf23e

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

FinalShot/FinalShot.cs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Drawing;
33
using System.Drawing.Imaging;
44
using System.IO;
5+
using System.Linq;
56
using System.Runtime.InteropServices;
67
using System.Windows.Forms;
78
using Rainmeter;
@@ -30,9 +31,10 @@ public static void Log(string message)
3031
internal class Measure
3132
{
3233
private string savePath;
33-
private string finishAction = "";
34-
private Rainmeter.API api;
35-
public static bool showCursor;
34+
private string finishAction = "";
35+
private Rainmeter.API api;
36+
public static bool showCursor;
37+
public static int jpegQuality;
3638
private int predefX;
3739
private int predefY;
3840
private int predefWidth;
@@ -70,6 +72,7 @@ public void Reload(Rainmeter.API api, ref double maxValue)
7072
savePath = api.ReadString("SavePath", "");
7173
finishAction = api.ReadString("ScreenshotFinishAction", "");
7274
showCursor = api.ReadInt("ShowCursor", 0) > 0;
75+
jpegQuality = api.ReadInt("JpgQuality", 70);
7376
predefX = api.ReadInt("PredefX", 0);
7477
predefY = api.ReadInt("PredefY", 0);
7578
predefWidth = api.ReadInt("PredefWidth", 0);
@@ -190,13 +193,42 @@ private void SaveImage(Bitmap bitmap)
190193
{
191194
try
192195
{
193-
bitmap.Save(savePath, ImageFormat.Png);
194-
Logger.Log("Image saved to: " + savePath);
196+
ImageFormat format = GetImageFormat(savePath);
197+
198+
if (format.Equals(ImageFormat.Jpeg))
199+
{
200+
var encoder = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
201+
var encoderParams = new EncoderParameters(1);
202+
encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, Measure.jpegQuality);
203+
bitmap.Save(savePath, encoder, encoderParams);
204+
}
205+
else
206+
{
207+
bitmap.Save(savePath, format);
208+
}
195209
}
196210
catch (Exception ex)
197211
{
198-
api.Log(API.LogType.Error, "Error saving image: " + ex.Message);
199-
Logger.Log("Error saving image: " + ex.Message);
212+
Logger.Log("Error saving custom screenshot: " + ex.Message);
213+
}
214+
}
215+
216+
private ImageFormat GetImageFormat(string path)
217+
{
218+
string ext = Path.GetExtension(path).ToLowerInvariant();
219+
switch (ext)
220+
{
221+
case ".jpg":
222+
case ".jpeg":
223+
return ImageFormat.Jpeg;
224+
case ".png":
225+
return ImageFormat.Png;
226+
case ".bmp":
227+
return ImageFormat.Bmp;
228+
case ".tiff":
229+
return ImageFormat.Tiff;
230+
default:
231+
return ImageFormat.Png; // fallback
200232
}
201233
}
202234

@@ -237,7 +269,6 @@ public CustomScreenshotForm(string savePath, Action finishAction)
237269
this.Cursor = Cursors.Cross;
238270
this.StartPosition = FormStartPosition.Manual;
239271
this.Location = SystemInformation.VirtualScreen.Location;
240-
241272
this.MouseDown += OnMouseDown;
242273
this.MouseMove += OnMouseMove;
243274
this.MouseUp += OnMouseUp;
@@ -379,15 +410,24 @@ private void SaveImage(Bitmap bitmap)
379410
try
380411
{
381412
ImageFormat format = GetImageFormat(savePath);
382-
bitmap.Save(savePath, format);
383-
Logger.Log("Custom screenshot saved to: " + savePath);
413+
414+
if (format.Equals(ImageFormat.Jpeg))
415+
{
416+
var encoder = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
417+
var encoderParams = new EncoderParameters(1);
418+
encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, Measure.jpegQuality);
419+
bitmap.Save(savePath, encoder, encoderParams);
420+
}
421+
else
422+
{
423+
bitmap.Save(savePath, format);
424+
}
384425
}
385426
catch (Exception ex)
386427
{
387428
Logger.Log("Error saving custom screenshot: " + ex.Message);
388429
}
389430
}
390-
391431
private ImageFormat GetImageFormat(string path)
392432
{
393433
string ext = Path.GetExtension(path).ToLowerInvariant();

0 commit comments

Comments
 (0)