Last post Dec 11, 2015 05:44 AM by Fei Han - MSFT
Dec 10, 2015 08:53 PM|dlchase|LINK
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
filePath &= "\\" & txtPhotoPrefix.Text & fiu.Name & "." & fiu.Extension
filePath = Replace(filePath, "..jpg", "")
txtMsg.Text = filePath
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))
Return New Bitmap(originalImage, newWidth, newHeight)
Dec 10, 2015 09:14 PM|dlchase|LINK
I figured it out.
Dec 11, 2015 05:44 AM|Fei Han - MSFT|LINK
I figured it out.
I’m glad to hear that you resolve the problem by yourself. Could you show us your solution? Besides, the following article explained how to resize image, hoping helpful for others to resolve the resizing image problem.