Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fbccc08

Browse files
committed
Improve sizing logic of redrawing images via indicator and enable enlargements.
1 parent 555fc42 commit fbccc08

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

‎src/SilDev/Drawing/ImageEx.cs‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// ==============================================
66
//
77
// Filename: ImageEx.cs
8-
// Version: 2023-12-20 00:28
8+
// Version: 2023-12-29 14:51
99
//
1010
// Copyright (c) 2023, Si13n7 Developments(tm)
1111
// All rights reserved.
@@ -21,7 +21,6 @@ namespace SilDev.Drawing
2121
using System.Drawing;
2222
using System.Drawing.Drawing2D;
2323
using System.Drawing.Imaging;
24-
using System.Linq;
2524
using System.Threading;
2625
using Properties;
2726
using static Crypto;
@@ -266,26 +265,42 @@ public static Image Redraw(this Image image, int width, int heigth, SmoothingMod
266265
/// Specifies the maximal size indicator, which determines when the image gets
267266
/// a new size.
268267
/// </param>
269-
public static Image Redraw(this Image image, SmoothingMode quality = SmoothingMode.HighQuality, int indicator = 1024)
268+
/// <param name="enlargement">
269+
/// <see langword="true"/> to allow enlargement of the image; otherwise,
270+
/// <see langword="false"/>.
271+
/// </param>
272+
public static Image Redraw(this Image image, SmoothingMode quality = SmoothingMode.HighQuality, int indicator = 1024, bool enlargement = true)
270273
{
271-
if (image is not { } img)
272-
return default;
274+
if (image is not { } img||indicator<1)
275+
return image;
273276
int[] size =
274277
{
275278
img.Width,
276279
img.Height
277280
};
278-
if (indicator > 0 && (indicator < size.First() || indicator < size.Last()))
279-
for (var i = 0; i < size.Length; i++)
281+
var max = Math.Max(size[0], size[1]);
282+
switch (enlargement)
283+
{
284+
case true when indicator != max:
285+
case false when indicator < max:
280286
{
281-
if (size[i] <= indicator)
282-
continue;
283-
var percent = (int)Math.Floor(100d / size[i] * indicator);
284-
size[i] = (int)(size[i] * (percent / 100d));
285-
size[i == 0 ? 1 : 0] = (int)(size[i == 0 ? 1 : 0] * (percent / 100d));
287+
if (size[0] == size[1])
288+
{
289+
size[0] = indicator;
290+
size[1] = indicator;
291+
}
292+
else
293+
{
294+
var iMax = size[0] == max ? 0 : 1;
295+
var iMin = iMax == 0 ? 1 : 0;
296+
var per = Math.Round(100d / max * indicator);
297+
size[iMax] = indicator;
298+
size[iMin] = (int)Math.Round(size[iMin] * (per / 100d));
299+
}
286300
break;
287301
}
288-
return img.Redraw(size.First(), size.Last(), quality);
302+
}
303+
return img.Redraw(size[0], size[1], quality);
289304
}
290305

291306
/// <summary>
@@ -299,8 +314,12 @@ public static Image Redraw(this Image image, SmoothingMode quality = SmoothingMo
299314
/// Specifies the maximal size indicator, which determines when the image gets
300315
/// a new size.
301316
/// </param>
302-
public static Image Redraw(this Image image, int indicator) =>
303-
image.Redraw(SmoothingMode.HighQuality, indicator);
317+
/// <param name="enlargement">
318+
/// <see langword="true"/> to allow enlargement of the image; otherwise,
319+
/// <see langword="false"/>.
320+
/// </param>
321+
public static Image Redraw(this Image image, int indicator, bool enlargement = true) =>
322+
image.Redraw(SmoothingMode.HighQuality, indicator, enlargement);
304323

305324
/// <summary>
306325
/// Blurs this image with the specified strength.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /