JPEG: Upload, resize & save

  • 14 years ago

    Hi

    Any one who can help me?
    I wanna write my self a script that can upload, resize and save an JPEG file.
    I know some about system.drawing and some about FileUpLoad, but not much.
    Where do I start?
    Any turtorials?







  • 14 years ago
  • 14 years ago

    Hello,

    If you can upload a file to the server then you can use the following code to resample the image to the size you want. I wrote it a wile ago. If you have problems post again.

    Regards

    Brin

    ______________________________________________________________________

    'need these imports at top
    Import System.Drawing.Bitmap
    Import System.Drawing.Imaging
    Import System.Drawing


    '************ Resize image ********************
     'load the uploaded photo on the server
     Dim TheImage AS System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("path/fileName"))
     
     Dim TheBitmap As bitmap = new bitmap(TheImage)
     Dim TheWidth as integer = TheBitmap.Width
     Dim TheHeight as integer = TheBitmap.Height
     
     Dim NewWidth, NewHeight as integer  
    'test if horizontal or vertical orintated
     if TheWidth > TheHeight then
    ' Set the new width and height of the photo 
    ' **horizontal**
     'Resize photo and maintain aspect ratio
     NewWidth = 500
     NewHeight = (TheHeight/(TheWidth/NewWidth))
    else
    '**vertical**
     'Resize photo and maintain aspect ratio
     NewHeight = 450
     NewWidth = (TheWidth/(TheHeight/NewHeight))
    end if




















     'reload the photo at the resampled size 
     Dim NewBitmap As bitmap = new bitmap(TheImage, NewWidth, NewHeight) '
     
     'Save the photo
     NewBitmap.Save(MapPath("path/fileName.jpg"), ImageFormat.Jpeg)

    TheImage.Dispose()    'Free image from server





Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond