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

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


当前回答

由于权限而发生错误。确保文件夹有所有的权限。

public Image Base64ToImage(string base64String)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        Image image = Image.FromStream(ms, true);
        return image;
    }

 img.Save("YOUR PATH TO SAVE IMAGE")

其他回答

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

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

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

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

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

我对这个问题有一个奇怪的解决办法。 我在编码时遇到过这种情况。 我认为位图是结构和环绕它的方法。 在我的想象中,位图将在方法内复制,并在方法外返回。 但后来我检查了一下,它是类,我不知道为什么它帮助我,但它是有效的! 也许有人有时间和乐趣看看这个的IL代码;) 不确定它是否有效,因为这个方法是静态方法中的静态方法, 我不知道。

        public SomeClass
        {
            public byte[] _screenShotByte;
            public Bitmap _screenShotByte;
            public Bitmap ScreenShot 
            { 
                get
                {
                    if (_screenShot == null)
                    {
                        _screenShotByte = ScreenShot();
                        using (var ms = new MemoryStream(_screenShotByte))
                        {
                            _screenShot = (Bitmap)Image.FromStream(ms);
                        }
                         
                        ImageUtils.GetBitmap(_screenShot).Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory) ,$"{DateTime.Now.ToFileTimeUtc()}.png"));
            
                    }
                    return ImageUtils.GetBitmap(_screenShot);
                }
            }
            public byte[] ScreenShot()
            {
                ///....return byte array for image in my case implementedd like selenium screen shot 
            }
        }
        public static ImageUtils
        {
            public static Bitmap GetBitmap(Bitmap image)
            {
                return Bitmap;
            }
        }


附:这不是喷子这个解决方案解决的问题,保持使用位图后保存在另一个地方。

在我的情况下,问题是在我保存的路径(根C:\)。把它改成D:\111\,异常就消失了。

我注意到你的“jpeg”案例实际上是:

            default:
                format = ImageFormat.Jpeg;
                break;

您确定格式是jpeg而不是其他格式吗?

我试一试:

            case "image/jpg": // or "image/jpeg" !
                format = ImageFormat.Jpeg;
                break;

或者检查imageToConvert.MimeType()实际返回的内容。

更新

你是否需要对MemoryStream对象进行其他初始化?