Skip to content

Commit c3c7d32

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

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/Views/ImageContainer.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ public override void Render(DrawingContext context)
286286
{
287287
base.Render(context);
288288

289-
var rect = new Rect(0, 0, Bounds.Width, Bounds.Height);
290289
var alpha = Alpha;
291290
var left = OldImage;
292291
var right = NewImage;
@@ -295,32 +294,27 @@ public override void Render(DrawingContext context)
295294

296295
if (drawLeft && drawRight)
297296
{
298-
using (var rt = new RenderTargetBitmap(right.PixelSize, right.Dpi))
297+
using (var rt = new RenderTargetBitmap(new PixelSize((int)Bounds.Width, (int)Bounds.Height), right.Dpi))
299298
{
300-
var rtRect = new Rect(rt.Size);
301299
using (var dc = rt.CreateDrawingContext())
302300
{
303301
using (dc.PushRenderOptions(RO_SRC))
304-
using (dc.PushOpacity(1 - alpha))
305-
dc.DrawImage(left, rtRect);
302+
RenderSingleSide(dc, left, rt.Size.Width, rt.Size.Height, 1 - alpha);
306303

307304
using (dc.PushRenderOptions(RO_DST))
308-
using (dc.PushOpacity(alpha))
309-
dc.DrawImage(right, rtRect);
305+
RenderSingleSide(dc, right, rt.Size.Width, rt.Size.Height, alpha);
310306
}
311307

312-
context.DrawImage(rt, rtRect, rect);
308+
context.DrawImage(rt, new Rect(0, 0, Bounds.Width, Bounds.Height));
313309
}
314310
}
315311
else if (drawLeft)
316312
{
317-
using (context.PushOpacity(1 - alpha))
318-
context.DrawImage(left, rect);
313+
RenderSingleSide(context, left, Bounds.Width, Bounds.Height, 1 - alpha);
319314
}
320315
else if (drawRight)
321316
{
322-
using (context.PushOpacity(alpha))
323-
context.DrawImage(right, rect);
317+
RenderSingleSide(context, right, Bounds.Width, Bounds.Height, alpha);
324318
}
325319
}
326320

@@ -348,6 +342,22 @@ private Size GetDesiredSize(Size img, Size available)
348342
return new Size(scale * img.Width, scale * img.Height);
349343
}
350344

345+
private void RenderSingleSide(DrawingContext context, Bitmap img, double w, double h, double alpha)
346+
{
347+
var imgW = img.Size.Width;
348+
var imgH = img.Size.Height;
349+
var scale = Math.Min(1, Math.Min(w / imgW, h / imgH));
350+
351+
var scaledW = img.Size.Width * scale;
352+
var scaledH = img.Size.Height * scale;
353+
354+
var src = new Rect(0, 0, imgW, imgH);
355+
var dst = new Rect((w - scaledW) * 0.5, (h - scaledH) * 0.5, scaledW, scaledH);
356+
357+
using (context.PushOpacity(alpha))
358+
context.DrawImage(img, src, dst);
359+
}
360+
351361
private static readonly RenderOptions RO_SRC = new RenderOptions() { BitmapBlendingMode = BitmapBlendingMode.Source, BitmapInterpolationMode = BitmapInterpolationMode.HighQuality };
352362
private static readonly RenderOptions RO_DST = new RenderOptions() { BitmapBlendingMode = BitmapBlendingMode.Plus, BitmapInterpolationMode = BitmapInterpolationMode.HighQuality };
353363
}

0 commit comments

Comments
 (0)