{"id":232,"date":"2012-09-21T11:56:43","date_gmt":"2012-09-21T11:56:43","guid":{"rendered":"http:\/\/ntspl.co.in\/blog\/?p=232"},"modified":"2024-09-18T10:44:33","modified_gmt":"2024-09-18T10:44:33","slug":"proportionally-resize-bitmap","status":"publish","type":"post","link":"https:\/\/www.ntspl.co.in\/blog\/proportionally-resize-bitmap\/","title":{"rendered":"Proportionally Resize Bitmap"},"content":{"rendered":"<p><strong>1.import two namespace<\/strong><\/p>\n<pre class=\"code-structure\"><code>\r\nusing System.Drawing;\r\n\r\nusing System.Drawing.Imaging;\r\n<\/code><\/pre>\n<p><strong>2.Write the function<\/strong><\/p>\n<pre class=\"code-structure\"><code>\r\npublic Bitmap ProportionallyResizeBitmap(Bitmap src, int maxWidth, int maxHeight) {\r\n\r\n\t<span class=\"comment\">\/\/ original dimensions<\/span>\r\n\r\n\tint w = src.Width;\r\n\tint h = src.Height;\r\n\r\n\t<span class=\"comment\">\/\/ Longest and shortest dimension<\/span>\r\n\r\n\tint longestDimension = (w &gt; h) ? w : h;\r\n\tint shortestDimension = (w &lt; h) ? w : h;\r\n\r\n\t<span class=\"comment\">\/\/ propotionality<\/span>\r\n\r\n\tfloat factor = ((float)longestDimension) \/ shortestDimension;\r\n\r\n\t<span class=\"comment\">\/\/ default width is greater than height<\/span>\r\n\r\n\tdouble newWidth = maxWidth;\r\n\tdouble newHeight = maxWidth \/ factor;\r\n\r\n\t<span class=\"comment\">\/\/ if height greater than width recalculate<\/span>\r\n\r\n\tif (w &lt; h) {\r\n\r\n\t\tnewWidth = maxHeight \/ factor;\r\n\t\tnewHeight = maxHeight;\r\n\r\n\t}\r\n\r\n\t<span class=\"comment\">\/\/ Create new Bitmap at new dimensions<\/span>\r\n\r\n\tBitmap result = new Bitmap((int)newWidth, (int)newHeight);\r\n\tusing (Graphics g = Graphics.FromImage((System.Drawing.Image)result))\r\n\tg.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);\r\n\r\n\treturn result;\r\n\r\n}\r\n<\/code><\/pre>\n<p><strong>3.At the time call function write<\/strong><\/p>\n<pre class=\"code-structure\"><code>\r\nif (fuImageFile.HasFile) {\r\n\r\n\tstring fileName = fuImageFile.FileName;\r\n\r\n\t<span class=\"comment\">\/\/ Get the bitmap data from the uploaded file<\/span>\r\n\r\n\tBitmap src = Bitmap.FromStream(fuImageFile.PostedFile.InputStream) as Bitmap;\r\n\r\n\t<span class=\"comment\">\/\/ Resize the bitmap data<\/span>\r\n\r\n\tBitmap result = ProportionallyResizeBitmap(src, 630, 252);\r\n\r\n\tstring saveName = Server.MapPath(savePath) + fileName;\r\n\r\n\tresult.Save(saveName, ImageFormat.Jpeg);\r\n\r\n\ttbbanner.BANNER_IMAGE = fileName;\r\n\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.import two namespace using System.Drawing; using System.Drawing.Imaging; 2.Write the function public Bitmap ProportionallyResizeBitmap(Bitmap src, int maxWidth, int maxHeight) { \/\/ original dimensions int w = src.Width; int h = src.Height; \/\/ Longest and shortest dimension int longestDimension = (w &gt; h) ? w : h; int shortestDimension = (w &lt; h) ? w : h; [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":1521,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[281,296],"class_list":["post-232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-dot-net","tag-proportinally-resize","tag-resize-image"],"acf":{"custom_meta_title":"Proportionally Resize a Bitmap to a Fixed Size | NTSPL","meta_description":"Do you know how to proportionally resize a bitmap to a fixed size? Get to know how to proportionally resize a bitmap to a fixed size.","meta_keyword":"resize bitmap, proportionally resize bitmap, resize bitmap to fixed size","other_meta_tag":"<meta property=og:locale content=\"en-IN\" \/>\r\n<meta property=og:type content=\"website\" \/>\r\n<meta property=og:title content=\"Proportionally Resize a Bitmap to a Fixed Size | NTSPL\"\/>\r\n<meta property=og:description content=\"Do you know how to proportionally resize a bitmap to a fixed size? Get to know how to proportionally resize a bitmap to a fixed size.\"\/>\r\n<meta property=og:url content=\"https:\/\/www.ntspl.co.in\/blog\/proportionally-resize-bitmap\"\/>\r\n<meta property=\"og:image\" content=\"https:\/\/www.ntspl.co.in\/blog\/wp-content\/uploads\/2012\/09\/proportionally-resize-bitmap.jpg\"\/>\r\n<meta property=og:site_name content=NTSPL \/>\r\n<meta name=\"twitter:site\" content=\"@NTSPL\">\r\n<meta name=twitter:card content=\"summary\" \/>\r\n<meta name=twitter:description content=\"Do you know how to proportionally resize a bitmap to a fixed size? Get to know how to proportionally resize a bitmap to a fixed size.\"\/>\r\n<meta name=twitter:title content=\"Proportionally Resize a Bitmap to a Fixed Size | NTSPL\"\/>"},"_links":{"self":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/232"}],"collection":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":7,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":10577,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions\/10577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/media\/1521"}],"wp:attachment":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}