From af3459e123101ec414d92d35c7cb239b2bbe432d Mon Sep 17 00:00:00 2001 From: Michael L Smith Date: Mon, 7 Nov 2016 20:59:07 -0800 Subject: [PATCH] Added an auto-resize if an image is over 150 pixels wide or high. --- .../Utility/Images/CubistImageProcessor.cs | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs b/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs index d9325b9..ef0540f 100644 --- a/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs +++ b/OSCADSharp/OSCADSharp/Utility/Images/CubistImageProcessor.cs @@ -3,6 +3,8 @@ using OSCADSharp.Spatial; using System; using System.Collections.Generic; using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -55,9 +57,25 @@ namespace OSCADSharp.Utility.Images } #region Private Methods + + private List processImage() { - Bitmap img = new Bitmap(Image.FromFile(this.imagePath)); + var rawImg = Image.FromFile(this.imagePath); + Bitmap img; + + if(rawImg.Width > 150 || rawImg.Height > 150) + { + var wdthRatio = (double)rawImg.Width / (double)rawImg.Height; + var htRatio = (double)rawImg.Height / (double)rawImg.Width; + + img = resizeImage(rawImg, (int)(150*wdthRatio), (int)(150*htRatio)); + } + else + { + img = new Bitmap(rawImg); + } + this.setColorArray(img); var simplifier = new ImageSimplifier(img.Width, img.Height, pixels); @@ -248,6 +266,40 @@ namespace OSCADSharp.Utility.Images return null; } + + // Image resize method, found on this SO thread + //http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp + /// + /// Resize the image to the specified width and height. + /// + /// The image to resize. + /// The width to resize to. + /// The height to resize to. + /// The resized image. + private static Bitmap resizeImage(Image image, int width, int 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; + } #endregion } }