Link to home
Start Free TrialLog in
Avatar of scm0sml
scm0sml

asked on

resizing an uploaded image.

I have the code below.

This is uploading an image fine but when uploading i want resize the image to say 200 wide by 100 tall for example.

I seem to be coming into so many bad articles on this.

Can someone either point me in the direction of a good one or provide the missing code.

thanks
If fupImage.HasFile Then
                    Dim filepath As String = fupImage.PostedFile.FileName
                    Dim pat As String = "\\(?:.+)\\(.+)\.(.+)"
                    Dim r As Regex = New Regex(pat)
                    Dim m As Match = r.Match(filepath)
 
                    Dim file_ext As String = m.Groups(2).Captures(0).ToString()
                    Dim filename As String = m.Groups(1).Captures(0).ToString()
                    Dim newFileName As Guid = Guid.NewGuid
 
                    Dim file As String = newFileName.ToString & "." & file_ext
 
                    'save the file to the server 
                    fupImage.PostedFile.SaveAs(Server.MapPath(".\featuredparks\") & file)
                    'lblStatus.Text = "File Saved to: " & Server.MapPath(".\testimageupload\") & file
 
                    .AdvertImage = file
                End If

Open in new window

Avatar of silemone
silemone
Flag of United States of America image

ok...you're showing upload code...but where are you updating image size?  I don't see an image or Bitmap instance anywhere in the code...in order to change the size of an image, unfortunately you must load it first and change width/height attributes...
and images may warp if 200 by 100 isn't the natural dimensions...i.e. 2:1 isn't the best ratio...then it gets to where you have to mathematically change dimensions proportionately...
Avatar of scm0sml
scm0sml

ASKER

yeah thats what im saying....i couldnt find a good example of doing it, i can normally find what i want easy enough but on this topic i came a lot of rubbish sites so am either after an example code snippet or a decent article on it.

further to your second point. say i need an image to be a size that perhaps the image isnt suitable for, is it just a case of unlucky?
ASKER CERTIFIED SOLUTION
Avatar of arhame
arhame
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh and of course to call it just do something like this, similiar to when you'd upload a regular picture:


Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If FileUpload2.HasFile Then
            Try
                FileUpload2.SaveAs("c:\websites\website1\uploadedpics\" & FileUpload2.FileName)
                SqlDataSource1.Insert()
                lblFileUploadReport.Text = "File successfully saved."
                Dim imageUrl As String = "c:\websites\website1\uploadedpics\" & FileUpload2.FileName & ""
                Dim imageUrlRS As String = "c:\websites\website1\uploadedpics\t_" & FileUpload2.FileName & ""
                ResizePicture(imageUrl, imageUrlRS, New Size(120, 90))
            Catch ex As Exception
                lblFileUploadReport.Text = "Failed because: <br />" & ex.Message
            End Try
        Else
            lblFileUploadReport.Text = "Please select a file before clicking the 'Upload' button."
        End If
End Sub

Open in new window

Avatar of scm0sml

ASKER

i see thats doing.bmp....i guess id have to create some code for all different image extenstions that id want to handle?
Well, only if you want it to save it in the format it's uploaded in.  If you pass it a jpg though it'll still work, it'll save it as a .bmp though.
Avatar of scm0sml

ASKER

ah right ok.

i will be wanting jpg's rather than bmp for my site.

i will try use ur code to convert to jpg
Actually - my fault.  I just looked a little closer and it does save it as a .JPG already.
newbmp.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg)

Saves it as .jpg
Avatar of scm0sml

ASKER

ok brilliant. will try this after work and get back to you.

thanks
Avatar of scm0sml

ASKER

worked well.

cheers