Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / January 2008

Tip: Looking for answers? Try searching our database.

Avoiding SecurityException: System.Security.Permissions.SecurityPermission

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Sokalski - 12 Jan 2008 16:58 GMT
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/CustomerCare/KnowledgeBase/Article.asp?url=Documen
ts/ASP/Where%2520can%2520I%2520find%2520more%2520details%2520about%2520your%2520
dot-NET%2520Security%2520Policy.htm


Thanks.
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/
Leon Mayne - 14 Jan 2008 10:52 GMT
>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?
Nathan Sokalski - 15 Jan 2008 02:08 GMT
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.
Signature

Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

>>I have a function that I wrote that add transparency to a
>>System.Drawing.Image. When using this function on my webhost, I receive
[quoted text clipped - 5 lines]
> operations are trying to write to a temporary path and your web host is
> disallowing write access to the temp folder?
Bob Powell [MVP] - 15 Jan 2008 07:23 GMT
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.

Signature

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

>I beleive the line of code that is causing the exception is the following:
>
[quoted text clipped - 13 lines]
>> operations are trying to write to a temporary path and your web host is
>> disallowing write access to the temp folder?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.