我正在收到用户的图片并希望保存。 最初我做
Stream file = Request.Files[0].InputStream;
然后执行保存,其中
传递上一步中的file
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
异常消息:A generic error occurred in GDI+
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
已经提到了其他问题,其中必须创建新流并保持打开但在我的情况下,我已经有一个输入流。我该如何解决这个问题?
答案 0 :(得分:1)
我遇到了同样的问题。我尝试了不同的编码器参数,不同的路径,但抛出相同的异常。我首先将图像保存到内存流然后保存到文件流。 这是一个片段。
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);
}