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

查看:1042
本文介绍了GDI +中发生一般性错误,JPEG图像到的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:\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: 

确定的东西,到目前为止我都试过了。

OK things I have tried so far.


  1. 克隆图像并就这一工作。

  2. 检索EN codeR针对通过该MIME与JPEG画质设置。

请谁能帮助。

推荐答案

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

OK I seem to have found the cause just by sheer luck and its nothing wrong with that particular method, it's further back up the call stack.

早些时候,我调整图像大小,并作为方法的一部分,我回到了调整大小的对象,如下所示。我已插入两个调用上述方法和直接保存到文件中。

Earlier I resize the image and as part of that method I return the resized object as follows. I have inserted two calls to the above method and a direct save to a file.

// 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;
 }

看来,该对象上的创建的内存流有是在保存对象时打开。我不知道这是为什么。有没有人能赐教,以及如何我可以解决这个问题。

It appears that the memory stream that the object was created on has to be open at the time the object is saved. I am not sure why this is. Is anyone able to enlighten me and how I can get around this.

我只从流中返回,因为使用类似这个调整大小code后目标文件有一个未知的MIME类型(img.RawFormat.Guid)和身份证一样MIME类型为在所有图像对象正确的,因为它使得它很难编写通用的处理code除外。

I only return from a stream because after using the resize code similar to this the destination file has an unknown mime type (img.RawFormat.Guid) and Id like the Mime type to be correct on all image objects as it makes it hard write generic handling code otherwise.

修改

这没上来在我最初的搜索,但<一个href=\"http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed\">here's从乔恩斯基特答案

This didn't come up in my initial search but here's the answer from Jon Skeet

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

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