file upload - How can I create an image object from an FileUpload control in asp.net? -


I have a FileUpload control. I am trying to save the uploaded file (an image) and also saved several thumbnail copies of the file.

When I try to do something like this:

System.Drawing.Image ImgOriginal = System.Drawing.Image.FromStream (PhotoUpload.PostedFile.InputStream);

I do not have a "System.ArgumentException: parameter valid."

I also tried to use PhotoUpload.FileBytes to create an image from file bytes instead of InputStream, but the same error occurs.

The uploaded file is a JPG. I know that this is a valid JPG because it saves the original fine.

EDIT: This code really works. The parameter is not valid PhotoUpload.PostedFile.InputStream was empty ... which seems to be a totally different issue. It seems that after uploading my original file the file is uploaded.

EDIT: It is learned that InputStream of a FileUpload can be read only once / and once it is consumed and then it is finished.

To get around I uploaded the file and saved the file byte in a byte array and used the byte array to copy the image.

code:

  // Copy a byte array byte [] imageData = PhotoUpload.FileBytes in FileBytes; // If you want to save it from somewhere then create a stream from the byte array: the system. Io Stream mystream = new system IOM .Morestream (image data); // Create an image from the stream as often as you need: System Drawing Image Implement = System. Drawing. Image Frostream (Mystream);  

view this link

This is my function call:

Upload and SICImage (System.Downings.image.FostStream (Uploadfile MD.PostedFile.InputStream))

I am getting this error :

Exception Description: System.ArgumentException: Invalid parameter is used.

Although Google has not found any reference to this, probably due to the stream reader at the end of the stream and I need to move it to one place

< P> Edit:

In addition, do you also have it

  System.IO.FileStream fs = System.IO.File.OpenRead (@ "Image. JPG ") tried to read some files manually; Byte [] data = new byte [fs.Length]; Fs.Read (data, 0, data length); System.IO.MemoryStream ms = New System.IO.MemoryStream (Data); System.Drawing.Image image = Image.FromStream (MS);  

Or save the temporary copy of the file copy by loading the image from the file?


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -