"Parameter is not valid. At System.Drawing.Bitmap..Ctor (Stream stream)."

Mahe picture Mahe · Feb 7, 2013 · Viewed 15.4k times · Source

I am getting the "Parameter is not valid.at System.Drawing.Bitmap..ctor(Stream stream)" in my code.

I am using the Following lines in my code,

System.Drawing.Bitmap image = new System.Drawing.Bitmap(fileUpload1.PostedFile.InputStream);

I don't find anything wrong in this code.

Answer

Marc Gravell picture Marc Gravell · Feb 7, 2013

In some cases, Bitmap requires a seekable stream. Try:

Bitmap image;
using(var ms = new MemoryStream()) {
    fileUpload1.PostedFile.InputStream.CopyTo(ms);
    ms.Position = 0;
    image = new System.Drawing.Bitmap(ms);
}

However. I must also note that this looks like ASP.NET; System.Drawing is not supported in ASP.NET: see here

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.