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