Skip to content

Commit a666ac8

Browse files
authored
Added JPG Quality option.
1 parent 8a182cf commit a666ac8

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

FinalShot/FinalShot.cs

Lines changed: 48 additions & 8 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;
@@ -34,6 +35,7 @@ internal class Measure
3435
private string finishAction = ""; // Action to execute after screenshot is taken
3536
private Rainmeter.API api; // Rainmeter API reference
3637
public static bool showCursor; // Include cursor in the screenshot
38+
public static int jpegQuality; // JPEG quality for saving images
3739

3840
// Predefined coordinates for -ps command.
3941
private int predefX;
@@ -77,7 +79,7 @@ public void Reload(Rainmeter.API api, ref double maxValue)
7779
savePath = api.ReadString("SavePath", "");
7880
finishAction = api.ReadString("ScreenshotFinishAction", "");
7981
showCursor = api.ReadInt("ShowCursor", 0) > 0;
80-
82+
jpegQuality = api.ReadInt("JpgQuality", 70);
8183
predefX = api.ReadInt("PredefX", 0);
8284
predefY = api.ReadInt("PredefY", 0);
8385
predefWidth = api.ReadInt("PredefWidth", 0);
@@ -204,13 +206,42 @@ private void SaveImage(Bitmap bitmap)
204206
{
205207
try
206208
{
207-
bitmap.Save(savePath, ImageFormat.Png);
208-
Logger.Log("Image saved to: " + savePath);
209+
ImageFormat format = GetImageFormat(savePath);
210+
211+
if (format.Equals(ImageFormat.Jpeg))
212+
{
213+
var encoder = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
214+
var encoderParams = new EncoderParameters(1);
215+
encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, Measure.jpegQuality);
216+
bitmap.Save(savePath, encoder, encoderParams);
217+
}
218+
else
219+
{
220+
bitmap.Save(savePath, format);
221+
}
209222
}
210223
catch (Exception ex)
211224
{
212-
api.Log(API.LogType.Error, "Error saving image: " + ex.Message);
213-
Logger.Log("Error saving image: " + ex.Message);
225+
Logger.Log("Error saving custom screenshot: " + ex.Message);
226+
}
227+
}
228+
229+
private ImageFormat GetImageFormat(string path)
230+
{
231+
string ext = Path.GetExtension(path).ToLowerInvariant();
232+
switch (ext)
233+
{
234+
case ".jpg":
235+
case ".jpeg":
236+
return ImageFormat.Jpeg;
237+
case ".png":
238+
return ImageFormat.Png;
239+
case ".bmp":
240+
return ImageFormat.Bmp;
241+
case ".tiff":
242+
return ImageFormat.Tiff;
243+
default:
244+
return ImageFormat.Png; // fallback
214245
}
215246
}
216247

@@ -412,15 +443,24 @@ private void SaveImage(Bitmap bitmap)
412443
try
413444
{
414445
ImageFormat format = GetImageFormat(savePath);
415-
bitmap.Save(savePath, format);
416-
Logger.Log("Custom screenshot saved to: " + savePath);
446+
447+
if (format.Equals(ImageFormat.Jpeg))
448+
{
449+
var encoder = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
450+
var encoderParams = new EncoderParameters(1);
451+
encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, Measure.jpegQuality);
452+
bitmap.Save(savePath, encoder, encoderParams);
453+
}
454+
else
455+
{
456+
bitmap.Save(savePath, format);
457+
}
417458
}
418459
catch (Exception ex)
419460
{
420461
Logger.Log("Error saving custom screenshot: " + ex.Message);
421462
}
422463
}
423-
424464
private ImageFormat GetImageFormat(string path)
425465
{
426466
string ext = Path.GetExtension(path).ToLowerInvariant();

0 commit comments

Comments
 (0)