C# Image.Save(..)抛出一个GDI+;例外

C# Image.Save(..)抛出一个GDI+;例外,c#,gdi+,C#,Gdi+,我正在从用户处接收图像,并希望保存它。 起初我是这样做的 Stream file = Request.Files[0].InputStream; 然后在传入上一步中的文件的位置执行保存 using(var image = Image.FromStream(file)) { // Set the codec parameters with another method. No Stream involved image.Save(filename, codecInfo, code

我正在从用户处接收图像,并希望保存它。 起初我是这样做的

Stream file = Request.Files[0].InputStream;
然后在传入上一步中的
文件的位置执行保存

using(var image = Image.FromStream(file)) {
    // Set the codec parameters with another method. No Stream involved
    image.Save(filename, codecInfo, codeParam); // Throws GDI+ exception
}
异常类型为:
System.Runtime.InteropServices.ExternalException

异常消息:
GDI+中出现一般错误。

堆栈跟踪:

at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)

我提到了其他问题,其中必须创建新的流并保持打开状态,但在我的情况下,我已经有了一个输入流。如何解决这个问题?

我也有同样的问题。我尝试了不同的编码器参数,不同的路径,但引发了相同的异常。对我来说,先将图像保存到memorystream,然后再保存到文件流是可行的。 这里有一个片段

   using(MemoryStream memoryStream = new MemoryStream())
   using(FileStream fileStream = File.Open(path, FileMode.OpenOrCreate))
   {
   image.Save(memoryStream, yourEncoder, yourEncoderParamaters);
   byte[] imgArray = memoryStream.ToArray();
   fileStream.Write(imgArray, 0, imgArray.Length);
   }

哪个GDI+异常?是否有消息?因此可能是
codeinfo
codeParam
导致了此问题。此异常在StackOverflow上被多次质疑,请查看有关此主题的旧问题。