Uploading and resizing an image.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I am trying to upload an image, and resize it automatically onto a web server. I have successfully uploaded the file, and I am able to rename it and change the file extention on it. Here is the code that I am using:
Code:
Dim strLongFilePath As String = fileupload1.PostedFile.FileName
Dim intFileNameLength As Integer = InStr(1, StrReverse(strLongFilePath), "\")
Dim strFileName As String = Mid(strLongFilePath, (Len(strLongFilePath) - intFileNameLength) + 2)

FileUpload1.PostedFile.SaveAs(Server.MapPath("\Samples\") & strFileName)

What I am having a problem with is in resizing the file. I have been looking on google but the examples I have found are related to window applications.

Any suggestions?

Mike55.
 
Hi all

Here is the code that I am using to resize the image, it fires off without an exception, however, it does not seem to create the new image.
Code:
Private Sub ResizeImage(ByRef fileName As String, ByVal factor As Integer)
Dim img As Bitmap
Dim imageResize As Bitmap
Dim fs As New FileStream(Server.MapPath("\Samples\") & fileName, FileMode.Open, FileAccess.Read)
Dim imgData(fs.Length) As Byte
Dim ms As New MemoryStream()

fs.Read(imgData, 0, fs.Length)
fs.Close()

Try
  img = Image.FromStream(New MemoryStream(imgData))
  imageResize = New Bitmap(img, New Size(img.Size.Width * factor, img.Size.Height * factor))
  imageResize.Save(ms, Imaging.ImageFormat.Bmp)
Catch ex As Exception
  Stop
End Try 
End Sub

Any suggestions?

Mike55.
 
Theimage has been saved to the MemoryStream. If you want to save it out to a physical file use a FileStream, if you needto do something further with the image then make the function return either ms or an image constructed from ms.
 
Back
Top