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

  • Thread starter Thread starter Darrel
  • Start date Start date
D

Darrel

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
 
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.
 
Use the Image.FromStream method, passing the HttpPostedFile.InputStream
property to it.

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
 
:-D

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Back
Top