Resize an image using asp.net (vb.net) while uploading it into the server ======================Copy following code to Invoke upload====================== Dim fileExt = System.IO.Path.GetExtension(FileUpload.FileName).ToLower() Dim previewName As String = Me.txtName.Text & "_preview" & fileExt If fileExt.ToString.ToLower = ".jpeg" Or fileExt.ToString.ToLower = ".jpg" Or fileExt.ToString.ToLower = ".gif" Then Dim previewPic As Bitmap = (ResizeImage(FileUpload.PostedFile.InputStream, 500, 695)) previewPic.Save(Server.MapPath("Images/preview/") & previewName, ImageFormat.Jpeg) previewPic.Dispose() End If ==================Copy following Function for resizing image==================== Function ResizeImage(ByVal streamImage As Stream, ByVal maxWidth As Int32, ByVal maxHeight As Int32) As Bitmap Dim originalImage As New Bitmap(streamImage) Dim newWidth As Int32 = originalImage.Width Dim newHeight As Int32 = originalImage.Height Dim aspectRatio As Double = Double.Parse(originalImage.Width) / Double.Parse(originalImage.Height) If (aspectRatio <= 1 And originalImage.Width > maxWidth) Then newWidth = maxWidth newHeight = CInt(Math.Round(newWidth / aspectRatio)) Else
If (aspectRatio > 1 And originalImage.Height > maxHeight) Then newHeight = maxHeight newWidth = CInt(Math.Round(newHeight * aspectRatio))
End If End If Dim newImage As New Bitmap(originalImage, newWidth, newHeight) Dim g As Graphics = Graphics.FromImage(newImage) g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height) originalImage.Dispose() Return newImage End Function