'System.Security.Permissions.FileIOPermission, mscorlib, Version= Error

Posted on 2009-07-08
Medium Priority
Last Modified: 2012-05-07
I'm getting the following error:


Server Error in '/' Application.

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:

Line 25:         String dest="./Pictures/" + imgname; //absolute location of the new image created(thumbnail)
Line 26:         Response.Write(dest + "<br />" + src);
Line 27:         System.Drawing.Image image = System.Drawing.Image.FromFile("./OriginalPics/26 weeks_2.JPG");
Line 28: /*
Line 29:         //- Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.

Source File: d:\hosting\3520755\html\modifypicture.aspx.cs    Line: 27

Stack Trace:

[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessPermission.Demand() +59
   System.Drawing.IntSecurity.DemandReadFileIO(String fileName) +68
   System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +398806
   System.Drawing.Image.FromFile(String filename) +6
   modifypicture.Page_Load(Object sender, EventArgs e) in d:\hosting\3520755\html\modifypicture.aspx.cs:27
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7350
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +213
   System.Web.UI.Page.ProcessRequest() +86
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +18
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.modifypicture_aspx.ProcessRequest(HttpContext context) in App_Web_xau97sfk.0.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

I'm new to asp.net and not sure what this error means.  I'm trying to inevitably resize an image that is passed to the page from a querystring and am running into this error.  You can see the logic I'm trying to follow through the commented out code.

Right now it doesn't like the line:
System.Drawing.Image image = System.Drawing.Image.FromFile("./OriginalPics/26 weeks_2.JPG");

which was originally displayed as:
System.Drawing.Image image = System.Drawing.Image.FromFile(src);

If there is a better way to do this i'm open to suggestions.

// From aspx file
			<div id="welcome">
				<p>Your image was successfully uploaded!</p>
				<table width="920" border="0">
				<td width="720">
                    <img src="Pictures/<% Response.Write(Convert.ToString(Request.QueryString["fn"])); %>" /></td>
// From cs codebehind file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
//using System.Drawing.Imaging;
using System.Drawing.Drawing2D; 
public partial class modifypicture : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        int thumbWidth = 640;  //Max width allowed
        int thumbHeight = 420; //Max height allowed
        decimal heightdiff;
        decimal widthdiff;
        string imgloc;
        string imgname = Convert.ToString(Request.QueryString["fn"]);
        imgloc = "./OriginalPics/" + imgname;
        String src = imgloc; //absolute location of source image
        String dest="./Pictures/" + imgname; //absolute location of the new image created(thumbnail) 
        Response.Write(dest + "<br />" + src);
        System.Drawing.Image image = System.Drawing.Image.FromFile("./OriginalPics/26 weeks_2.JPG");
        //- Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
        int srcWidth=image.Width;
        int srcHeight=image.Height;
        if (srcWidth > thumbWidth)  //example sw = 800  tw = 640  
            widthdiff = srcWidth/thumbWidth;  // wd = 1.25
                if(srcHeight > thumbHeight)  //example sh = 500 th = 420
                    heightdiff = srcHeight/thumbHeight;  //hd = 1.19
                        if(widthdiff > heightdiff)  // 1.25 > 1.19
                            thumbHeight = Convert.ToInt32(srcHeight/widthdiff);  // 800 div 1.25  = 640
                            thumbWidth = Convert.ToInt32(srcWidth/widthdiff);    // 500 div 1.25  = 400
                        else  // say hd was 1.28
                            thumbHeight = Convert.ToInt32(srcHeight/heightdiff); //800 div 1.28
                            thumbWidth = Convert.ToInt32(srcWidth/heightdiff);   //500 div 1.28
                else  // ex sw = 800 sh = 400
                    thumbHeight = Convert.ToInt32(srcHeight/widthdiff);  // 800 div 1.25  = 640
                    thumbWidth = Convert.ToInt32(srcWidth/widthdiff);    // 400 div 1.25  = 320
        else  //example sw = 600
            if(srcHeight > thumbHeight)  // sh = 450
                heightdiff = srcHeight/thumbHeight;  // wd = 1.07
                thumbHeight = Convert.ToInt32(srcHeight/heightdiff);  // 600 div 1.07  = 560
                thumbWidth = Convert.ToInt32(srcWidth/heightdiff);    // 450 div 1.07  = 420
                thumbHeight = srcHeight;
                thumbWidth = srcWidth;
        Response.Write(thumbHeight + " = thumbheight<br />" + thumbWidth + " = thumbWidth<br />" + dest 
            + " = dest<br />" + imgloc + " = imgloc<br />" + src + " = src<br />");
        Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
        //- Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        //- Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ;
        //- Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //- Set the System.Drawing.Graphics object property InterpolationMode to High
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //- Draw the original image into the target Graphics object scaling to the desired width and height
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
        //- Save to destination file
        //- dispose / release resources

Open in new window

Question by:rondre
  • 4
  • 3
LVL 22

Expert Comment

ID: 24813490
Is this happening on your production server or local machine?

Author Comment

ID: 24813734
This is happening on a GoDaddy server that is hosting my page.
LVL 22

Expert Comment

ID: 24813953
It seems to me that your asp.net application does not have enough permission to read a file from a folder. In this case, the folder is "OriginalPics". You should contact GoDaddy's technical support to get permission for your application.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 22

Expert Comment

ID: 24813972
Also, if you need to save the resized image, then you will need to make sure your application has "write" permission to the destination folder.

Author Comment

ID: 24814946
there are 3 total folders that I have given read and write permissions to from the control panel in godaddy.

OriginalPics is the folder that the image is uploaded to - this works fine.  From the upload page I then re-direct to a modifypicture page where I was planning on having tools to mess w/ the image.  The re-direct passes the name of the uploaded file through the url and I'm trying to resize the uploaded image from the previous page and store it in a Pictures folder.  I'm also going to have a 3rd folder called thumbnails as well.  The site is basically going to be for a landscape business where they can upload a picture to have the original size in the OriginalPics folder so they can access that if they need a large image.  There is a page that will use some javascript to do a slideshow and as you'll see from the code the image window will allow a max image size of 640x420 so i have thumbnail sizing (currently just for the 640x420 sizing but will do it for the small thumbnails also).

Hopefully this info will help - do you think that the "permissions" info from the GoDaddy control panel in their "folder options" section is not enough?  I can get a screenshot of that if that will help.

LVL 22

Accepted Solution

prairiedog earned 2000 total points
ID: 24815029
The control panel should set the right permission.

Another thing:

try using an absolute path in System.Drawing.Image.FromFile, instead of a relative path. You can get the actually file path by using Server.MapPath(YourRelativePath), maybe it will fix the error.

Author Comment

ID: 24816284
You rock prairiedog - had to apply that to both src and dest but the process works perfect now!

thank you so much!

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question