We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

rondre
rondre asked
on
Medium Priority
1,949 Views
Last Modified: 2012-05-07
I'm getting the following error:

<hr>

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=2.0.0.0, 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=2.0.0.0, 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
<hr>

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.

Thanks!
// From aspx file
 
			<div id="welcome">
				<h2>Success!</h2>
				<p>Your image was successfully uploaded!</p>
				<table width="920" border="0">
				<tr>
				<td width="720">
                    <img src="Pictures/<% Response.Write(Convert.ToString(Request.QueryString["fn"])); %>" /></td>
				<td>
				Tools:
				<ul>
				    <li>resize</li>
				</ul>
				</td>
				</tr>
				</table>
			</div>
 
// 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
            }
            else
            {
                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
        bmp.Save(dest);
 
        //- dispose / release resources
        bmp.Dispose();
        image.Dispose(); 
        */
    }
}

Open in new window

Comment
Watch Question

Is this happening on your production server or local machine?

Author

Commented:
This is happening on a GoDaddy server that is hosting my page.
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.
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

Commented:
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.

Thanks,
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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

thank you so much!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.