문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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);

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top