By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,474 Members | 3,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,474 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
booledi,
This is not a C# language newsgroup question. Post it over at the ASP.NET
group:

http://msdn.microsoft.com/newsgroups...k.aspnet&fltr=
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"boeledi" wrote:
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

Nov 8 '06 #2

P: n/a
Boeledi,

A little bit much work if you use the C# language (or another dotNet program
language), there is for that a thumbnail method in the drawing part

http://msdn2.microsoft.com/en-us/lib...nailimage.aspx

however if your question is about the dotNet language VB.Net than you can
better ask this in future in the newsgroup
microsoft.public.dotnet.languages.vb

I hope this helps,

Cor

"boeledi" <di************@steams.beschreef in bericht
news:11**********************@h54g2000cwb.googlegr oups.com...
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

Nov 8 '06 #3

P: n/a
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

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:eS**************@TK2MSFTNGP03.phx.gbl...
Boeledi,

A little bit much work if you use the C# language (or another dotNet
program language), there is for that a thumbnail method in the drawing
part

http://msdn2.microsoft.com/en-us/lib...nailimage.aspx

however if your question is about the dotNet language VB.Net than you can
better ask this in future in the newsgroup
microsoft.public.dotnet.languages.vb

I hope this helps,

Cor

"boeledi" <di************@steams.beschreef in bericht
news:11**********************@h54g2000cwb.googlegr oups.com...
>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


Nov 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.