C# 保存上载的图像时出现问题

C# 保存上载的图像时出现问题,c#,asp.net-mvc,C#,Asp.net Mvc,这是我的第一个问题。感谢所有为这个网站做贡献的人,这对我自学编程是一个巨大的帮助 我在保存上传的图像时遇到问题。我想我的image.save功能有问题。我的代码如下: Server Error in '/' Application. A generic error occurred in GDI+. Description: An unhandled exception occurred during the execution of the current web request. P

这是我的第一个问题。感谢所有为这个网站做贡献的人,这对我自学编程是一个巨大的帮助

我在保存上传的图像时遇到问题。我想我的image.save功能有问题。我的代码如下:

    Server Error in '/' Application.
A generic error occurred in GDI+.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Source Error:

Line 51:             
Line 52:             string filePath = @"c:\test.jpg";
Line 53:             outfitImage.Save(filePath);
Line 54: 
Line 55:             return View();


Source File: C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs    Line: 53

Stack Trace:

[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +377630
   System.Drawing.Image.Save(String filename, ImageFormat format) +69
   System.Drawing.Image.Save(String filename) +25
   AmIStylin.Controllers.TestController.Index(String fileName, HttpPostedFileBase pic) in C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs:53
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +205
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
   System.Web.Mvc.Controller.ExecuteCore() +126
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Version Information: Microsoft .NET Framework Version:2.0.50727.4062; ASP.NET Version:2.0.50727.3074 
控制器:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(string fileName, HttpPostedFileBase pic)
    {
        string mimeType = pic.ContentType;
        byte[] imagedata = new byte[pic.ContentLength];
        pic.InputStream.Read(imagedata, 0, pic.ContentLength);
        Image outfitImage = (Image.FromStream(new MemoryStream(imagedata)));
        string filePath = @"c:\test.jpg";
        outfitImage.Save(filePath);
        return View();
    }
视图:


领域

姓名:

上载新图像:

堆栈跟踪如下所示:

    Server Error in '/' Application.
A generic error occurred in GDI+.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Source Error:

Line 51:             
Line 52:             string filePath = @"c:\test.jpg";
Line 53:             outfitImage.Save(filePath);
Line 54: 
Line 55:             return View();


Source File: C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs    Line: 53

Stack Trace:

[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +377630
   System.Drawing.Image.Save(String filename, ImageFormat format) +69
   System.Drawing.Image.Save(String filename) +25
   AmIStylin.Controllers.TestController.Index(String fileName, HttpPostedFileBase pic) in C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs:53
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +205
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
   System.Web.Mvc.Controller.ExecuteCore() +126
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Version Information: Microsoft .NET Framework Version:2.0.50727.4062; ASP.NET Version:2.0.50727.3074 
“/”应用程序中出现服务器错误。 GDI+中发生一般性错误。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:System.Runtime.InteropServices.ExternalException:GDI+中发生一般错误。 源错误: 第51行: 第52行:字符串filePath=@“c:\test.jpg”; 第53行:outfighimage.Save(文件路径); 第54行: 第55行:返回视图(); 源文件:C:\Users\Solomon\SolomonApp\amistylen\amistylen\Controllers\TestController.cs行:53 堆栈跟踪: [外部异常(0x80004005):GDI+中发生一般错误。] System.Drawing.Image.Save(字符串文件名、ImageCodeInfo编码器、EncoderParameters encoderParams)+377630 System.Drawing.Image.Save(字符串文件名,ImageFormat格式)+69 System.Drawing.Image.Save(字符串文件名)+25 C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.Index(字符串文件名,HttpPostedFileBase pic)中的AmIStylin.Controllers.TestController.Index lambda_方法(ExecutionScope,ControllerBase,Object[])+205 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+17 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)+178 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)+24 System.Web.Mvc.c__显示ClassA.b__7()+52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)+254 System.Web.Mvc.c__显示类c.b__9()+19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList`1筛选器,ActionDescriptor ActionDescriptor,IDictionary`2参数)+192 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+399 System.Web.Mvc.Controller.ExecuteCore()+126 System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+27 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext RequestContext)+7 ProcessRequest(HttpContextBase httpContext)+151 ProcessRequest(HttpContext HttpContext)+57 System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext HttpContext)+7 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+181 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+75 版本信息:Microsoft.NET Framework版本:2.0.50727.4062;ASP.NET版本:2.0.50727.3074
非常感谢你的帮助

您正在尝试将图像转换为JPEG格式吗

如果没有,您只需调用
pic.Save(@“C:\text.jpg”)

您可能会收到错误,因为ASP.Net没有对C:驱动器根目录的写入权限。(GDI+可以给出有趣的错误)
这段代码是如何运行的,在哪里运行的?(卡西尼?IIS?

看看这篇文章


我只想找出相对于web服务器的存储位置,将其转换到文件服务器,然后保存它

string relativePath = "~/somefolder/somefile.ext";
string diskPath = ControllerContext.HttpContext.Server.MapPath(newFileWebPath);
pic.SaveAs(diskPath);

您应该在循环中调用
pic.InputStream.Read
,因为不能保证一次读取所有内容。(检查它的返回值)谢谢。我该怎么做?这对他毫无帮助。除非他想把它转换成jpeg。谢谢!我的托管提供程序(rackspace)是中等信任的,我需要使用绝对路径,而不是相对路径,尽管在部署…
Server时。MapPath
返回绝对路径。假设您对该路径具有写访问权限,则此代码应该可以工作。(你可能不知道)谢谢!我尝试对文件路径进行硬编码,但得到了相同的错误。代码正在我的本地机器上运行(VisualStudio中很好的旧F5),所以我认为它是IIS。我希望图片是JPEG格式。如果在VS中按F5,则运行的是Cassini。请尝试将其更改为
pic.Save(@“C:\text.jpg”)
,然后查看是否出现错误。SLaks,您是对的,这是权限问题。我把我的应用程序的一个子目录改成了filepath,它成功了。非常感谢!