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

奇怪的是,这适用于png,但给出上述错误的jpg和gif,这是相当令人困惑的。

大多数类似的问题都与将图像保存到没有权限的文件有关。具有讽刺意味的是,解决方案是使用内存流,正如我所做的....

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();
    }
}

关于异常的更多细节。这导致这么多问题的原因是缺乏解释:(

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:\Users\Ian\SVN\Caldoo\Caldoo.Coordinator\PhotoEditor.cs:line 139
   at Caldoo.Web.Controllers.PictureController.Croppable() in C:\Users\Ian\SVN\Caldoo\Caldoo.Web\Controllers\PictureController.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: 

好的,到目前为止我已经试过了。

克隆图像并进行处理。 检索MIME的编码器,传递jpeg质量设置。


当前回答

这是Fred的回复的扩展/限定,他说:“GDI限制图像的高度为65534”。我们在一个。net应用程序中遇到了这个问题,看到这篇文章后,我们的外包团队举起双手说,如果不进行重大修改,他们无法解决这个问题。

根据我的测试,可以创建/操作高度大于65534的图像,但在以某些格式保存到流或文件时出现了问题。在下面的代码中,当像素高度为65501时,t.Save()方法调用会向我们的朋友抛出泛型异常。出于好奇,我重复了宽度测试,同样的限制适用于保存。

    for (int i = 65498; i <= 100000; i++)
    {
        using (Bitmap t = new Bitmap(800, i))
        using (Graphics gBmp = Graphics.FromImage(t))
        {
            Color green = Color.FromArgb(0x40, 0, 0xff, 0);
            using (Brush greenBrush = new SolidBrush(green))
            {
                // draw a green rectangle to the bitmap in memory
                gBmp.FillRectangle(greenBrush, 0, 0, 799, i);
                if (File.Exists("c:\\temp\\i.jpg"))
                {
                    File.Delete("c:\\temp\\i.jpg");
                }
                t.Save("c:\\temp\\i.jpg", ImageFormat.Jpeg);
            }
        }
        GC.Collect();
    }

如果写入内存流,也会发生相同的错误。

为了解决这个问题,你可以重复上面的代码,用ImageFormat.Tiff或ImageFormat.Bmp代替ImageFormat.Jpeg。

这对我来说达到了100,000的高度/宽度-我没有测试极限。碰巧。tiff对我们来说是一个可行的选择。

被警告

内存中的TIFF流/文件比它们的JPG对应文件消耗更多的内存。

其他回答

这个错误的另一个原因,解决我的problème是你的应用程序没有写权限的某些目录。

所以要完成savindra的回答:https://stackoverflow.com/a/7426516/6444829。

以下是授予IIS_IUSERS文件访问权的方法

提供对ASP的访问。NET应用程序时,必须将访问权限授予IIs_IUSERS。

为特定的文件或文件夹授予读、写和修改权限

In Windows Explorer, locate and select the required file. Right click the file, and then click Properties. In the Properties dialog box, click the Security tab. On the Security tab, examine the list of users. (If your application is running as a Network Service, add the network service account in the list and grant it the permission. In the Properties dialog box, click IIs_IUSERS, and in the Permissions for NETWORK SERVICE section, select the Read, Write, and Modify permissions. Click Apply, and then click OK.

这适用于我的IIS的windows server 2016和本地IIS windows 10。

这是Fred的回复的扩展/限定,他说:“GDI限制图像的高度为65534”。我们在一个。net应用程序中遇到了这个问题,看到这篇文章后,我们的外包团队举起双手说,如果不进行重大修改,他们无法解决这个问题。

根据我的测试,可以创建/操作高度大于65534的图像,但在以某些格式保存到流或文件时出现了问题。在下面的代码中,当像素高度为65501时,t.Save()方法调用会向我们的朋友抛出泛型异常。出于好奇,我重复了宽度测试,同样的限制适用于保存。

    for (int i = 65498; i <= 100000; i++)
    {
        using (Bitmap t = new Bitmap(800, i))
        using (Graphics gBmp = Graphics.FromImage(t))
        {
            Color green = Color.FromArgb(0x40, 0, 0xff, 0);
            using (Brush greenBrush = new SolidBrush(green))
            {
                // draw a green rectangle to the bitmap in memory
                gBmp.FillRectangle(greenBrush, 0, 0, 799, i);
                if (File.Exists("c:\\temp\\i.jpg"))
                {
                    File.Delete("c:\\temp\\i.jpg");
                }
                t.Save("c:\\temp\\i.jpg", ImageFormat.Jpeg);
            }
        }
        GC.Collect();
    }

如果写入内存流,也会发生相同的错误。

为了解决这个问题,你可以重复上面的代码,用ImageFormat.Tiff或ImageFormat.Bmp代替ImageFormat.Jpeg。

这对我来说达到了100,000的高度/宽度-我没有测试极限。碰巧。tiff对我们来说是一个可行的选择。

被警告

内存中的TIFF流/文件比它们的JPG对应文件消耗更多的内存。

我将加上这个错误的原因,希望它能帮助一些未来的互联网旅行者。:)

GDI+将图像的最大高度限制为65500

我们做了一些基本的图像调整,但在调整大小时,我们试图保持纵横比。我们有一个非常擅长这项工作的QA人员;他决定用一张宽为1像素、高为480像素的照片进行测试。当图像缩放到符合我们的尺寸时,高度超过了68,000像素,我们的应用程序爆炸了,在GDI+中出现了一个通用错误。

你可以自己用test来验证:

  int width = 480;
  var height = UInt16.MaxValue - 36; //succeeds at 65499, 65500
  try
  {
    while(true)
    {
      var image = new Bitmap(width, height);
      using(MemoryStream ms = new MemoryStream())
      {
        //error will throw from here
        image.Save(ms, ImageFormat.Jpeg);
      }
      height += 1;
    }
  }
  catch(Exception ex)
  {
    //explodes at 65501 with "A generic error occurred in GDI+."
  }

Bitmap的构造函数中没有抛出一个友好的。net ArgumentException,这太糟糕了。

如果你得到了这个错误,那么我可以说你的应用程序在某个目录上没有写权限。

例如,如果您试图将映像从内存流保存到文件系统,则可能会得到该错误。

如果您正在使用XP,请确保为该文件夹的aspnet帐户添加写权限。

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

希望它能帮助到一些人。

对我来说,我使用的是意象。保存(流,ImageCodecInfo, EncoderParameters),显然这导致了臭名昭著的GDI+错误中发生的通用错误。

我试图使用EncoderParameter保存100%质量的jpeg文件。这在“我的机器”上运行得很好,但在生产中却没有。

当我使用图像时。保存(流,ImageFormat)代替,错误消失!所以我像个白痴一样继续使用后者,尽管它将它们保存在默认质量(我假设只有50%)。

希望这些信息能帮助到一些人。