From 9a6c2fa7c8890948d3471cec2d208033e9ed189b Mon Sep 17 00:00:00 2001 From: David Sparer Date: Mon, 3 Sep 2018 10:01:06 -0500 Subject: [PATCH] added a bitmap scaling function --- mRemoteV1/UI/DisplayProperties.cs | 59 +++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/mRemoteV1/UI/DisplayProperties.cs b/mRemoteV1/UI/DisplayProperties.cs index 905e0ab5..c3e42abd 100644 --- a/mRemoteV1/UI/DisplayProperties.cs +++ b/mRemoteV1/UI/DisplayProperties.cs @@ -1,5 +1,7 @@ using System; using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; using System.Windows.Forms; namespace mRemoteNG.UI @@ -15,9 +17,62 @@ namespace mRemoteNG.UI /// Scale the given nominal width value by the /// /// - public int CalculateScaledWidth(int width) + public int ScaleWidth(int width) { - return (int) Math.Round(width * ResolutionScalingFactor.Width); + return CalculateScaledValue(width, ResolutionScalingFactor.Width); + } + + /// + /// Scale the given nominal height value by the + /// + /// + public int ScaleHeight(int height) + { + return CalculateScaledValue(height, ResolutionScalingFactor.Width); + } + + /// + /// 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) + { + 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; + } + + /// + /// Scale the given nominal height value by the + /// + /// + private int CalculateScaledValue(int value, float scalingValue) + { + return (int)Math.Round(value * scalingValue); } private static SizeF GetResolutionScalingFactor()