public override BitmapBase Apply(Tank tank, BitmapBase layer) { var pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height); if (ShowPixelBorders || PositionByPixels || (SizeByPixels && SizeMode2 != SizeMode2.NoChange && SizeMode2 != SizeMode2.ByPercentage)) pixels = layer.PreciseSize(PixelAlphaThreshold); bool emptyPixels = pixels.Width <= 0 || pixels.Height <= 0; if (emptyPixels) pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height); double scaleWidth, scaleHeight; int sourceWidth = SizeByPixels ? pixels.Width : layer.Width; int sourceHeight = SizeByPixels ? pixels.Height : layer.Height; switch (SizeMode2) { case SizeMode2.NoChange: scaleWidth = scaleHeight = 1; break; case SizeMode2.ByPercentage: scaleWidth = scaleHeight = Percentage / 100.0; break; case SizeMode2.BySizeWidthOnly: scaleWidth = scaleHeight = Width / (double) sourceWidth; break; case SizeMode2.BySizeHeightOnly: scaleWidth = scaleHeight = Height / (double) sourceHeight; break; case SizeMode2.BySizeFit: scaleWidth = scaleHeight = Math.Min(Width / (double) sourceWidth, Height / (double) sourceHeight); break; case SizeMode2.BySizeStretch: scaleWidth = Width / (double) sourceWidth; scaleHeight = Height / (double) sourceHeight; break; default: throw new Exception("7924688"); } if (GrowShrinkMode == GrowShrinkMode.GrowOnly) { scaleWidth = Math.Max(1.0, scaleWidth); scaleHeight = Math.Max(1.0, scaleHeight); } else if (GrowShrinkMode == GrowShrinkMode.ShrinkOnly) { scaleWidth = Math.Min(1.0, scaleWidth); scaleHeight = Math.Min(1.0, scaleHeight); } var anchor = (AnchorRaw) Anchor; int anchorWidth = (int) Math.Ceiling((PositionByPixels ? pixels.Width : layer.Width) * scaleWidth); int anchorHeight = (int) Math.Ceiling((PositionByPixels ? pixels.Height : layer.Height) * scaleHeight); // Location of the top left corner of the anchored rectangle int tgtX = X - (anchor.HasFlag(AnchorRaw.Right) ? anchorWidth - 1 : anchor.HasFlag(AnchorRaw.Center) ? (anchorWidth - 1) / 2 : 0); int tgtY = Y - (anchor.HasFlag(AnchorRaw.Bottom) ? anchorHeight - 1 : anchor.HasFlag(AnchorRaw.Mid) ? (anchorHeight - 1) / 2 : 0); // Location of the top left corner of the whole scaled layer image double x = tgtX - (PositionByPixels ? pixels.Left * scaleWidth : 0); double y = tgtY - (PositionByPixels ? pixels.Top * scaleHeight : 0); var src = layer.ToBitmapGdi(); if (ShowLayerBorders || ShowPixelBorders) using (var dc = System.Drawing.Graphics.FromImage(src.Bitmap)) { if (ShowLayerBorders) dc.DrawRectangle(System.Drawing.Pens.Aqua, 0, 0, layer.Width - 1, layer.Height - 1); if (ShowPixelBorders && !emptyPixels) dc.DrawRectangle(System.Drawing.Pens.Red, pixels.Left, pixels.Top, pixels.Width - 1, pixels.Height - 1); } #if true // Using GDI: sharp-ish downscaling, but imprecise boundaries var result = new BitmapGdi(Math.Max(layer.Width, Layer.ParentStyle.IconWidth), Math.Max(layer.Height, Layer.ParentStyle.IconHeight)); using (var dc = Graphics.FromImage(result.Bitmap)) { dc.InterpolationMode = InterpolationMode.HighQualityBicubic; dc.DrawImage(src.Bitmap, (float) x, (float) y, (float) (src.Width * scaleWidth), (float) (src.Height * scaleHeight)); if (ShowAnchor) using (var pen = new Pen(Color.FromArgb(120, Color.Yellow), 0)) { dc.DrawLine(pen, X - 1, Y, X + 1, Y); dc.DrawLine(pen, X, Y - 1, X, Y + 1); } } #else // Using WPF: precise boundaries but rather blurry downscaling var result = Ut.NewBitmapWpf(dc => { var img = src.ToBitmapWpf().UnderlyingImage; var group = new System.Windows.Media.DrawingGroup(); System.Windows.Media.RenderOptions.SetBitmapScalingMode(group, System.Windows.Media.BitmapScalingMode.Fant); group.Children.Add(new System.Windows.Media.ImageDrawing(img, new System.Windows.Rect(x, y, src.Width * scaleWidth, src.Height * scaleHeight))); dc.DrawDrawing(group); if (ShowTargetPosition) { var pen = new System.Windows.Media.Pen(new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(200, 255, 255, 0)), 1); dc.DrawLine(pen, new System.Windows.Point(X - 1 + 0.5, Y + 0.5), new System.Windows.Point(X + 1 + 0.5, Y + 0.5)); dc.DrawLine(pen, new System.Windows.Point(X + 0.5, Y - 1 + 0.5), new System.Windows.Point(X + 0.5, Y + 1 + 0.5)); } }, Math.Max(layer.Width, Layer.Style.IconWidth), Math.Max(layer.Height, Layer.Style.IconHeight)); #endif GC.KeepAlive(src); return result.ToBitmapRam(); }