{"id":892,"date":"2018-04-10T15:26:00","date_gmt":"2018-04-10T14:26:00","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=892"},"modified":"2020-04-10T15:58:10","modified_gmt":"2020-04-10T14:58:10","slug":"c-resize-images","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-resize-images\/","title":{"rendered":"C# Resize Images"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> \nusing System;\nusing System.Collections.Generic;\nusing System.Drawing;\nusing System.Drawing.Drawing2D;\nusing System.Drawing.Imaging;\nusing System.Linq;\nusing System.Web;\n\nnamespace Default\n{\n    public class ImageHelper\n    {\n        private static int DoubleToIntDef(double d, int def)\n        {\n\t        try\n\t        {\n\t\t        return Convert.ToInt32(d);\n\t        }\n\t        catch (Exception)\n\t        {\n\t\t        return def;\n\t        }\n        }\n\n        public static Size GetImageContainSize(Size currentSize, Size maxSize, bool scaleUp = false)\n        {\n\t        Size newSize = currentSize;\n\t        double ratioX = Convert.ToDouble(maxSize.Width) \/ Convert.ToDouble(currentSize.Width);\n\t        double ratioY = Convert.ToDouble(maxSize.Height) \/ Convert.ToDouble(currentSize.Height);\n\t        double ratio = System.Math.Min(ratioX, ratioY);\n\n\t        if (ratio &lt; 1.0 || scaleUp)\n\t        {\n\t\t        newSize.Width = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Width) * ratio), 0);\n\t\t        newSize.Height = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Height) * ratio), 0);\n\t        }\n\t        return newSize;\n        }\n\n        public static Bitmap ResizeImage(Image image, int width, int height)\n        {\n            var destRect = new Rectangle(0, 0, width, height);\n            var destImage = new Bitmap(width, height);\n\n            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);\n\n            using (var graphics = Graphics.FromImage(destImage))\n            {\n                graphics.CompositingMode = CompositingMode.SourceCopy;\n                graphics.CompositingQuality = CompositingQuality.HighQuality;\n                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;\n                graphics.SmoothingMode = SmoothingMode.HighQuality;\n                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;\n\n                using (var wrapMode = new ImageAttributes())\n                {\n                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);\n                    graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);\n                }\n            }\n\n            return destImage;\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-892","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=892"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/892\/revisions"}],"predecessor-version":[{"id":3698,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/892\/revisions\/3698"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}