Urgent ,Urgent ,Urgent

N

Nir

Hello .

We have a strange problem in our site .

The aspnet_wp process stop to response and his memory start to grow
dramatically,
always after we got an error in one module in our application this
module generate
an jpg file ,randomly this module have this exception :


"The object is currently in use elsewhere.Object Namemscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

This is the module Code :

***********************************************************

Dim width As Integer
Dim height As Integer
Dim size As SizeF
Dim img As New Bitmap(10, 10)
Dim sFileName As String
Dim fnt As Font

'BuildScript()

'define font
If FontFromFile Then
Dim privateFontCollection As New
System.Drawing.Text.PrivateFontCollection()
privateFontCollection.AddFontFile("C:\FontToBMP\Fonts\" &
ImageFontName)
Dim thisFont As FontFamily =
privateFontCollection.Families(0)
fnt = New Font(thisFont, ImageFontSize)

Else
fnt = New Font(ImageFontName, ImageFontSize)
End If

Dim gr As Graphics = Graphics.FromImage(img)


'calc image size
size = gr.MeasureString(ImageText, fnt)

width = CInt(size.Width) + 1
height = CInt(size.Height) + 1

Dim bmp As New Bitmap(width, height)


Dim g As Graphics = Graphics.FromImage(bmp)


'create image background
width = width + 1000

Dim pPen As New Pen(Drawing.Color.White, width)

Dim point1 As New Point(0, 0)
Dim point2 As New Point(0, width)
Dim point3 As New Point(height, width)
Dim point4 As New Point(height, 0)
Dim enc As Encoder = Encoder.Quality
Dim myEncoderParameter As New EncoderParameter(enc, 2000)
Dim myEncoderParameters As EncoderParameters
Dim imgCodecInfo As ImageCodecInfo

imgCodecInfo = GetEncoderInfo("image/jpeg")
myEncoderParameters = New EncoderParameters(1)
myEncoderParameters.Param(0) = myEncoderParameter

Dim curvePoints As Point() = {point1, point2, point3, point4}

g.DrawPolygon(pPen, curvePoints)

'draw text on image
g.DrawString(ImageText, fnt, Brushes.Black, 0, 0)

fnt.Dispose()
g.Dispose()

'define filename
sFileName = Session.SessionID & Rnd() & ".jpg"

bmp.Save(ConfigurationSettings.AppSettings("BarCodeFolder") &
sFileName, imgCodecInfo, myEncoderParameters)
*******************************************************************

The exception occurs in this line "g.DrawString(ImageText, fnt,
Brushes.Black, 0, 0)"



Anybody have an idea why error in this code may cause the
aspnet_wp.exe crash ?

Thank all.
 
G

Girish Bharadwaj

Nir said:
Hello .

We have a strange problem in our site .

The aspnet_wp process stop to response and his memory start to grow
dramatically,
always after we got an error in one module in our application this
module generate
an jpg file ,randomly this module have this exception :


"The object is currently in use elsewhere.Object Namemscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

This is the module Code :

***********************************************************

Dim width As Integer
Dim height As Integer
Dim size As SizeF
Dim img As New Bitmap(10, 10)
Dim sFileName As String
Dim fnt As Font

'BuildScript()

'define font
If FontFromFile Then
Dim privateFontCollection As New
System.Drawing.Text.PrivateFontCollection()
privateFontCollection.AddFontFile("C:\FontToBMP\Fonts\" &
ImageFontName)
Dim thisFont As FontFamily =
privateFontCollection.Families(0)
fnt = New Font(thisFont, ImageFontSize)

Else
fnt = New Font(ImageFontName, ImageFontSize)
End If

Dim gr As Graphics = Graphics.FromImage(img)


'calc image size
size = gr.MeasureString(ImageText, fnt)

width = CInt(size.Width) + 1
height = CInt(size.Height) + 1

Dim bmp As New Bitmap(width, height)


Dim g As Graphics = Graphics.FromImage(bmp)


'create image background
width = width + 1000

Dim pPen As New Pen(Drawing.Color.White, width)

Dim point1 As New Point(0, 0)
Dim point2 As New Point(0, width)
Dim point3 As New Point(height, width)
Dim point4 As New Point(height, 0)
Dim enc As Encoder = Encoder.Quality
Dim myEncoderParameter As New EncoderParameter(enc, 2000)
Dim myEncoderParameters As EncoderParameters
Dim imgCodecInfo As ImageCodecInfo

imgCodecInfo = GetEncoderInfo("image/jpeg")
myEncoderParameters = New EncoderParameters(1)
myEncoderParameters.Param(0) = myEncoderParameter

Dim curvePoints As Point() = {point1, point2, point3, point4}

g.DrawPolygon(pPen, curvePoints)

'draw text on image
g.DrawString(ImageText, fnt, Brushes.Black, 0, 0)

fnt.Dispose()
g.Dispose()

'define filename
sFileName = Session.SessionID & Rnd() & ".jpg"

bmp.Save(ConfigurationSettings.AppSettings("BarCodeFolder") &
sFileName, imgCodecInfo, myEncoderParameters)
*******************************************************************

The exception occurs in this line "g.DrawString(ImageText, fnt,
Brushes.Black, 0, 0)"



Anybody have an idea why error in this code may cause the
aspnet_wp.exe crash ?

Thank all.

You might have to dispose all Graphic objects. I have also seen this
when I attempted creating too many image objects. You have "gr" variable
that is not Dispose()d.
 

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