Here's my VB code as promised. I'm a newbee to VWD and ASP.NET and know how hard it can be to find useful code. My code might not be elegant but it works well for me! Hope it helps someone else.
Imports
System.IO
Imports
System.Drawing
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnUpload_Click(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles btnUpload.Click
Const bmpW = 300
'New image canvas width
Const bmpH = 226
'New Image canvas height
If (FileUpload1.HasFile)
Then
lblError.Text =
""
'Check to make sure the file to upload has a picture file format extention
If (CheckFileType(FileUpload1.FileName))
Then
Dim newWidth
As Integer = bmpW
Dim newHeight
As Integer = bmpH
'Use the uploaded filename without the '.' extension
Dim upName
As String = Mid(FileUpload1.FileName, 1, (InStr(FileUpload1.FileName,
".") - 1))
Dim filePath
As String =
"~/Upload/" & upName &
".png"
'Create a new Bitmap using the uploaded picture as a Stream
'Set the new bitmap resolution to 72 pixels per inch
Dim upBmp As Bitmap = Bitmap.FromStream(FileUpload1.PostedFile.InputStream)
Dim newBmp
As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb)
newBmp.SetResolution(72, 72)
'Get the uploaded image width and height
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
'Keep the aspect ratio of image the same if not 4:3 and work out the newX and newY positions
'to ensure the image is always in the centre of the canvas vertically and horizontally
If upWidth > upHeight
Then 'Landscape picture
reDuce = newWidth / upWidth
'calculate the width percentage reduction as decimal
newHeight = Int(upHeight * reDuce)
'reduce the uploaded image height by the reduce amount
newY = Int((bmpH - newHeight) / 2)
'Position the image centrally down the canvas
newX = 0
'Picture will be full width
ElseIf upWidth < upHeight
Then 'Portrait picture
reDuce = newHeight / upHeight
'calculate the height percentage reduction as decimal
newWidth = Int(upWidth * reDuce)
'reduce the uploaded image height by the reduce amount
newX = Int((bmpW - newWidth) / 2)
'Position the image centrally across the canvas
newY = 0
'Picture will be full hieght
ElseIf upWidth = upHeight
Then 'square picture
reDuce = newHeight / upHeight
'calculate the height percentage reduction as decimal
newWidth = Int(upWidth * reDuce)
'reduce the uploaded image height by the reduce amount
newX = Int((bmpW - newWidth) / 2)
'Position the image centrally across the canvas
newY = Int((bmpH - newHeight) / 2)
'Position the image centrally down the canvas
End If
'Create a new image from the uploaded picture using the Graphics class
'Clear the graphic and set the background colour to white
'Use Antialias and High Quality Bicubic to maintain a good quality picture
'Save the new bitmap image using 'Png' picture format and the calculated canvas positioning
Dim newGraphic
As Graphics = Graphics.FromImage(newBmp)
Try
'Show the uploaded resized picture in the image control
Image1.ImageUrl = filePath
Image1.Visible =
True
Catch ex As Exception
lblError.Text = ex.ToString
Finally
upBmp.Dispose()
newBmp.Dispose()
newGraphic.Dispose()
End Try
Else
lblError.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 Function
End
Class
Regards
Smcoxon
No Gem is ever polished without some friction.
Marked as answer by smcoxon on Mar 11, 2007 10:25 PM
smcoxon
Contributor
5455 Points
948 Posts
Re: Resize Images on upload
Mar 11, 2007 10:23 PM|LINK
Here's my VB code as promised. I'm a newbee to VWD and ASP.NET and know how hard it can be to find useful code. My code might not be elegant but it works well for me! Hope it helps someone else.
Imports
System.IOImports
System.DrawingPartial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Const bmpW = 300 'New image canvas width
Const bmpH = 226 'New Image canvas height If (FileUpload1.HasFile) ThenlblError.Text =
"" 'Check to make sure the file to upload has a picture file format extention If (CheckFileType(FileUpload1.FileName)) Then Dim newWidth As Integer = bmpW Dim newHeight As Integer = bmpH 'Use the uploaded filename without the '.' extension Dim upName As String = Mid(FileUpload1.FileName, 1, (InStr(FileUpload1.FileName, ".") - 1)) Dim filePath As String = "~/Upload/" & upName & ".png" 'Create a new Bitmap using the uploaded picture as a Stream 'Set the new bitmap resolution to 72 pixels per inch Dim upBmp As Bitmap = Bitmap.FromStream(FileUpload1.PostedFile.InputStream) Dim newBmp As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb)newBmp.SetResolution(72, 72)
'Get the uploaded image width and height 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 'Keep the aspect ratio of image the same if not 4:3 and work out the newX and newY positions 'to ensure the image is always in the centre of the canvas vertically and horizontally If upWidth > upHeight Then 'Landscape picturereDuce = newWidth / upWidth
'calculate the width percentage reduction as decimalnewHeight = Int(upHeight * reDuce)
'reduce the uploaded image height by the reduce amountnewY = Int((bmpH - newHeight) / 2)
'Position the image centrally down the canvasnewX = 0
'Picture will be full width ElseIf upWidth < upHeight Then 'Portrait picturereDuce = newHeight / upHeight
'calculate the height percentage reduction as decimalnewWidth = Int(upWidth * reDuce)
'reduce the uploaded image height by the reduce amountnewX = Int((bmpW - newWidth) / 2)
'Position the image centrally across the canvasnewY = 0
'Picture will be full hieght ElseIf upWidth = upHeight Then 'square picturereDuce = newHeight / upHeight
'calculate the height percentage reduction as decimalnewWidth = Int(upWidth * reDuce)
'reduce the uploaded image height by the reduce amountnewX = Int((bmpW - newWidth) / 2)
'Position the image centrally across the canvasnewY = Int((bmpH - newHeight) / 2)
'Position the image centrally down the canvas End If 'Create a new image from the uploaded picture using the Graphics class 'Clear the graphic and set the background colour to white 'Use Antialias and High Quality Bicubic to maintain a good quality picture 'Save the new bitmap image using 'Png' picture format and the calculated canvas positioning Dim newGraphic As Graphics = Graphics.FromImage(newBmp) TrynewGraphic.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)
'Show the uploaded resized picture in the image controlImage1.ImageUrl = filePath
Image1.Visible =
True Catch ex As ExceptionlblError.Text = ex.ToString
FinallyupBmp.Dispose()
newBmp.Dispose()
newGraphic.Dispose()
End Try ElselblError.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
ClassSmcoxon
No Gem is ever polished without some friction.