asp.net "the object is currently in use elsewhere"

B

boeledi

Dear All,

(First of all this is not a c# piece of code but it does not really
matter).

I would really appreciate if someone could help me.
I am developing an ASP.NET web site and I have to deal with images
upload.
When the user is selecting an image, I save it as such together with a
thumbnail version.
The following piece of code attempts to reduce the image size in order
to generate the thumbnail.
Everything seems to work fine with Internet Explorer 6 but, using
Firefox, I receive the following exception:
"The object is currently in use elsewhere" when trying to save the
bitmap in the MemoryStream.

I don't understand.

Many thanks in advance, for any help.

Didier



'*********************************************************************
'
' pf_ModifyImage Method
'
' Modifies the width or height of an image.
'

'*********************************************************************
Private Function pf_ModifyImage(ByVal original() As Byte, ByVal
width As Integer, ByVal height As Integer, ByVal contentType As String)
As Byte()

' original: byte array of the image
' width: new image width
' height: new image height
' contentType: type of image (this should be either pjpeg or
gif)

' convert byte array to image
Dim stream As New MemoryStream(original)

' convert size to new dimensions
Dim bmp As Bitmap = CType(Image.FromStream(stream), Bitmap)

' Calculate missing width or height
If width = -1 Then
width = Fix(System.Convert.ToDouble(height) /
System.Convert.ToDouble(bmp.Height) *
System.Convert.ToDouble(bmp.Width))
End If
If height = -1 Then
height = Fix(System.Convert.ToDouble(width) /
System.Convert.ToDouble(bmp.Width) *
System.Convert.ToDouble(bmp.Height))
End If

Dim thumbBmp As New Bitmap(bmp, width, height)
thumbBmp.Palette = pf_GetTransparentColorPalette(bmp)

' convert to stream in preparation to convert to byte array
Dim stream2 As MemoryStream = New MemoryStream

' save the stream
' first we need to determine if image is JPG or GIF
' to determine the proper encoder type

If contentType.ToLower().EndsWith("pjpeg") Then
thumbBmp.Save(stream2, ImageFormat.Jpeg)
Else
thumbBmp.Save(stream2, ImageFormat.Gif)
'<<<<<<<<<<<<<<< Exception occurs here.
End If


' cleanup
thumbBmp.Dispose()
bmp.Dispose()

Return stream2.GetBuffer()
End Function 'pf_ModifyImage

Private Function pf_GetTransparentColorPalette(ByVal original As
Bitmap) As ColorPalette
Dim testColor As Color
Dim newColor As Color
Dim pal As ColorPalette = original.Palette

Dim i As Integer
For i = 0 To (pal.Entries.Length - 1) - 1
testColor = pal.Entries(i)

If testColor.A = 0 Then
newColor = Color.FromArgb(0, testColor)
pal.Entries(i) = newColor
End If
Next i

Return pal
End Function 'pf_GetTransparentColorPalette
 
C

Cor Ligthert [MVP]

To change this,

I saw now that you asked this question as well in the language.vb newsgroup
as almost the same time, what is the reason you ask this question here?

Cor
 

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