Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is correct, it will not work. The problem is that you are crossing a managed/unmanaged boundary, I recently encountered the same. Other problems are that the stream is not directly there and the Image.FromStream has no idea how to deal with it.</p> <p>The solution is quite straightforward: read everything from PostedFile into a MemoryStream (just use <code>new MemoryStream()</code>) and use the MemoryStream with the <code>Image.FromStream</code>. This will solve your problem.</p> <p>Make sure to make proper use of <code>using</code> when you work with <code>Image</code>, <code>Graphics</code> and <code>Stream</code>s. All of them implement the IDisposable and in an ASP.NET environment, not using <code>using</code> blocks properly, can and will lead to increased memory usage and other nasty side effect on the long run (and ASP.NET apps do run very long!).</p> <p>The solution should look something like this:</p> <pre><code>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. } } </code></pre> <p><strong>Update:</strong> the crossing managed boundary issue only occurs in the reverse (using Response stream), it seems, not with Upload streams, but I'm not entirely sure.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload