PC Review


Reply
Thread Tools Rate Thread

Avoiding SecurityException: System.Security.Permissions.SecurityPermission

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      12th Jan 2008
I have a function that I wrote that add transparency to a
System.Drawing.Image. When using this function on my webhost, I receive the
error

SecurityException: System.Security.Permissions.SecurityPermission

I am still able to generate graphics, but if I use this function I receive
this error. The function does not attempt to access any other files or
resources, and it works when testing it locally. My webhost is
myhosting.com, and the code for my function (which can also be seen on my
website) is:


Imports System.Drawing
Imports System.Drawing.Imaging
Namespace NathanSokalski
Public Class Transparency
Public Shared Function MakeTransparent(ByVal oldbmp As Bitmap, ByVal
transparentcolor As Color) As Bitmap
Dim bmp As New Bitmap(oldbmp.Width, oldbmp.Height,
PixelFormat.Format8bppIndexed)
Dim palette As ColorPalette = bmp.Palette
Dim nextindex As Byte = 0
Dim bmpdata As BitmapData = bmp.LockBits(New Rectangle(0, 0,
oldbmp.Width, oldbmp.Height), ImageLockMode.WriteOnly,
PixelFormat.Format8bppIndexed)
Dim index As Integer
For i As UShort = 0 To 255
palette.Entries(i) = Color.Empty
Next
For y As Integer = 0 To oldbmp.Height - 1
For x As Integer = 0 To oldbmp.Width - 1
'Get the palette index of the current pixel
index = Transparency.InPalette(palette, nextindex, oldbmp.GetPixel(x,
y))
'If the color is not in the palette, add it at the next unused index
If index = -1 Then
palette.Entries(nextindex) = oldbmp.GetPixel(x, y)
index = nextindex
nextindex += CByte(1)
End If
'Set the pixel to the proper index
System.Runtime.InteropServices.Marshal.WriteByte(bmpdata.Scan0, y *
bmpdata.Stride + x, CByte(index))
Next
Next
bmp.UnlockBits(bmpdata)
'If the specified transparent color is included in the palette, change
that color to transparent
If transparentcolor <> Color.Empty AndAlso
Transparency.InPalette(palette, nextindex - CByte(1), transparentcolor)
<> -1 Then palette.Entries(Transparency.InPalette(palette, nextindex -
CByte(1), transparentcolor)) = Color.FromArgb(0, 0, 0, 0)
bmp.Palette = palette
Return bmp
End Function

'Returns number of colors in bitmap
Public Shared Function ColorCount(ByVal bmp As Bitmap) As Integer
Dim palette As New Collections.ObjectModel.Collection(Of Integer)
Dim currcolor As Integer
For y As Integer = 0 To bmp.Height - 1
For x As Integer = 0 To bmp.Width - 1
currcolor = bmp.GetPixel(x, y).ToArgb()
If Not palette.Contains(currcolor) Then palette.Add(currcolor)
Next
Next
Return palette.Count
End Function

'Returns index of color in palette or -1
Private Shared Function InPalette(ByVal palette As ColorPalette, ByVal
maxindex As Byte, ByVal colortofind As Color) As Integer
For i As Byte = 0 To maxindex
If palette.Entries(i).ToArgb() = colortofind.ToArgb() Then Return
CInt(i)
Next
Return -1
End Function
End Class
End Namespace


Can anyone tell me a way to avoid this (or if there isn't one, a webhost
that doesn't prevent me from doing it)? myhosting.com's security policy for
ASP.NET 2.0 can be seen at:

https://support.myhosting.com/Custom...2520Policy.htm

Thanks.
--
Nathan Sokalski
(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Leon Mayne
Guest
Posts: n/a
 
      14th Jan 2008
"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I have a function that I wrote that add transparency to a
>System.Drawing.Image. When using this function on my webhost, I receive the
>error
>
> SecurityException: System.Security.Permissions.SecurityPermission


Which line of code is causing the exception? Is it because the bitmap
operations are trying to write to a temporary path and your web host is
disallowing write access to the temp folder?

 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      15th Jan 2008
I beleive the line of code that is causing the exception is the following:

System.Runtime.InteropServices.Marshal.WriteByte(bmpdata.Scan0, y *
bmpdata.Stride + x, CByte(index))

Because the class does not read from or write to any files, I cannot imagine
what else it could be, since everything else is pretty much just using
properties of the BitMap.
--
Nathan Sokalski
(E-Mail Removed)
http://www.nathansokalski.com/

"Leon Mayne" <leon@rmv_me.mvps.org> wrote in message
news:AB04703A-DE23-4941-9CF3-(E-Mail Removed)...
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>I have a function that I wrote that add transparency to a
>>System.Drawing.Image. When using this function on my webhost, I receive
>>the error
>>
>> SecurityException: System.Security.Permissions.SecurityPermission

>
> Which line of code is causing the exception? Is it because the bitmap
> operations are trying to write to a temporary path and your web host is
> disallowing write access to the temp folder?



 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      15th Jan 2008
You could be right. There is no guarantee that there are 256 entries in a
palette and you may be falling off the end...

You should certainly look at the declared number of palette entries before
you arbitrarily decide to look at a colour entry range of 0-255.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I beleive the line of code that is causing the exception is the following:
>
> System.Runtime.InteropServices.Marshal.WriteByte(bmpdata.Scan0, y *
> bmpdata.Stride + x, CByte(index))
>
> Because the class does not read from or write to any files, I cannot
> imagine what else it could be, since everything else is pretty much just
> using properties of the BitMap.
> --
> Nathan Sokalski
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
> "Leon Mayne" <leon@rmv_me.mvps.org> wrote in message
> news:AB04703A-DE23-4941-9CF3-(E-Mail Removed)...
>> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>>I have a function that I wrote that add transparency to a
>>>System.Drawing.Image. When using this function on my webhost, I receive
>>>the error
>>>
>>> SecurityException: System.Security.Permissions.SecurityPermission

>>
>> Which line of code is causing the exception? Is it because the bitmap
>> operations are trying to write to a temporary path and your web host is
>> disallowing write access to the temp folder?

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.Security.SecurityException: System.Security.Permissions.SecurityPermission Victor Microsoft Dot NET Framework 5 19th Mar 2007 10:16 AM
System.Security.Permissions.SecurityPermission =?Utf-8?B?UGV0ZXIgTmV3bWFu?= Microsoft Dot NET 0 28th Feb 2006 03:48 PM
System.Security.Permissions.SecurityPermission naidanac Microsoft Dot NET Framework 0 23rd Feb 2005 11:40 PM
System.Security.Permissions.SecurityPermission Adrian Microsoft VB .NET 4 17th Jan 2005 12:43 PM
System.Security.Permissions.SecurityPermission Error Glenn Hulls via .NET 247 Microsoft VB .NET 2 23rd Apr 2004 07:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:09 PM.