23 if (bufferWidth <= 0 || bufferHeight <= 0 || adapterWidth <= 0 || adapterHeight <= 0)
26 float scale = Math.Min((
float)adapterWidth / bufferWidth, (
float)adapterHeight / bufferHeight);
27 float width = bufferWidth * scale;
28 float height = bufferHeight * scale;
33 (adapterWidth - width) / 2f,
34 (adapterHeight - height) / 2f,
43 public bool TryAdapterPxToScreenPx(PointF adapterPx, out PointF screenPx)
46 ?
new((adapterPx.X - DestinationRect.Left) / Scale, (adapterPx.Y - DestinationRect.Top) / Scale)
49 return Scale > 0 && DestinationRect.Contains(adapterPx.X, adapterPx.Y);
55 public Rectangle ScreenRectToAdapterRect(Rectangle screenRect)
59 (
int)Math.Floor(DestinationRect.Left + screenRect.Left * Scale),
60 (
int)Math.Floor(DestinationRect.Top + screenRect.Top * Scale),
61 (
int)Math.Ceiling(DestinationRect.Left + screenRect.Right * Scale),
62 (
int)Math.Ceiling(DestinationRect.Top + screenRect.Bottom * Scale));
64 internal static int ScaleDimension(
int dimension,
float renderScale)
66 double scaled = Math.Round(dimension * (
double)renderScale, MidpointRounding.AwayFromZero);
68 if (scaled >
int.MaxValue)
69 throw new ArgumentOutOfRangeException(
71 "Scaled dimension exceeds the supported integer range.");
73 return Math.Max(1, (
int)scaled);