简体   繁体   English

“参数无效。 位于System.Drawing.Bitmap..Ctor(流流)。”

[英]“Parameter is not valid. At System.Drawing.Bitmap..Ctor (Stream stream).”

I am getting the "Parameter is not valid.at System.Drawing.Bitmap..ctor(Stream stream)" in my code. 我在代码中收到“参数无效。在System.Drawing.Bitmap..ctor(Stream流)”。

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. 在某些情况下, Bitmap需要可搜索的数据流。 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; 我还必须注意,这看起来像ASP.NET。 System.Drawing is not supported in ASP.NET: see here ASP.NET 不支持 System.Drawing请参见此处

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Windows或ASP.NET服务中不支持使用System.Drawing命名空间中的类。 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. 有关支持的替代方法,请参见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. 我在使用相对路径时遇到了类似的问题,但是我忘记将图像文件设置为要复制到bin目录,因此出现了同样的错误。 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#System.ArgumentException:参数无效。 在System.Drawing.Bitmap..ctor(Stream stream) - C# System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap..ctor(Stream stream) 参数无效。 System.Drawing.Bitmap..ctor(流流)处的堆栈跟踪 - Parameter is not valid. Stack Trace at System.Drawing.Bitmap..ctor(Stream stream) 我收到此异常“ System.Drawing.Bitmap..ctor(Int32宽度,Int32高度,PixelFormat格式)中的参数无效。” - i am getting this Exception “Parameter is not valid.at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)” 从Stream转换System.Drawing.Image-参数无效 - Convert System.Drawing.Image from Stream — Parameter not valid System.Drawing.Bitmap参数无效 - System.Drawing.Bitmap Parameter is not valid 位图-ArgumentException:“参数无效。”(间歇性) - Bitmap - ArgumentException: “Parameter is not valid.” (intermittent) 使用保存位图时,“参数无效。” - “Parameter is not valid.” when using saving bitmap System.ArgumentException HResult = 0x80070057 消息 = 参数无效。 来源 = System.Drawing。 为什么? - System.ArgumentException HResult = 0x80070057 Message = Parameter is not valid. Source = System.Drawing. Why? 参数在内存流中无效 - Parameter is not valid in memory stream 参数无效。 位图()和NReco.VideoConverter.FFMpegConverter() - Parameter is not valid. Bitmap() and NReco.VideoConverter.FFMpegConverter()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM