Image.FromStream (Added by File.InputStream) Does not work. (The parameter is not valid.) (AsyncFileUpload))

I am using AsyncFileUpload (AJAX Toolkit) to upload images. I have a button that handles image resizing. This has been working fine for some time, but nothing more ...

protected void BtnUploadImage_Click(object sender, EventArgs e) { var imageFileNameRegEx = new Regex(@"(.*?)\.(jpg|jpeg|png|gif)$", RegexOptions.IgnoreCase); if (!AsyncFileUpload1.HasFile || !imageFileNameRegEx.IsMatch(AsyncFileUpload1.FileName)) { AsyncFileUpload1.FailedValidation = true; ErrorLabel.Visible = true; return; } ErrorLabel.Visible = false; var file = AsyncFileUpload1.PostedFile.InputStream; var img = Image.FromStream(file, false, false); ... } 

Another thing that I find strange: if I try an image smaller than 80 kB, it will work.!

We tried to restart the server, but no changes. The same code works fine on my machine. (heard that before ?? :))

I also tried to save the file on the server and then get the file through Image.FromFile (), but then I get "Can't access the private file."

How to resolve this?

+5
c # upload image asp.net-ajax ajaxcontroltoolkit
source share
2 answers

I would like to make sure the thread is at the beginning:

 var file = AsyncFileUpload1.FileContent; file.Seek(0, SeekOrigin.Begin); var img = Image.FromFile(file); 

The second thing to check: setting requestLengthDiskThreshold . If not specified, this parameter has a default value ... yes, 80 KB.

Note. imo there should be no general difference whether you use an image to directly read a file stream or if you use an intermediate MemoryStream (except that in the latter case you actually load the entire file into memory twice). In any case, the original file stream will be considered, therefore the position of the stream, CAS rights, file permissions, etc. Still applicable.

Note2: and yes, be sure to ensure that these resources are located correctly.

+7
source share

This is correct, this will not work. The problem is that you are crossing a managed / unmanaged border, I recently ran into it. Other problems are that the stream is not right there, and Image.FromStream has no idea how to deal with this.

The solution is quite simple: read everything from FileFile into a MemoryStream (just use new MemoryStream() ) and use a MemoryStream with Image.FromStream . This will solve your problem.

Be sure to use using when working with Image , Graphics and Stream s. All of them implement IDisposable in the ASP.NET environment, without using the correct using blocks, can and will lead to an increase in the use of memory and other unpleasant side effects in the long run (and ASP.NET applications work for a very long time!).

The solution should look something like this:

 using(Stream memstr = new MemoryStream()) { // copy to a memory stream Stream uploadStream = AsyncFileUpload1.PostedFile.InputStream; byte[] all = new byte[uploadStream.Length]; uploadStream.Read(all, 0, uploadStream.Length); memstr.Write(all, 0, uploadStream.Length); memstr.Seek(0, SeekOrigin.Begin); using(Graphics g = Graphics.FromStream(memstr)) { // do your img manipulation, or Save it. } } 

Update: The problem with the controlled intersection border occurs only in the reverse order (using the response stream), it seems not with the download streams, but I'm not quite sure.

+1
source share

All Articles