Avoiding SecurityException: System.Security.Permissions.SecurityPermission

N

Nathan Sokalski

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/Custo...bout%20your%20dot-NET%20Security%20Policy.htm

Thanks.
 
L

Leon Mayne

Nathan Sokalski said:
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?
 
N

Nathan Sokalski

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.
 
B

Bob Powell [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top