GDI+, JPEG Image to MemoryStream 中发生一般错误 [英] A generic error occurred in GDI+, JPEG Image to MemoryStream

查看:24
本文介绍了GDI+, JPEG Image to MemoryStream 中发生一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在网络上似乎是一个臭名昭著的错误.以至于我一直无法找到我的问题的答案,因为我的场景不适合.当我将图像保存到流时抛出异常.

This seems to be a bit of an infamous error all over the web. So much so that I have been unable to find an answer to my problem as my scenario doesn't fit. An exception gets thrown when I save the image to the stream.

奇怪的是,这与 png 完美配合,但在 jpg 和 gif 时出现上述错误,这很令人困惑.

Weirdly this works perfectly with a png but gives the above error with jpg and gif which is rather confusing.

大多数类似的问题都与在没有权限的情况下将图像保存到文件有关.具有讽刺意味的是,解决方案是像我一样使用内存流......

Most similar problem out there relate to saving images to files without permissions. Ironically the solution is to use a memory stream as I am doing....

public static byte[] ConvertImageToByteArray(Image imageToConvert)
{
    using (var ms = new MemoryStream())
    {
        ImageFormat format;
        switch (imageToConvert.MimeType())
        {
            case "image/png":
                format = ImageFormat.Png;
                break;
            case "image/gif":
                format = ImageFormat.Gif;
                break;
            default:
                format = ImageFormat.Jpeg;
                break;
        }

        imageToConvert.Save(ms, format);
        return ms.ToArray();
    }
}

有关异常的更多详细信息.这导致这么多问题的原因是缺乏解释:(

More detail to the exception. The reason this causes so many issues is the lack of explanation :(

System.Runtime.InteropServices.ExternalException was unhandled by user code
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
   at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters    encoderParams)
   at System.Drawing.Image.Save(Stream stream, ImageFormat format)
   at Caldoo.Infrastructure.PhotoEditor.ConvertImageToByteArray(Image imageToConvert) in C:UsersIanSVNCaldooCaldoo.CoordinatorPhotoEditor.cs:line 139
   at Caldoo.Web.Controllers.PictureController.Croppable() in C:UsersIanSVNCaldooCaldoo.WebControllersPictureController.cs:line 132
   at lambda_method(ExecutionScope , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
 InnerException: 

到目前为止我已经尝试过的东西.

OK things I have tried so far.

  1. 克隆图像并进行处理.
  2. 使用 jpeg 质量设置检索该 MIME 的编码器.

推荐答案

如果您收到该错误,那么我可以说您的应用程序对某个目录没有写权限.

If you are getting that error , then I can say that your application doesn't have a write permission on some directory.

例如,如果您尝试将 Image 从内存流保存到文件系统,您可能会遇到该错误.

For example, if you are trying to save the Image from the memory stream to the file system , you may get that error.

如果您使用的是 XP,请确保为该文件夹中的 aspnet 帐户添加写入权限.

Please if you are using XP, make sure to add write permission for the aspnet account on that folder.

如果您使用的是 windows server (2003,2008) 或 Vista,请确保为网络服务帐户添加写入权限.

If you are using windows server (2003,2008) or Vista, make sure that add write permission for the Network service account.

希望对大家有所帮助.

这篇关于GDI+, JPEG Image to MemoryStream 中发生一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆