Skip to content

Commit 7c1a894

Browse files
committed
refactor: do not change original image aspect ratio and do not upscale image in SWIPE mode
Signed-off-by: leo <longshuang@msn.cn>
1 parent bcefb77 commit 7c1a894

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

src/Views/ImageContainer.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,12 @@ public override void Render(DrawingContext context)
137137
var w = Bounds.Width;
138138
var h = Bounds.Height;
139139
var x = w * alpha;
140-
var left = OldImage;
141-
if (left != null && alpha > 0)
142-
{
143-
var src = new Rect(0, 0, left.Size.Width * alpha, left.Size.Height);
144-
var dst = new Rect(0, 0, x, h);
145-
context.DrawImage(left, src, dst);
146-
}
147140

148-
var right = NewImage;
149-
if (right != null && alpha < 1)
150-
{
151-
var src = new Rect(right.Size.Width * alpha, 0, right.Size.Width * (1 - alpha), right.Size.Height);
152-
var dst = new Rect(x, 0, w - x, h);
153-
context.DrawImage(right, src, dst);
154-
}
141+
if (OldImage is { } left && alpha > 0)
142+
RenderSingleSide(context, left, new Rect(0, 0, x, h));
143+
144+
if (NewImage is { } right && alpha < 1)
145+
RenderSingleSide(context, right, new Rect(x, 0, w - x, h));
155146

156147
context.DrawLine(new Pen(Brushes.DarkGreen, 2), new Point(x, 0), new Point(x, Bounds.Height));
157148
}
@@ -233,6 +224,25 @@ private Size GetDesiredSize(Size img, Size available)
233224
return new Size(scale * img.Width, scale * img.Height);
234225
}
235226

227+
private void RenderSingleSide(DrawingContext context, Bitmap img, Rect clip)
228+
{
229+
var w = Bounds.Width;
230+
var h = Bounds.Height;
231+
232+
var imgW = img.Size.Width;
233+
var imgH = img.Size.Height;
234+
var scale = Math.Min(1, Math.Min(w / imgW, h / imgH));
235+
236+
var scaledW = img.Size.Width * scale;
237+
var scaledH = img.Size.Height * scale;
238+
239+
var src = new Rect(0, 0, imgW, imgH);
240+
var dst = new Rect((w - scaledW) * 0.5, (h - scaledH) * 0.5, scaledW, scaledH);
241+
242+
using (context.PushClip(clip))
243+
context.DrawImage(img, src, dst);
244+
}
245+
236246
private bool _pressedOnSlider = false;
237247
private bool _lastInSlider = false;
238248
}

0 commit comments

Comments
 (0)