Ok, I found out the issue. Literally my host provider doesn't support full trust policy, so I had to change it to medium trust. Even if I set the full trust on my web.config, It'll just change it back to medium trust or display the error.
So, the error occured on my 'GetImage' function, which was
public static System.Drawing.Image GetImage(String fileName, System.Drawing.Size maxSize) {
System.Drawing.Image result = null;
try {
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
using (System.Drawing.Image img = System.Drawing.Image.FromStream(fs, true, false)) {
Size sz = GetProportionalSize(maxSize, img.Size);
result = img.GetThumbnailImage(sz.Width, sz.Height, null, IntPtr.Zero);
}
fs.Close();
}
}
catch (Exception ex) {
throw ex;
}
return result;
}
or
using (FileStream fs = File.OpenRead(fileName)) {
using (StreamReader sr = new StreamReader(fs)) {
using (System.Drawing.Image img = System.Drawing.Image.FromStream(sr.BaseStream, true, false)) {
System.Drawing.Size sz = GetProportionalSize(maxSize, img.Size);
result = img.GetThumbnailImage(sz.Width, sz.Height, null, IntPtr.Zero);
}
sr.Close();
}
fs.Close();
}
and throws...
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.CheckNReturnSO(PermissionToken permToken, CodeAccessPermission demand, StackCrawlMark& stackMark, Int32 unrestrictedOverride, Int32 create) at System.Security.CodeAccessSecurityEngine.Assert(CodeAccessPermission cap, StackCrawlMark& stackMark) at System.Security.CodeAccessPermission.Assert() at [Snipped Method Name] at ReportExprHostImpl.CustomCodeProxy.[Snipped Method Name] The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.SecurityPermission The Zone of the assembly that failed was: MyComputer
BUT, I still don't know WHY the following code WORKS
using (System.Drawing.Image img = System.Drawing.Image.FromFile(fileName)) {
System.Drawing.Size sz = GetProportionalSize(maxSize, img.Size);
result = img.GetThumbnailImage(sz.Width, sz.Height, null, IntPtr.Zero);
}
I've tested it with my host provider and my local IIS with this in my web.config
<trust level="Medium" originUrl=""/>
Anyway, it works now. I still haven't got the right time to find out why Stream type object would act differently to Image.FromFile, since what I know is those two still streams for the file. I'll post more update if I found out something interesting.
Cheers.