using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using mRemoteNG.Tools; using mRemoteNG.UI.GraphicsUtilities; namespace mRemoteNG.UI { public class DisplayProperties { private readonly IGraphicsProvider _graphicsProvider; public SizeF ResolutionScalingFactor => _graphicsProvider.GetResolutionScalingFactor(); /// /// Creates a new instance with the default /// of type /// public DisplayProperties() : this(new GdiPlusGraphicsProvider()) { } /// /// Creates a new instance with the given /// . /// /// public DisplayProperties(IGraphicsProvider graphicsProvider) { _graphicsProvider = graphicsProvider.ThrowIfNull(nameof(graphicsProvider)); } /// /// Scale the given nominal width value by the /// /// public int ScaleWidth(float width) { return CalculateScaledValue(width, ResolutionScalingFactor.Width); } /// /// Scale the given nominal height value by the /// /// public int ScaleHeight(float height) { return CalculateScaledValue(height, ResolutionScalingFactor.Height); } /// /// Scales the height and width of the given struct /// by the /// /// /// public Size ScaleSize(Size size) { return new Size(ScaleWidth(size.Width), ScaleHeight(size.Height)); } /// /// Scales the given image by /// /// The image to resize. /// The resized image. /// /// Code from https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp /// public Bitmap ScaleImage(Image image) { if (image == null) throw new ArgumentNullException(nameof(image)); var width = ScaleWidth(image.Width); var height = ScaleHeight(image.Height); var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } return destImage; } public Bitmap ScaleImage(Icon icon) { if (icon == null) throw new ArgumentNullException(nameof(icon)); return ScaleImage(icon.ToBitmap()); } /// /// Scale the given nominal height value by the /// /// private int CalculateScaledValue(float value, float scalingValue) { return (int)Math.Round(value * scalingValue); } } }