Home All Groups Group Topic Archive Search About

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

Author
7 Nov 2006 10:16 PM
boeledi
Dear All,

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

Author
8 Nov 2006 6:03 AM
Cor Ligthert [MVP]
Boeledi,

Will you please not multipost, I have answered your question in another
newsgroup.

Cor

Show quoteHide quote
"boeledi" <didier.boel***@steams.be> schreef in bericht
news:1162937797.527276.6440@h48g2000cwc.googlegroups.com...
> Dear All,
>
> 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
>