|
2 | 2 | using System.Drawing; |
3 | 3 | using System.Drawing.Imaging; |
4 | 4 | using System.IO; |
| 5 | +using System.Linq; |
5 | 6 | using System.Runtime.InteropServices; |
6 | 7 | using System.Windows.Forms; |
7 | 8 | using Rainmeter; |
@@ -30,9 +31,10 @@ public static void Log(string message) |
30 | 31 | internal class Measure |
31 | 32 | { |
32 | 33 | 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; |
36 | 38 | private int predefX; |
37 | 39 | private int predefY; |
38 | 40 | private int predefWidth; |
@@ -70,6 +72,7 @@ public void Reload(Rainmeter.API api, ref double maxValue) |
70 | 72 | savePath = api.ReadString("SavePath", ""); |
71 | 73 | finishAction = api.ReadString("ScreenshotFinishAction", ""); |
72 | 74 | showCursor = api.ReadInt("ShowCursor", 0) > 0; |
| 75 | + jpegQuality = api.ReadInt("JpgQuality", 70); |
73 | 76 | predefX = api.ReadInt("PredefX", 0); |
74 | 77 | predefY = api.ReadInt("PredefY", 0); |
75 | 78 | predefWidth = api.ReadInt("PredefWidth", 0); |
@@ -190,13 +193,42 @@ private void SaveImage(Bitmap bitmap) |
190 | 193 | { |
191 | 194 | try |
192 | 195 | { |
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 | + } |
195 | 209 | } |
196 | 210 | catch (Exception ex) |
197 | 211 | { |
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 |
200 | 232 | } |
201 | 233 | } |
202 | 234 |
|
@@ -237,7 +269,6 @@ public CustomScreenshotForm(string savePath, Action finishAction) |
237 | 269 | this.Cursor = Cursors.Cross; |
238 | 270 | this.StartPosition = FormStartPosition.Manual; |
239 | 271 | this.Location = SystemInformation.VirtualScreen.Location; |
240 | | - |
241 | 272 | this.MouseDown += OnMouseDown; |
242 | 273 | this.MouseMove += OnMouseMove; |
243 | 274 | this.MouseUp += OnMouseUp; |
@@ -379,15 +410,24 @@ private void SaveImage(Bitmap bitmap) |
379 | 410 | try |
380 | 411 | { |
381 | 412 | 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 | + } |
384 | 425 | } |
385 | 426 | catch (Exception ex) |
386 | 427 | { |
387 | 428 | Logger.Log("Error saving custom screenshot: " + ex.Message); |
388 | 429 | } |
389 | 430 | } |
390 | | - |
391 | 431 | private ImageFormat GetImageFormat(string path) |
392 | 432 | { |
393 | 433 | string ext = Path.GetExtension(path).ToLowerInvariant(); |
|
0 commit comments