{"id":2254,"date":"2012-10-10T09:53:09","date_gmt":"2012-10-10T13:53:09","guid":{"rendered":"http:\/\/www.iowawebnet.com\/ein\/?p=2254"},"modified":"2013-01-24T11:40:56","modified_gmt":"2013-01-24T15:40:56","slug":"resize-image-before-upload","status":"publish","type":"post","link":"https:\/\/www.iowawebnet.com\/ein\/2012\/10\/resize-image-before-upload\/","title":{"rendered":"Resize image before upload"},"content":{"rendered":"<pre>\r\nMarkup Page\r\n<code>\r\n&lt;div id=\"FileUpd1\" runat=\"server\" >\r\nDefault:&nbsp;&lt;asp:FileUpload ID=\"ImgUpload1\" runat=\"server\" \/>\r\n&lt;asp:Button ID=\"Btn1\" runat=\"server\" Text=\"Submit \" \r\nCssClass=\"btnSubmit\" CausesValidation=\"true\"\/>\r\n<\/code>\r\n\r\n<a style=\"display:none;\" id=\"te20733739\" href=\"javascript:expand('#te20733739')\">Code Behind - Click to read more...<\/a>\n<div class=\"te_div\" id=\"te20733739\"><script language=\"JavaScript\" type=\"text\/javascript\">expander_hide('#te20733739');<\/script>\r\n<code>\r\nImports System.IO\r\nImports System.Web.UI\r\nImports System.Data.Sql\r\nImports System.Data.SqlClient\r\nImports System.Drawing\r\nImports System.Drawing.Imaging\r\n\r\nProtected Sub Btn1_Click(ByVal sender As Object, \r\n              ByVal e As System.EventArgs) Handles Btn1.Click\r\n\r\n'define variable\r\nDim adPhotoName As String = \"PhotoNum\"\r\n\r\n'img directory - physical path\r\nDim saveDir As String = \"\\photo_repository\\\"\r\nDim appPath As String = Request.PhysicalApplicationPath\r\n\r\n'img extensiong check\r\nDim ImgExt1 As String = LCase(System.IO.Path.GetExtension(ImgUpload1.FileName))\r\nDim ImgName1 = adPhotoName & \"_1\" & ImgExt1\r\n\r\n If (ImgUpload1.HasFile) Then\r\n \r\n 'save with custom file name \r\n Dim savePath As String = Path.Combine(Server.MapPath(\"~\/photo_repository\"), ImgName1)\r\n 'save with default file name               \r\n 'Dim savePath As String = appPath + saveDir + Server.HtmlEncode(ImgUpload1.FileName)\r\n\r\n 'allow only jpg and gif photo files\r\n    If (ImgExt1.ToString = \".jpg\") Or \r\n       (ImgExt1.ToString = \".jpeg\") Or \r\n       (ImgExt1.ToString = \".gif\") Then\r\n\r\n    'resize image before upload\r\n      Dim image As New Bitmap(System.Drawing.Image.FromStream +\r\n                            (ImgUpload1.PostedFile.InputStream))\r\n\r\n     Dim width As Integer = 448   'image width\r\n     Dim height As Integer = 336  'image height\r\n\r\n     Using thumbnail As System.Drawing.Image = image.GetThumbnailImage + \r\n     (width, height, New System.Drawing.Image.GetThumbnailImageAbort + \r\n     (<b>AddressOf ThumbnailCallback<\/b>), IntPtr.Zero)\r\n\r\n        Using memoryStream As New MemoryStream()\r\n        '**default img name - use these two lines of code\r\n        'thumbnail.Save(Server.MapPath(\"~\/Uploaded\/\") + \r\n        'Path.GetFileName(Me.ImgUpload1.FileName), ImageFormat.Png)\r\n        '********\r\n        thumbnail.Save(savePath)\r\n      End Using\r\n   End Using\r\n'ends here\r\n\r\n'confirm image upload\r\nImgUpConf1 = ImgName1.ToString\r\nFileUpd1.InnerHtml = + \r\n  \"&lt;table>&lt;tr>&lt;td>&lt;img src='..\/photo_repository\/\" +\r\n  ImgUpConf1 & \"' height='35' width='50'>\" +\r\n  \"&lt;td style='vertical-align: top;'>&lt;strong>Image 1:&nbsp;\" + \r\n  ImgUpload1.FileName & \"&lt;br \/>\" +\r\n  \"&lt;span class='lblAdded'>Image 1 successfuly uploaded!\"\r\n\r\nElse\r\n\r\n'alert if img fiel ext <> jpg or gif\r\nBadImgFormat = ImgExt1.ToString\r\nImgName1 = NoImage      'db photo record\r\nFileUpd1.InnerHtml = +\r\n  \"&lt;table>&lt;tr>&lt;td>&lt;img src='..\/photo_repository\/no_image.jpg' \" + \r\n  \"height='35' width='50'\/>\" +\r\n  \"&lt;td style='vertical-align: top;' >&lt;span style='color: red;'>\" +\r\n  \"ERROR: The default image format is \" & BadImgFormat & \"!&lt;br \/>\" +\r\n  \"Only .jpg and .gif file type allowed.&lt;br \/>&lt;a href='ad_photo.aspx?img=1'\" +\r\n  \"class='colorbox2'>Try again.\"\r\n\r\n  End If\r\n End Sub\r\n\r\n'define function   \r\n Public Function <b>ThumbnailCallback()<\/b> As Boolean\r\n        Return False\r\n    End Function\r\nEnd Class\r\n<\/div>\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Markup Page &lt;div id=&#8221;FileUpd1&#8243; runat=&#8221;server&#8221; > Default:&nbsp;&lt;asp:FileUpload ID=&#8221;ImgUpload1&#8243; runat=&#8221;server&#8221; \/> &lt;asp:Button ID=&#8221;Btn1&#8243; runat=&#8221;server&#8221; Text=&#8221;Submit &#8221; CssClass=&#8221;btnSubmit&#8221; CausesValidation=&#8221;true&#8221;\/> Code Behind &#8211; Click to read more&#8230; Imports System.IO Imports System.Web.UI Imports System.Data.Sql Imports System.Data.SqlClient Imports System.Drawing Imports System.Drawing.Imaging Protected Sub Btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn1.Click &#8216;define variable Dim adPhotoName As String = [&hellip;]<\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85,17,16,52,72,84],"tags":[19,74,79,21],"class_list":["post-2254","post","type-post","status-publish","format-standard","hentry","category-net-4-0","category-applications","category-net","category-controls","category-images","category-vs2010","tag-asp-net","tag-image","tag-upload","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/2254"}],"collection":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/comments?post=2254"}],"version-history":[{"count":22,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/2254\/revisions"}],"predecessor-version":[{"id":2447,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/2254\/revisions\/2447"}],"wp:attachment":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/media?parent=2254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/categories?post=2254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/tags?post=2254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}