{"id":122,"date":"2016-03-19T11:48:47","date_gmt":"2016-03-19T10:48:47","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=122"},"modified":"2016-03-19T11:50:27","modified_gmt":"2016-03-19T10:50:27","slug":"c-calculate-image-size-for-fitting-or-cropping","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-calculate-image-size-for-fitting-or-cropping\/","title":{"rendered":"C#  Calculate image size for fitting or cropping"},"content":{"rendered":"<pre lang=\"c#\">\r\npublic static System.Drawing.Size GetImageSize(System.Drawing.Size currentSize, System.Drawing.Size maxSize, bool scaleUp)\r\n{\r\n\tSystem.Drawing.Size newSize = currentSize;\r\n\tdouble ratioX = Convert.ToDouble(maxSize.Width) \/ Convert.ToDouble(currentSize.Width);\r\n\tdouble ratioY = Convert.ToDouble(maxSize.Height) \/ Convert.ToDouble(currentSize.Height);\r\n\tdouble ratio = System.Math.Min(ratioX, ratioY);\r\n\r\n\tif (ratio < 1.0 || scaleUp)\r\n\t{\r\n\t\tnewSize.Width = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Width) * ratio), 0);\r\n\t\tnewSize.Height = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Height) * ratio), 0);\r\n\t}\r\n\treturn newSize;\r\n}\r\n\r\npublic static System.Drawing.Size GetMinImageSizeToFit(System.Drawing.Size currentSize, System.Drawing.Size targetSize)\r\n{\r\n\tSystem.Drawing.Size newSize = currentSize;\r\n\tdouble ratioX = Convert.ToDouble(targetSize.Width) \/ Convert.ToDouble(currentSize.Width);\r\n\tdouble ratioY = Convert.ToDouble(targetSize.Height) \/ Convert.ToDouble(currentSize.Height);\r\n\tdouble ratio = System.Math.Max(ratioX, ratioY);\r\n\r\n\tnewSize.Width = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Width) * ratio), 0);\r\n\tnewSize.Height = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Height) * ratio), 0);\r\n\treturn newSize;\r\n}\r\n\r\nprivate static int DoubleToIntDef(double d, int def)\r\n{\r\n\ttry\r\n\t{\r\n\t\treturn Convert.ToInt32(d);\r\n\t}\r\n\tcatch (Exception)\r\n\t{\r\n\t\treturn def;\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>public static System.Drawing.Size GetImageSize(System.Drawing.Size currentSize, System.Drawing.Size maxSize, bool scaleUp) { System.Drawing.Size newSize = currentSize; double ratioX = Convert.ToDouble(maxSize.Width) \/ Convert.ToDouble(currentSize.Width); double ratioY = Convert.ToDouble(maxSize.Height) \/ Convert.ToDouble(currentSize.Height); double ratio = System.Math.Min(ratioX, ratioY); if (ratio < 1.0 || scaleUp) { newSize.Width = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Width) * ratio), 0); newSize.Height = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Height) * ratio), 0); } return newSize; } public [&hellip;]\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/122","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=122"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/122\/revisions\/124"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}