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

奇怪的是,这适用于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质量设置。


当前回答

简单,创建一个新的位图实例就解决了这个问题。

string imagePath = Path.Combine(Environment.CurrentDirectory, $"Bhatti{i}.png");
Bitmap bitmap = new Bitmap(image);
bitmap.Save(imagePath);

其他回答

可能导致这种错误的问题有:

目录不存在(您正在调用的方法不会自动为您创建此目录) 写入输出目录的安全权限不允许运行应用程序的用户写入

我希望这有助于,这是修复我的问题,我只是确保输出目录存在之前保存输出图像!

保存图像到位图变量

using (var ms = new MemoryStream())
{
    Bitmap bmp = new Bitmap(imageToConvert);
    bmp.Save(ms, format);
    return ms.ToArray();
}

这个错误的另一个原因,解决我的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。

解决方法——我遇到了这个问题。对我来说,解决办法是提高IIS服务器上IUSR的磁盘配额。在本例中,我们有一个带有物品等图像的目录应用程序。“匿名Web用户”的上传配额被设置为100MB,这是该特定托管公司的IIS服务器的默认值。我把它升级到400MB,上传图片时没有出错。

这可能不是你的问题,但如果是的话,很容易解决。

好吧,我似乎已经找到了原因,只是纯粹的运气,它没有错与特定的方法,它进一步备份调用堆栈。

前面我调整了图像的大小,作为该方法的一部分,我返回调整后的对象,如下所示。我插入了对上述方法的两次调用,并直接保存到一个文件。

// At this point the new bitmap has no MimeType
// Need to output to memory stream
using (var m = new MemoryStream())
{
       dst.Save(m, format);

       var img = Image.FromStream(m);

       //TEST
       img.Save("C:\\test.jpg");
       var bytes = PhotoEditor.ConvertImageToByteArray(img);


       return img;
 }

在保存对象时,创建对象的内存流必须打开。我不知道为什么会这样。有没有人能给我点启发,告诉我怎么解决这个问题。

我只从流返回,因为使用类似于此的调整大小代码后,目标文件有一个未知的mime类型(img.RawFormat.Guid)和Id像mime类型是正确的所有图像对象,因为它很难编写通用处理代码,否则。

EDIT

这在我最初的搜索中没有出现,但这是乔恩·斯基特的答案