Imports System.IOImports System.DrawingPartial Class Default2 Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Const bmpW = 200 Const bmpH = 175 If (FileUpload1.HasFile) Then Label1.Text = "" If (CheckFileType(FileUpload1.FileName)) Then Dim newWidth As Integer = bmpW Dim newHeight As Integer = bmpH Dim upName As String = Mid(FileUpload1.FileName, 1, (InStr(FileUpload1.FileName, ".") - 1)) ' Dim upname As String = FileUpload1.FileName Dim filePath As String = "~/Upload/" & upName & ".png" Dim upBmp As Bitmap = Bitmap.FromStream(FileUpload1.PostedFile.InputStream) Dim newBmp As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb) newBmp.SetResolution(72, 72) Dim upWidth As Integer = upBmp.Width Dim upHeight As Integer = upBmp.Height Dim newX As Integer = 0 Dim newY As Integer = 0 Dim reDuce As Decimal If upWidth > upHeight Then reDuce = newWidth / upWidth newHeight = Int(upHeight * reDuce) newY = Int((bmpH - newHeight) / 2) newX = 0 ElseIf upWidth < upHeight Then reDuce = newHeight / upHeight newWidth = Int(upWidth * reDuce) newX = Int((bmpW - newWidth) / 2) newY = 0 ElseIf upWidth = upHeight Then reDuce = newHeight / upHeight newWidth = Int(upWidth * reDuce) newX = Int((bmpW - newWidth) / 2) newY = Int((bmpH - newHeight) / 2) End If Dim newGraphic As Graphics = Graphics.FromImage(newBmp) Try newGraphic.Clear(Color.White) newGraphic.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias newGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight) newBmp.Save(MapPath(filePath), Imaging.ImageFormat.Png) Image1.ImageUrl = filePath Image1.Visible = True Catch ex As Exception Label1.Text = ex.ToString Finally upBmp.Dispose() newBmp.Dispose() newGraphic.Dispose() End Try Else Label1.Text = "Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png." End If End If End Sub Function CheckFileType(ByVal fileName As String) As Boolean Dim ext As String = Path.GetExtension(fileName) Select Case ext.ToLower() Case ".gif" Return True Case ".png" Return True Case ".jpg" Return True Case ".jpeg" Return True Case ".bmp" Return True Case Else Return False End Select End FunctionEnd Class