How to pass a postedfile to a system.drawing.image

Discussion in 'ASP .Net' started by Darrel, Apr 23, 2006.

  1. Darrel

    Darrel Guest

    I'm grabbing a file from a file upload form field. This is a
    'system.web.httppostedfile'

    I would like to modify the image (Cropping/scaling) using
    system.drawing.image.

    Is there anyway to go from 'system.web.httppostedfile' directly to
    'system.drawing.image'?

    Normally, I'd grab the System.Drawing.Image from a file itself:

    Dim g As System.Drawing.Image =
    System.Drawing.Image.FromFile(pathToFile.jpg)

    But I can't figure how how to go directly from a postedfile to an image
    type. I keep getting cast errors.

    The only solution I've come up with is to grab the posted file, save it,
    then regrab it to turn into an image, but that seems like a redundant step.

    -Darrel
     
    Darrel, Apr 23, 2006
    #1
    1. Advertisements

  2. Use the Image.FromStream method, passing the HttpPostedFile.InputStream
    property to it.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Professional Numbskull

    Hard work is a medication for which
    there is no placebo.
     
    Kevin Spencer, Apr 23, 2006
    #2
    1. Advertisements

  3. Darrel

    Darrel Guest

    Use the Image.FromStream method, passing the HttpPostedFile.InputStream
    Kevin:

    Thanks! That should do the trick.

    However, I'm getting an error.

    Here's my function call:

    uploadAndSizeImage(System.Drawing.Image.FromStream(uploadedFileMD.PostedFile.InputStream))

    I'm getting this error:

    Exception Details: System.ArgumentException: Invalid parameter used.

    Google isn't turning up much though I did find a reference to it possibly
    being caused by the stream reader being at the end of the stream and me
    needing to reset it to position one. But that was kind of vague and not
    really sure if it applies here.

    -Darrel
     
    Darrel, Apr 24, 2006
    #3
  4. Darrel

    Darrel Guest

    Thanks! That should do the trick.
    The error = user error.

    Thanks, kevin. Things are working!

    -Darrel
     
    Darrel, Apr 24, 2006
    #4
  5. :-D

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Professional Numbskull

    Hard work is a medication for which
    there is no placebo.
     
    Kevin Spencer, Apr 24, 2006
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.