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

前端 未结 30 1377
再見小時候
再見小時候 2020-11-22 06:47

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 ge

相关标签:
30条回答
  • 2020-11-22 07:02

    Just in case if someone is doing as stupid stuff as I was. 1. make sure path does exist. 2. make sure you have permissions to write. 3. make sure your path is correct, in my case I was missing file name in the TargetPath :(

    it should have said, your path sucks than "A generic error occurred in GDI+"

    0 讨论(0)
  • 2020-11-22 07:07

    For me I was using the Image.Save(Stream, ImageCodecInfo, EncoderParameters) and apparently this was causing the infamous A generic error occurred in GDI+ error.

    I was trying to use EncoderParameter to save the jpegs in 100% quality. This was working perfectly on "my machine" (doh!) and not on production.

    When I used the Image.Save(Stream, ImageFormat) instead, the error disappeared! So like an idiot I continued to use the latter although it saves them in default quality which I assume is just 50%.

    Hope this info helps someone.

    0 讨论(0)
  • 2020-11-22 07:07

    I encountered the problem too. The problem was due to the loading stream being disposed. But I did not dispose it, it was inside .Net framework. All I had to do was use:

    image_instance = Image.FromFile(file_name);
    

    instead of

    image_instance.Load(file_name);
    

    image_instance is of type System.Windows.Forms.PictureBox! PictureBox's Load() disposes the stream which the image was loaded from, and I did not know that.

    0 讨论(0)
  • 2020-11-22 07:08

    Error occurring because of Permission. make sure folder have ALL THE PERMISSION.

    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")
    
    0 讨论(0)
  • 2020-11-22 07:08

    One other cause of this error and that solve my problème is that your application doesn't have a write permission on some directory.

    so to complete the answer of savindra : https://stackoverflow.com/a/7426516/6444829.

    Here is how you Grant File Access to IIS_IUSERS

    To provide access to an ASP.NET application, you must grant access to the IIs_IUSERS.

    To grant read, write, and modify permissions to a specific File or Folder

    1. In Windows Explorer, locate and select the required file.

    2. Right click the file, and then click Properties.

    3. In the Properties dialog box, click the Security tab.

    4. 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.

    5. In the Properties dialog box, click IIs_IUSERS, and in the Permissions for NETWORK SERVICE section, select the Read, Write, and Modify permissions.

    6. Click Apply, and then click OK.

    this worked for me in my IIS of windows server 2016 and local IIS windows 10.

    0 讨论(0)
  • 2020-11-22 07:09

    If you are trying to save an image to a remote location be sure to add the NETWORK_SERVICE user account into the security settings and give that user read and write permissions. Otherwise it is not going to work.

    0 讨论(0)
提交回复
热议问题