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

17,255

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.

Share:
17,255

Related videos on Youtube

Mahe
Author by

Mahe

Updated on June 04, 2022

Comments

  • Mahe
    Mahe almost 2 years

    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.

  • Mahe
    Mahe about 11 years
    but, What is the Problem in my code? Actually that should work. could u please elaborate the reason for not working.
  • Marc Gravell
    Marc Gravell about 11 years
    @Mahe the first thing to do is try it; if it works, then the answer is "it didn't like HttpInputStream" for some reason that it isn't telling us. Of course, another possibility is that the incoming data is not actually an image (or not a supported image). Re "that should work": no, that is completely false: see the warning I copied at the bottom of my post: there is explicitly zero expectation that Bitmap works in ASP.NET, although generally it does (and a lot of people do use it there). Bitmap uses GDI/GDI+; and that is not technically supported outside of desktop UI code.