[Solved-2 Solutions] A generic error occurred in GDI+, JPEG Image to MemoryStream



Error Description:

    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();
        }
    }
    
    click below button to copy the code. By - C# tutorial - team

    We get this error:

    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: 
    
    click below button to copy the code. By - C# tutorial - team

    Solution 1:

      • If we resize the image and as part of that method we return the resized object as follows. we have to insert 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;
       }
      
      click below button to copy the code. By - C# tutorial - team

      Solution 2:

        • If you are getting that error , then I can say that your application doesn't have a write permission on some directory.
        • For example, if you are trying to save the Image from the memory stream to the file system , you may get that error.
        • If you are using XP, make sure to add write permission for the aspnet account on that folder.
        • If you are using windows server (2003,2008) or Vista, make sure that add write permission for the Network service account.

        Related Searches to A generic error occurred in GDI+, JPEG Image to MemoryStream