locked
Permission issue writing image to folder? - a generic error occurred in GDI+. RRS feed

  • Question

  • User379720387 posted

    I have an image created from a base64 array which I would like to save as a picture to \Images\Clients\

    Image convertToImage = Image.FromStream(new MemoryStream(Convert.FromBase64String(base64EncodedFile)));
                            convertToImage.Save(@"~/" + imagePath + ext, System.Drawing.Imaging.ImageFormat.Png);

    I have used an online decoder for base64 on base64EncodedFile and it has no problems reproducing the pictures

    This is the stack trace:

    at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
    at System.Drawing.Image.Save(String filename, ImageFormat format)
    at ASP._Page_Members_EntryVNEW_cshtml.Execute() in c:\Users\Rwavemaster\source\Repos\VT\Website\Members\EntryVNEW.cshtml:line 1184
    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
    at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors)
    at System.Web.WebPages.WebPage.ExecutePageHierarchy()
    at System.Web.WebPages.StartPage.RunPage()
    at System.Web.WebPages.StartPage.ExecutePageHierarchy()
    at System.Web.WebPages.StartPage.RunPage()
    at System.Web.WebPages.StartPage.ExecutePageHierarchy()
    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
    at System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext)

    This is IIS Express and user wavemaster has R/W permissions to the Images folder.

    Any suggestions?

    Wednesday, July 11, 2018 1:09 AM

Answers

  • User-1171043462 posted

      convertToImage.Save(@"~/" + imagePath + ext, System.Drawing.Imaging.ImageFormat.Png);

    Try

      convertToImage.Save(Server.MapPath(@"~/" + imagePath + ext), System.Drawing.Imaging.ImageFormat.Png);

    Already I have suggested a working solution without using Image class in my Reply

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 7:50 PM

All replies

  • User1120430333 posted

    I have an image created from a base64 array which I would like to save as a picture to \Images\Clients\

    I believe the issue is that your user account doesn't' have the permissions to write to the folder, since you are using IIS Express.

    If you deploy to IIS, then you'll have the same issue again, becuase all ASP.NET applications on IIS run under the context of the ASP.NET Worker Process account , that services the Application Pool and it is hosting the ASP.NET program.

    http://codetunnel.com/how-to-debug-your-aspnet-projects-running-under-iis/

    https://stackify.com/w3wp-exe-iis-worker-process/

    Myself, I never fool with IIS Express and always use local IIS to run the code under VS, since the code must eventually run on IIS and not IIS Express. So I might as well expose any problems that IIS Express might hide that get exposed when program is deployed to IIS.

     

    Wednesday, July 11, 2018 3:36 AM
  • User379720387 posted

    I checked and my wavemaster account has r/w permissions to that folder.

    Wednesday, July 11, 2018 12:26 PM
  • User1120430333 posted

    I checked and my wavemaster account has r/w permissions to that folder.

    I don't know what a wavemaster account is, but it is not the user account that Visual Stuido and IIS Express are running under I don't think, which would be the logged on user account on the machine. And even if you're logged on as admin on Vista to Win10, you are only a Standard user at the desktop with Standard user rights with UAC enabled until such time  that UAC escalates your account to be an admin account. That also would be that you started Vistal Stuido with 'Run as Administrator' for VS to have admin rights.

    Wednesday, July 11, 2018 3:18 PM
  • User379720387 posted

    wavemaster is the local admin account under Users/

    Even if I run VS2017 as administrator I still get the same error message.

    Wednesday, July 11, 2018 3:27 PM
  • User1120430333 posted

    wavemaster is the local admin account under Users/

    Even if I run VS2017 as administrator I still get the same error message.

    1) Is this wavemaster the account you used to login on the machine? If it is not the logged in user account, then wavemaster is not being used.

    2) Even with an admin account with full rights on a resource, another account can supersede  admin rights on the resource, like Users/machinename(users) with lessor rights will supersede admin rights on the resource.  

    Wednesday, July 11, 2018 3:53 PM
  • User-1171043462 posted

      convertToImage.Save(@"~/" + imagePath + ext, System.Drawing.Imaging.ImageFormat.Png);

    Try

      convertToImage.Save(Server.MapPath(@"~/" + imagePath + ext), System.Drawing.Imaging.ImageFormat.Png);

    Already I have suggested a working solution without using Image class in my Reply

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 7:50 PM
  • User379720387 posted

    Server.MapPath did the trick.

    Thank you.

    What is the benefit of not using the Image class?

    Wednesday, July 11, 2018 8:16 PM
  • User-1171043462 posted

    There is no benefit. I prefer easiest and simple coding.

    The less code the less processing work for your CPU.

    Since you just want to save an Image File, you can use the File class.

    Wednesday, July 11, 2018 8:19 PM