3

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.

rene
  • 41,474
  • 78
  • 114
  • 152
Mahe
  • 2,707
  • 13
  • 50
  • 69

4 Answers4

3

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • but, What is the Problem in my code? Actually that should work. could u please elaborate the reason for not working. – Mahe Feb 07 '13 at 09:43
  • 1
    @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. – Marc Gravell Feb 07 '13 at 09:51
1

In my case, the folder where the file was located didn't give the application permission to access the file. By trying to read the file directly as a stream, an exception was reported that access was denied. Then I checked the security of the file and folder to discover neither had sufficient permissions to allow the application to read it by a different user. The original "parameter is not valid" error did not give enough information to diagnose the actual problem.

Suncat2000
  • 966
  • 1
  • 12
  • 15
0

this solved my problem.

    byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
    fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
ImageConverter imageConverter = new System.Drawing.ImageConverter();
System.Drawing.Image image = imageConverter.ConvertFrom(fileData) as System.Drawing.Image;
image.Save(imageFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
Savas Adar
  • 4,083
  • 3
  • 46
  • 54
0

Also, sometimes you get that invalid parameter when the file exists, but not at that location. I had a similar problem where I was using a relative path, but I forgot to set the image file to copy to the bin directory, so I got the same error. Just FYI

William
  • 491
  • 5
  • 9