Skip to content

Commit

Permalink
Fix multiple image resize (#1673)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored Nov 17, 2024
1 parent 1b99871 commit 680ed28
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
22 changes: 11 additions & 11 deletions OneMore/Commands/Images/AdjustImagesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override async Task Execute(params object[] args)
}
}

if (elements != null && elements.Any())
if (elements.Any())
{
var updated = elements.Count == 1
// single selected image
Expand Down Expand Up @@ -137,17 +137,16 @@ private bool ResizeMany(List<XElement> elements, Page page)
using var image = wrapper.ReadImage();

var editor = dialog.GetImageEditor(image);

// when pasting an image onto the page, width or height can be zero
// OneNote ignores both if either is zero so we'll do the same...
var viewWidth = wrapper.Width;
if (viewWidth == 0)
{
viewWidth = image.Width;
}

if (editor.IsReady || (editor.AutoSize && wrapper.IsSetByUser))
{
// when pasting an image onto the page, width or height can be zero
// OneNote ignores both if either is zero so we'll do the same...
var viewWidth = wrapper.Width;
if (viewWidth == 0)
{
viewWidth = image.Width;
}

if (editor.Constraint == SC.All ||
(editor.Constraint == SC.OnlyShrink && viewWidth > editor.Size.Width) ||
(editor.Constraint == SC.OnlyEnlarge && viewWidth < editor.Size.Width))
Expand All @@ -157,7 +156,8 @@ private bool ResizeMany(List<XElement> elements, Page page)
}
else
{
logger.WriteLine($"skipped image, size=[{wrapper.Width} x {wrapper.Width}]");
logger.WriteLine("skipped image due to constraint: " +
$"viewWidth:{viewWidth} size=[{wrapper.Width} x {wrapper.Width}]");
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions OneMore/Commands/Images/AdjustImagesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,19 @@ public ImageEditor GetImageEditor(Image image)
}
else // %
{
var w = (int)(viewWidth * percentBox.Value / 100);
var h = (int)(viewHeight * percentBox.Value / 100);
if (singleImage)
{
var w = (int)(viewWidth * percentBox.Value / 100);
var h = (int)(viewHeight * percentBox.Value / 100);

if (w != image.Width || h != image.Height)
if (w != image.Width || h != image.Height)
{
editor.Size = new Size(w, h);
}
}
else
{
editor.Size = new Size(w, h);
editor.SizePercent = (float)percentBox.Value / 100;
}
}

Expand Down
35 changes: 32 additions & 3 deletions OneMore/Commands/Images/ImageEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public ImageEditor()
Saturation = float.MinValue;
Style = Stylization.None;
Size = Size.Empty;
SizePercent = float.MinValue;
Constraint = SizeConstraint.All;
PreserveQualityOnResize = true;

Expand All @@ -74,6 +75,8 @@ public ImageEditor()

public Size Size { get; set; }

public float SizePercent { get; set; }

public SizeConstraint Constraint { get; set; }

public Stylization Style { private get; set; }
Expand All @@ -85,6 +88,7 @@ public ImageEditor()
!Opacity.EstEquals(float.MinValue) ||
Quality != long.MinValue ||
!Saturation.EstEquals(float.MinValue) ||
!SizePercent.EstEquals(float.MinValue) ||
Size != Size.Empty ||
Style != Stylization.None;

Expand All @@ -102,6 +106,13 @@ public Image Apply(OneImage wrapper)
{
wrapper.SetAutoSize();
}
else if (SizePercent > float.MinValue)
{
wrapper.SetSize(
(int)(SizePercent * wrapper.Width),
(int)(SizePercent * wrapper.Height),
true);
}
else if (Size != Size.Empty)
{
wrapper.SetSize(Size.Width, Size.Height, true);
Expand Down Expand Up @@ -171,7 +182,8 @@ public Image Apply(Image edit)
}

// resize last, to attempt to preserve quality
if (Size != Size.Empty && !PreserveStorageSize)
if (!PreserveStorageSize &&
(SizePercent > float.MinValue || Size != Size.Empty))
{
trash.Push(edit);
edit = Resize(edit);
Expand Down Expand Up @@ -386,7 +398,24 @@ private Image Resize(Image original)
// Resize the physical storage model of the given image, creating a new Image instance
// with the specified width and height, optionally decreasing the quality level

var edit = new Bitmap(Size.Width, Size.Height);
int width;
int height;

// when resizing many, height could be zero so much adjust per image
if (SizePercent > float.MinValue)
{
width = (int)(original.Width * SizePercent);
height = (int)(original.Height * SizePercent);
}
else
{
width = Size.Width;
height = Size.Height == 0
? (int)(original.Height * (1.0 * Size.Width / original.Width))
: Size.Height;
}

var edit = new Bitmap(width, height);
edit.SetResolution(original.HorizontalResolution, original.VerticalResolution);

using var g = Graphics.FromImage(edit);
Expand All @@ -404,7 +433,7 @@ private Image Resize(Image original)
attributes.SetWrapMode(WrapMode.TileFlipXY);

g.DrawImage(original,
new Rectangle(0, 0, Size.Width, Size.Height),
new Rectangle(0, 0, width, height),
0, 0, original.Width, original.Height,
GraphicsUnit.Pixel,
attributes);
Expand Down

0 comments on commit 680ed28

Please sign in to comment.