We have an asp.net page with the AsyncFileUpload control to do immediate uploads of a selected file(s) and it works perfectly. However, the users now want to have the file resized before it is saved/uploaded. We are using the following code-behind to save the uploaded file to a specific location. After that code is a Resize_Image function that we use for resizing an image from a stream and then saving the resized image. How can I get the code-behind to create a stream for the upload?
Protected Sub UploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs) Handles AsyncFileUpload1.UploadedComplete
Dim filePath As String = txtFolderPath.Text
Dim fi As FileInfo = New FileInfo(filePath)
filePath = Replace(filePath, "\", "\\")
filePath = Replace(filePath, "\\\\", "\\")
Dim fiu As FileInfo = New FileInfo(AsyncFileUpload1.PostedFile.FileName)
If ckJobIDPhoto.Checked Then
filePath &= "\\Job ID Photo" & fiu.Name & "." & fiu.Extension
ckJobIDPhoto.Checked = False
Else
filePath &= "\\" & txtPhotoPrefix.Text & fiu.Name & "." & fiu.Extension
End If
filePath = Replace(filePath, "..jpg", "")
txtMsg.Text = filePath
AsyncFileUpload1.SaveAs(filePath)
End Sub
Public Shared Function Resize_Image(ByVal streamImage As Stream, ByVal maxWidth As Integer, ByVal maxHeight As Integer) As Bitmap
Dim originalImage As New Bitmap(streamImage)
Dim newWidth As Integer = originalImage.Width
Dim newHeight As Integer = originalImage.Height
Dim aspectRatio As Double = CDbl(originalImage.Width) / CDbl(originalImage.Height)
If aspectRatio <= 1 AndAlso originalImage.Width > maxWidth Then
newWidth = maxWidth
newHeight = CInt(Math.Round(newWidth / aspectRatio))
ElseIf aspectRatio > 1 AndAlso originalImage.Height > maxHeight Then
newHeight = maxHeight
newWidth = CInt(Math.Round(newHeight * aspectRatio))
End If
Return New Bitmap(originalImage, newWidth, newHeight)
End Function